RecyclerView嵌套ScrollView导致RecyclerView内容显示不全

我们在使用RecyclerView嵌套至ScrollView内的时候

RecyclerView不在屏幕内的数据会不显示出来,这里是一个坑,我们需要重写RecyclerView

/**
 * Created by Arcturis lfy on 2019/3/26.
 * <p/>
 * 解决ScrollView内嵌套引起MyRecyclerView显示不全的问题
 * <p>Copyright</p>
 */
public class MyRecyclerView extends RecyclerView {

    public MyRecyclerView(@NonNull Context context) {
        super(context);
    }

    public MyRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyRecyclerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public boolean canScrollVertically(int direction) {
        return false;
    }

    @Override
    public boolean canScrollHorizontally(int direction) {
        return false;
    }
}

即 canScrollVertically 和 canScrollHorizontally这两个方法返回为false

而且需要在RecyclerView外层包裹一个RelativeLayout

这样重新运行下,就能显示出来了

        <ScrollView
            android:id="@+id/scrollView_right_up"
            android:layout_width="match_parent"
            android:scrollbars="none"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <demo.MyRecyclerView
                    android:id="@+id/recyclerview_right_up"
                    android:layout_width="match_parent"
                    android:layout_height="40dp">

                </demo.MyRecyclerView>

            </RelativeLayout>



        </ScrollView>

猜你喜欢

转载自www.cnblogs.com/fengfenghuifei/p/10602049.html