NestedScrollView中嵌套RecycleView

来源:互联网 发布:烛魔枪一段数据 编辑:程序博客网 时间:2024/04/16 16:35
想做一个需求为1、整体滑动View(NestedScrollView)2、滑动视图内包括上部轮播广告View,下部类Gridview不可滑动View(RecycleView)简单说就是可以统一滑动界面中视图

主要实现方法:
1、布局文件

<?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"    android:background="@android:color/white"    tools:context="com.zlh.mvp_retrofit2_rxjava_rxbus.MainActivity">    <android.support.v7.widget.Toolbar        android:id="@+id/toolbar"        android:layout_width="match_parent"        android:layout_height="?attr/actionBarSize"        android:background="?attr/colorPrimary"        app:popupTheme="@style/AppTheme.PopupOverlay" />    <android.support.v4.widget.NestedScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:fitsSystemWindows="true"        android:fillViewport="true">        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical">            <android.support.v4.view.ViewPager                android:id="@+id/convenientBanner"                android:layout_width="match_parent"                android:layout_height="100dp"/>            <android.support.v7.widget.RecyclerView                android:id="@+id/main_recycleview"                android:layout_width="match_parent"                android:layout_height="wrap_content"/>        </LinearLayout>    </android.support.v4.widget.NestedScrollView></LinearLayout>

注:需要在NestedScrollView中加入android:fillViewport=”true”,否则不显示

2、Activity实现代码

private void initView() {        setSupportActionBar(toolbar);        //mainPresenter = MainPresenter.getInstent(this);        //mainPresenter.getAdNetworkImages(); //获取网络图片信息        AutoGridRecycleViewAdapter adapter = new AutoGridRecycleViewAdapter();        mainRecycleview.setLayoutManager(new AutoFitGridLayoutManager(MainActivity.this,2,adapter,mainRecycleview));        mainRecycleview.setAdapter(adapter);        mainRecycleview.setNestedScrollingEnabled(false);        mainRecycleview.setHasFixedSize(true);    }

其中有两个自定义类
AutoGridRecycleViewAdapter是自定义RecycleView.Adapter;
AutoFitGridLayoutManager是自定义GridLayoutManager,动态计算RecycleView高度。

//RecycleView固定大小会实现优化
mainRecycleview.setHasFixedSize(true);

注:如果不加如下代码则滑动时“不顺畅”、“卡顿”、“粘滞”
//NesterScrollView不允许滑动
mainRecycleview.setNestedScrollingEnabled(false);

0 0
原创粉丝点击