Android RecyclerView添加头部

来源:互联网 发布:淘宝花卉店铺推荐 编辑:程序博客网 时间:2024/05/01 23:36


首先,添加依赖:

compile 'com.zhy:base-rvadapter:3.0.3'
单独写一个布局,想添加啥就写啥(我添加的是XBanner),布局如下:xb.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="150dp">    <com.stx.xhb.xbanner.XBanner        android:id="@+id/xbanner"        android:layout_width="match_parent"        android:layout_height="150dp"        android:layout_below="@+id/tablayout">    </com.stx.xhb.xbanner.XBanner></LinearLayout>

然后,获取布局(头部布局)和控件
View view1 = View.inflate(getActivity(), R.layout.xb,null);
XBanner xbanner = (XBanner) view1.findViewById(R.id.xbanner);

最后,在添加适配器的时候写(请求玩数据,要添加适配器):

FragAdapter fa=new FragAdapter(getActivity(),list);//实例化适配器mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(fa);//实例化头部xrecyclerview.setAdapter(fa);//xrecyclerview添加适配器mHeaderAndFooterWrapper.addHeaderView(view1);//添加头部,放入xbanner的布局xrecyclerview.setAdapter(mHeaderAndFooterWrapper);//xrecyclerview添加头部mHeaderAndFooterWrapper.notifyDataSetChanged();//刷新