NestedScrollView在调整大小的Recyclerview上滚动到顶部 [英] NestedScrollView scrolls to top on Recyclerview resized

查看:128
本文介绍了NestedScrollView在调整大小的Recyclerview上滚动到顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NestedScrollView,其中包含LinearLayout和RecyclerView(都在RelativeLayout内部).

I have a NestedScrollView containing a LinearLayout and a RecyclerView (both inside a RelativeLayout).

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scroll">

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lay_account">


        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/white"
                android:paddingTop="10dp"
                android:id="@+id/lay_total"
                android:paddingBottom="10dp">

            <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/total"
                    android:id="@+id/lblTotal"
                    android:textSize="@dimen/text_medium"
                    android:layout_marginLeft="20dp"
                    android:textColor="@color/dark_eurakostheme_color"
                    android:textStyle="bold"/>


            <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical|center_horizontal">

                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/txtTotalAmount"
                        android:textSize="@dimen/text_extra_extra_large"
                        android:gravity="center_horizontal"
                        android:textColor="@color/eurakostheme_color"/>

                <ImageView
                        android:layout_width="@dimen/icon_extra_extra_large"
                        android:layout_height="match_parent"
                        android:id="@+id/imageView3"
                        android:src="@drawable/coin_eurakos"/>
            </LinearLayout>

        </LinearLayout>

        <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:id="@+id/divisor"
                android:background="@color/dividerColor"
                android:layout_alignBottom="@+id/lay_total"/>

        <android.support.v7.widget.RecyclerView
                android:layout_below="@id/lay_total"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/rclBasketAmounts"
                android:background="@android:color/white"
                android:padding="10dp">
        </android.support.v7.widget.RecyclerView>

    </RelativeLayout>

</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

将数据加载到RecyclerView之后,我需要像这样通过编程方式更改其高度:

After loading the data into the RecyclerView, I need to change it's height programmatically like this:

RelativeLayout.LayoutParams lay_params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
    50 + ((int) (baskets.length * getResources().getDimension(R.dimen.list_height_small))));

lay_params.addRule(RelativeLayout.BELOW, lay_total.getId());
recyclerView.setLayoutParams(lay_params);
recyclerView.setAdapter(new ListBasketAmountAdapter(baskets));

问题是当数据加载完成时,NestedScrollView滚动条会自动滚动到RecyclerView的顶部,将LinearLayout隐藏在其上方.有什么想法吗?

The problem is when the data has finished loading the NestedScrollView scrolls automatically scrolls to the top of the RecyclerView, hiding the LinearLayout above it. Any ideas?

谢谢.

推荐答案

几天后,我找到了解决该问题的方法.您只需要在ScrollView下的第一个布局中添加descendantFocusability,如下所示:

After several days I've found a solution to the problem. You just need to add the descendantFocusability in the first layout under the ScrollView like this:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lay_account"
    android:descendantFocusability="blocksDescendants">

这篇关于NestedScrollView在调整大小的Recyclerview上滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆