NestedScrollView 嵌套RecyclerView 外加RecyclerView的上拉下载

引入:

implementation 'com.lcodecorex:tkrefreshlayout:1.0.7'

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
        android:id="@+id/trl_main_activity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/mNestView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:orientation="vertical">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:background="@color/colorAccent"/>

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/rv_main_activity"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>

            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>

    </com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout>
</LinearLayout>

java文件:

    private void initRecyclerView(List<MainDataBean> dataBeans) {
        if (mMainAdapter == null) {
            LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            // 默认为true
            manager.setSmoothScrollbarEnabled(true);
            // 允许自动测量
            manager.setAutoMeasureEnabled(true);
            mRecyclerView.setLayoutManager(manager);
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setNestedScrollingEnabled(false);
            mMainAdapter = new MainAdapter(mData);
            mRecyclerView.setAdapter(mMainAdapter);
        } else {
            mMainAdapter.setData(mData);
        }
    }

    public void initRefreshLayout() {
        mTwinklingRefreshLayout.setEnableOverScroll(false);
        mTwinklingRefreshLayout.setEnableRefresh(false);
        mTwinklingRefreshLayout.setEnableLoadmore(true);
        mTwinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                if (mRecyclerView == null) { return; }
                refreshData();
                refreshLayout.finishLoadmore();
            }
        });
    }

### 回答1: 嵌套ScrollViewRecyclerView时,可以使用NestedScrollView来解决滑动冲突的问题。NestedScrollView是Android 5.引入的一个新控件,它可以嵌套其他可滑动的控件,如RecyclerView、ListView等。在嵌套时,需要在RecyclerView的外层包裹一个NestedScrollView,并在NestedScrollView中设置app:layout_behavior="@string/appbar_scrolling_view_behavior"属性,以便与AppBarLayout联动。同时,需要在RecyclerView中设置android:nestedScrollingEnabled="false"属性,以禁止RecyclerView自身的滑动事件。这样就可以实现NestedScrollView嵌套RecyclerView的效果了。 ### 回答2: NestedScrollView是一个可以嵌套滚动的视图容器,可以方便地实现嵌套滚动的效果。而RecyclerView是一个能够高效地维护大量数据的控件,它可以展示大数据集合,并且拥有灵活的布局以及可定制性。因此,嵌套ScrollViewRecyclerView的需求是非常普遍的。 在使用NestedScrollView嵌套RecyclerView的时候,需要注意以下几个问题: 1.布局的设置 嵌套布局时需要设置正确的布局层级,例如可以将NestedScrollView作为父布局,让它的孩子是RecyclerView。 2.增加嵌套滑动事件 NestedScrollView默认不具备嵌套滑动事件,因此需要在RecyclerView中增加相应的滑动事件来实现嵌套滑动。 3.优化RecyclerView性能 在RecyclerView中可能会存在大量的数据,因此需要做一些性能优化。例如可以使用ViewHolder来重用子视图,使用LayoutManager来管理子视图的布局等等。 4.合理设置RecyclerView高度 由于RecyclerView的高度需要根据具体情况来设置,因此需要根据每个item的高度来设置RecyclerView的高度,否则可能会出现一些问题。 综上所述,NestedScrollView嵌套RecyclerView需要考虑的问题比较多,需要开发者综合考虑各种情况来尽可能达到良好的用户体验和流畅的界面效果。 ### 回答3: NestedScrollView嵌套RecyclerView是常见的布局方式,可以应用在需要列表上下滑动,同时支持整个界面上下滑动的情况下。 NestedScrollView是Android 5.0之后提供的用于嵌套滑动的控件,它继承自ScrollView,但比ScrollView功能更强大,可以实现更复杂的嵌套滑动效果。而RecyclerView是Android系统提供的一个列表控件,比ListView具有更好的性能和灵活性。 在嵌套RecyclerView时,我们需要先将RecyclerView的滑动事件交给外层的NestedScrollView来处理,这可以通过给RecyclerView设置一个OnTouchListener来实现。 同时,在NestedScrollView中,我们可以设置子view这个属性app:layout_behavior="@string/appbar_scrolling_view_behaviour",也就是将NestedScrollView的滑动事件交给外层的AppBarLayout来处理。 在使用上,我们需要了解两个重要的方法:onNestedPreScroll和onNestedPreFling。这两个方法分别用于处理子view传递过来的滑动事件,我们需要在这两个方法中做出相应处理,从而实现NestedScrollView内部RecyclerView与外部AppBarLayout的联动效果。 另外,为了保证RecyclerView能够完全显示出来,我们还需要在RecyclerView的外层布局中设置android:layout_height="wrap_content"属性。 综上所述,NestedScrollView嵌套RecyclerView需要注意以下几点:将RecyclerView的滑动事件交给外层的NestedScrollView处理;设置子view的app:layout_behavior;重写onNestedPreScroll和onNestedPreFling方法;设置RecyclerView的外层布局高度为wrap_content。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值