android: RecyclerView inside a ScrollView

落爺英雄遲暮 提交于 2019-11-27 20:44:24

Set this property for the ScrollView,

 android:fillViewport="true"

ScrollView will extend itself to fill the contents

After checking implementation, the reason appears to be the following. If RecyclerView gets put into a ScrollView, then during measure step its height is unspecified (because ScrollView allows any height) and, as a result, gets equal to minimum height (as per implementation) which is apparently zero.

You have couple of options for fixing this:

  • Set a certain height to RecyclerView
  • Set ScrollView.fillViewport to true
  • Or keep RecyclerView outside of ScrollView. I my opinion, this is the best option by far. If RecyclerView height is not limited - which is the case when it's put into ScrollView - then all Adapter's views have enough place vertically and get created all at once. There is no view recycling anymore which kinda breaks the purpose of RecyclerView.
neustart47

Nothing helped me except this:

mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        int action = e.getAction();
        switch (action) {
        case MotionEvent.ACTION_MOVE:
            rv.getParent().requestDisallowInterceptTouchEvent(true);
            break;
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
});

I got this answer there. Thank you Piyush Gupta for that.

Using RecyclerView inside ScrollView is not a good practice.

When RecyclerView is wrapping its content, it’s not recycling anymore. Every record in the dataset has an item View kept in memory for as long as the RecyclerView is in the layout hierarchy.

Read more here - https://android.jlelse.eu/android-dev-tip-4-91b7757b1f0a

Hope this helps :

Add this line to your recyclerView xml :

android:nestedScrollingEnabled="false"

Try it ,recyclerview will be smoothly scrolled with flexible height inside scrollview .

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