问题
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>
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));
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?
Thanks.
回答1:
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">
回答2:
The solution given by kevings14 works but it wasn't helpful in my case. I've a RecyclerView
+ a couple of EditText
under NestedScrollView
, when I added
android:descendantFocusability="blocksDescendants"
in main layout under scroll view. I was unable to focus EditText
.
Then I came up with this solution, which worked for me.
<RelativeLayout
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content">
回答3:
In my case, I use multiple RecyclerView inside NestedScrollView. Then it does not work for me. So my solution is, When my data update complete then I just scroll to top,
yourList.setAdapter(yourListAdapter);
scrollView.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_UP);
//scrollView.scrollTo(0,0);
}
});
回答4:
I had a nestedscrollview that would get scrolled up by itself. In the post run method of the recyclerview, I scrolled the nestedscrollview to (0,0) and all good.
rvComments.setAdapter(adapterComment);
rvComments.post(new Runnable() {
@Override
public void run() {
nestedScrollView.scrollTo(0, 0);
}
});
来源:https://stackoverflow.com/questions/34329707/nestedscrollview-scrolls-to-top-on-recyclerview-resized