RecyclerView inside ScrollView is not working

前端 未结 26 1510
梦如初夏
梦如初夏 2020-11-22 05:37

I\'m trying to implement a layout which contains RecyclerView and ScrollView at the same layout.

Layout template:


    

        
相关标签:
26条回答
  • 2020-11-22 06:24

    **Solution which worked for me
    Use NestedScrollView with height as wrap_content

    <br> RecyclerView 
                    android:layout_width="match_parent"<br>
                    android:layout_height="wrap_content"<br>
                    android:nestedScrollingEnabled="false"<br>
                      app:layoutManager="android.support.v7.widget.LinearLayoutManager"
                    tools:targetApi="lollipop"<br><br> and view holder layout 
     <br> android:layout_width="match_parent"<br>
            android:layout_height="wrap_content"
    

    //Your row content goes here

    0 讨论(0)
  • 2020-11-22 06:25

    I used CustomLayoutManager to disable RecyclerView Scrolling. Also don't use Recycler View as WrapContent, use it as 0dp, Weight=1

    public class CustomLayoutManager extends LinearLayoutManager {
        private boolean isScrollEnabled;
    
        // orientation should be LinearLayoutManager.VERTICAL or HORIZONTAL
        public CustomLayoutManager(Context context, int orientation, boolean isScrollEnabled) {
            super(context, orientation, false);
            this.isScrollEnabled = isScrollEnabled;
        }
    
        @Override
        public boolean canScrollVertically() {
            //Similarly you can customize "canScrollHorizontally()" for managing horizontal scroll
            return isScrollEnabled && super.canScrollVertically();
        }
    }
    

    Use CustomLayoutManager in RecyclerView:

    CustomLayoutManager mLayoutManager = new CustomLayoutManager(getBaseActivity(), CustomLayoutManager.VERTICAL, false);
            recyclerView.setLayoutManager(mLayoutManager);
            ((DefaultItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false); 
            recyclerView.setAdapter(statsAdapter);
    

    UI XML:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_main"
        android:fillViewport="false">
    
    
        <LinearLayout
            android:id="@+id/contParentLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <edu.aku.family_hifazat.libraries.mpchart.charts.PieChart
                    android:id="@+id/chart1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/x20dp"
                    android:minHeight="@dimen/x300dp">
    
                </edu.aku.family_hifazat.libraries.mpchart.charts.PieChart>
    
    
            </FrameLayout>
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
    
    
            </android.support.v7.widget.RecyclerView>
    
    
        </LinearLayout>
    
    
    </ScrollView>
    
    0 讨论(0)
  • 2020-11-22 06:27

    I was having the same problem. That's what i tried and it works. I am sharing my xml and java code. Hope this will help someone.

    Here is the xml

    <?xml version="1.0" encoding="utf-8"?>
    

    < NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:id="@+id/iv_thumbnail"
                android:layout_width="match_parent"
                android:layout_height="200dp" />
    
            <TextView
                android:id="@+id/tv_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Description" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Buy" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Reviews" />
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rc_reviews"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
            </android.support.v7.widget.RecyclerView>
    
        </LinearLayout>
    </NestedScrollView >
    

    Here is the related java code. It works like a charm.

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setNestedScrollingEnabled(false);
    
    0 讨论(0)
  • 2020-11-22 06:29

    If you put RecyclerView inside NestedScrollView and enable recyclerView.setNestedScrollingEnabled(false);, scrolling will working well.
    However, there is a problem

    RecyclerView don't recycle

    For example, your RecyclerView (inside NestedScrollView or ScrollView) have 100 item.
    When Activity launch, 100 item will create (onCreateViewHolder and onBindViewHolder of 100 item will called at same time).
    Example, for each item, you will load a large image from API => activity created -> 100 image will load.
    It make starting Activity slowness and lagging.
    Possible solution:
    - Thinking about using RecyclerView with multiple type.

    However, if in your case, there are just a few item in RecyclerView and recycle or don't recycle don't affect performance a lot, you can use RecyclerView inside ScrollView for simple

    0 讨论(0)
  • 2020-11-22 06:29

    If RecyclerView showing only one row inside ScrollView. You just need to set height of your row to android:layout_height="wrap_content".

    0 讨论(0)
  • 2020-11-22 06:32

    For those people who trying to do it just for design purposes - leave it. Redesign your app and leave only RecyclerView. It will be better solution than doing ANY hardcode.

    0 讨论(0)
提交回复
热议问题