NestedScrollView在Recyclerview调整大小时滚动到顶部

kev*_*s14 28 android android-recyclerview nestedscrollview

我有一个NestedScrollView包含一个LinearLayout和一个RecyclerView(都在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>
Run Code Online (Sandbox Code Playgroud)

将数据加载到RecyclerView后,我需要以编程方式更改它的高度,如下所示:

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));
Run Code Online (Sandbox Code Playgroud)

问题是当数据加载完NestedScrollView时,滚动会自动滚动到RecyclerView的顶部,将LinearLayout隐藏在它上面.有任何想法吗?

谢谢.

kev*_*s14 101

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lay_account"
    android:descendantFocusability="blocksDescendants">
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案有效,但请记住,如果在子视图中有一个`EditText`,那么它将无法调焦. (3认同)

Nom*_*que 15

kevings14给出的解决方案有效,但在我的情况下没有用.当我补充说,我有一RecyclerView两个EditText以下NestedScrollView

android:descendantFocusability="blocksDescendants"
Run Code Online (Sandbox Code Playgroud)

在滚动视图下的主布局中.我无法集中注意力EditText.

然后我想出了这个对我有用的解决方案.

<RelativeLayout
   android:layout_width="match_parent"
   android:focusable="true"
   android:focusableInTouchMode="true"
   android:layout_height="wrap_content">
Run Code Online (Sandbox Code Playgroud)


Md *_*ury 5

就我而言,我在 NestedScrollView 中使用多个 RecyclerView。那么它对我不起作用。所以我的解决方案是,当我的数据更新完成时,我只需滚动到顶部,

yourList.setAdapter(yourListAdapter);

scrollView.post(new Runnable() {
    @Override
    public void run() {
         scrollView.fullScroll(ScrollView.FOCUS_UP);
         //scrollView.scrollTo(0,0);
    }
});
Run Code Online (Sandbox Code Playgroud)