NestedScrollView scrolls to top on Recyclerview resized

房东的猫 提交于 2019-12-02 17:58:47
kevings14

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">

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">

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);
    }
});

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);
        }
    });

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!