繁体   English   中英

Android NestedScrollView 在 app:layout_behavior 后尺寸错误

[英]Android NestedScrollView has wrong size after app:layout_behavior

由于谷歌已经发布了 android 的设计支持库,有很多不错的事情可以在不实现自定义代码的情况下完成。 虽然我已经测试了这个库中的自定义视图,但我发现了一个更糟糕的事情,我不知道这是否是一个错误。

我在 github 上找到了 cheesesquare 项目。 activity_detail.xml(布局文件)中,NestedScrollView 中有3 个CardView。 如果删除其中的 2 个,您可以看到 NestedScrollView 没有父级 (match_parent) 的完整大小。 NestedScrollView 绑定到父视图的底部。 http://i.stack.imgur.com/BXl7w.png

当我删除app:layout_behavior="@string/appbar_scrolling_view_behavior"时, NestedScrollView 会得到他的全尺寸。

但是当我删除布局行为时,工具栏不会折叠。

有什么解决办法吗? 示例布局文件可以在这里找到: https : //github.com/Smove/cheesesquare/blob/stackoverflow/app/src/main/res/layout/activity_detail.xml

你可以从我的 github 分支stackoverflow构建 cheesesquare apk

我遇到了这个问题并修复了添加:

android:layout_gravity="fill_vertical"

到 NestedScrollView。 然后它开始正常运行,正如我在这里解释的那样。 当然,NestedScrollView 需要某种孩子(即它不能为空),否则工具栏不会折叠。

更新

虽然这种小含量的效果很好,我注意到它有一些问题,显示较长内容,如喜欢全activity_detail.xml以上。 问题是您无法滚动到内容的最底部 - 它在底部无法到达。

从我的测试中,我可以发现缺失的部分与折叠的工具栏一样大(或者至少在我看来是这样)。 为了解决这个问题,并且有一个对小内容和大内容都可靠的解决方案,您应该向 ScrollView 添加layout_marginBottom ,以便对其进行测量并释放缺少的底部部分。 因此:

android:layout_gravity="fill_vertical"
android:layout_marginBottom="?attr/actionBarSize"

或您给Toolbar提供的任何大小。

但还是

使用此解决方案滚动小内容,即使内容恰好在顶部对齐,也不会像滚动大内容那样平滑。 我会一直使用,直到库修复到来。

更新2

看起来这已在 v22.2.1 中修复。

使用@natario的回答

<androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_gravity="fill_vertical">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingBottom="?attr/actionBarSize">
                <!--Rest of the code-->

或者在 Kotlin 中,您可以执行以下操作:

 coordinator.doOnLayout {
        nestedScrollView.minimumHeight = resources.displayMetrics.heightPixels - with(TypedValue().also {theme.resolveAttribute(android.R.attr.actionBarSize, it, true)}) {
            TypedValue.complexToDimensionPixelSize(data, resources.displayMetrics)}
    }

添加android:minHeight="?attr/actionBarSize"CollapsingToolbarLayout

解决方法

在显示我的 NestedScrollView 之前和将数据绑定到 NestedScrollView 内容之后,我使用 View.FOCUS_UP 方向作为参数调用我的 NestedScrollView 实例的 fullScroll(int direction) 方法。

片段的代码示例:

NestedScrollView scrollView = (NestedScrollView) getActivity().findViewById(R.id.scroll_view); scrollView.fullScroll(View.FOCUS_UP);

使用 RecyclerView 替换 NestedScrollView 修复这个 bug,设置 item count 1,即 ViewHolder 返回你真正的 contentView;

我的代码:

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
    // 添加分割线
    recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext()));

    recyclerView.setAdapter(new Adapter<ViewHolder>() {

        @Override
        public int getItemCount() {
            return 1;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int arg1) {
            WebView view = (WebView) holder.itemView;
            view.getSettings().setJavaScriptEnabled(true);
            view.loadUrl("http://www.baidu.com");
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
            return new ViewHolder(inflater.inflate(R.layout.webview, arg0, false)) {
            };
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM