Android RecyclerView从入门到玩坏

目录

  • 前言
  • 基础使用
  • 分隔线
  • 点击监听
  • 搭配CardView
  • 更丰富的条目
  • 增删条目
  • 快速添加视图
  • 让RecyclerView支持复杂视图
  • 最后

前言

RecyclerView在Android界面开发当中是很重要的, 那掌握它也是很必要的. 但是有些时候会觉得它很厚重, 这里就从RecyclerView的基础一直说到扩展, 让你把RecyclerView学薄了.

RecyclerView官方文档也是非常厚重.

这篇文章融合了自己原来的多篇文章, 并进行了修正和改进, 而且添加了很多很有趣的内容. 本文需要20分钟以上的阅读时间, 请合理安排. 多图预警, 转载请注明出处!


基础使用

要使用RecyclerView在Android Studio 2.x(以下简称AS), 要这样:

compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
复制代码

到了AS 3.x, 要这样:

implementation 'com.android.support:cardview-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
复制代码

之后在布局文件中写入如下代码就引入了RecyclerView了.

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical" />
复制代码

接下来说说介绍些各种布局. 可以看RecyclerView.LayoutManager官方文档.

布局类效果
LinearLayoutManager以垂直或水平滚动列表方式显示项目
GridLayoutManager在网格中显示项目
StaggeredGridLayoutManager在分散对齐网格中显示项目
mRvMain = (RecyclerView) findViewById(R.id.rv_main);

// 设置布局
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRvMain.setLayoutManager(linearLayoutManager);
复制代码

最关键的还是适配器的撰写. 但是理解起来不是很难, 你只要将ListView的适配器写法带入理解就好. 这里把全部代码贴出来, 因为后面要在这个基础上不断扩充.

public class MyRVAdapter2 extends RecyclerView.Adapter<MyRVAdapter2.MyTVHolder> {

    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    private final ArrayList<String> mData;

    public MyRVAdapter2(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;

        mData = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            mData.add("hello " + i);
        }
    }

    @Override
    public MyRVAdapter2.MyTVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyRVAdapter2.MyTVHolder(mLayoutInflater.inflate(R.layout.rv_txt_item, parent, false));
    }

    @Override
    public void onBindViewHolder(final MyRVAdapter2.MyTVHolder holder, int pos) {
        holder.mTextView.setText(mData.get(pos));
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class MyTVHolder extends RecyclerView.ViewHolder {
        TextView mTextView;

        MyTVHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.tv_txt);
        }
    }
}
复制代码

然后写个最基础的TextView条目. 让它跑起来看看效果.

<?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="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="@dimen/eight_dp"
        android:text="@string/tmp"
        android:textSize="@dimen/thirty_sp" />
</LinearLayout>
复制代码


分隔线

前面的部分已经是基础的RecyclerView使用了. 那比起ListView是不是没有了分隔线. 这里上一个简单好用的开源库RecyclerView-FlexibleDivider.

引入:

implementation 'com.yqritc:recyclerview-flexibledivider:1.4.0'
复制代码

使用:

mRvMain.addItemDecoration(
        new HorizontalDividerItemDecoration.Builder(this).build());
复制代码

看效果就达到了吧.

觉得不好看, 还可以自定义, 更多写法可以参见文档内容.

mRvMain.addItemDecoration(
        new HorizontalDividerItemDecoration.Builder(this)
                .color(Color.BLUE)
                .sizeResId(R.dimen.two_dp)
                .marginResId(R.dimen.eight_dp, R.dimen.eight_dp)
                .build());
复制代码

而且而且, 竖着的分隔线也大丈夫哦.

GridLayoutManager gridLayoutManager
        = new GridLayoutManager(this, 2);
mRvMain.setLayoutManager(gridLayoutManager);
复制代码
mRvMain.addItemDecoration(
        new VerticalDividerItemDecoration.Builder(this).build());
复制代码


点击监听

再回忆一下在天国的ListView, 还有item的点击吧, 这个也要自己写.

适配器中:

public interface OnItemClickListener {
    void onItemClick(View view, int position);

    void onItemLongClick(View view, int position);
}

private MyRVAdapter2.OnItemClickListener mOnItemClickListener;

public void setOnItemClickListener(MyRVAdapter2.OnItemClickListener mOnItemClickListener) {
    this.mOnItemClickListener = mOnItemClickListener;
}
复制代码

onBindViewHolder中设置点击监听.

@Override
public void onBindViewHolder(final MyRVAdapter2.MyTVHolder holder, int pos) {
    holder.mTextView.setText(mData.get(pos));

    if (mOnItemClickListener != null) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.onItemClick(holder.itemView, pos);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                int pos = holder.getLayoutPosition();
                mOnItemClickListener.onItemLongClick(holder.itemView, pos);
                return false;
            }
        });
    }
}
复制代码

使用监听:

mAdapter.setOnItemClickListener(new MyRVAdapter2.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        Toast.makeText(UIUtil.getContext(), "click" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onItemLongClick(View view, int position) {
        Toast.makeText(UIUtil.getContext(), "long click" + position, Toast.LENGTH_SHORT).show();
    }
});
复制代码


搭配CardView

是不是这个点击看着没啥感觉, 没事, 我们换上CardView再来一次.

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/eight_dp"
    android:foreground="@drawable/card_foreground"
    card_view:cardCornerRadius="@dimen/four_dp">

    <TextView
        android:id="@+id/tv_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="@dimen/eight_dp"
        android:text="@string/tmp"
        android:textSize="@dimen/thirty_sp" />
</android.support.v7.widget.CardView>
复制代码

给CardView加上水波纹点击特效:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/colorPrimary" />
复制代码

在老版本就只能用选择器了, 其实效果也还好:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/card_foreground_selector"
    android:insetBottom="@dimen/four_dp"
    android:insetLeft="@dimen/three_dp"
    android:insetRight="@dimen/three_dp"
    android:insetTop="@dimen/four_dp" />
复制代码
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimaryTran" />
            <corners android:radius="@dimen/four_dp" />
        </shape>
    </item>
    <item android:state_enabled="true" android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="#0f000000" />
            <corners android:radius="@dimen/four_dp" />
        </shape>
    </item>
</selector>
复制代码


更丰富的条目

大家应该都知道TextView可以设置图标吧, 这里来看下效果图, 顺带感受下android界面设计语言的变化.

<?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="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@mipmap/ic_launcher"
        android:drawablePadding="@dimen/sixteen_dp"
        android:drawableStart="@mipmap/ic_launcher"
        android:gravity="center_vertical"
        android:padding="@dimen/eight_dp"
        android:text="@string/tmp"
        android:textSize="@dimen/thirty_sp" />
</LinearLayout>
复制代码

让GridLayoutManager展示不同宽度的条目

方的是4.x上的, 圆的是8.x上的, 可以看到, 变化还是很大的. 我们回正题. GridLayoutManager布局是可以设置宽度的, 不一定都是一样大的, 来看下实现.

// 指定item宽度
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        if (position == 0
                || position == (mAdapter.getItemCount() - 1) / 2
                || position == (mAdapter.getItemCount() - 1)) {
            return gridLayoutManager.getSpanCount();
        } else {
            return 1;
        }
    }
});
复制代码

来看效果图, 发现我们的分隔线崩了是吧, 如果真想用这个分隔线也还是要自己动手修补修补, 改动改动, 开源库再棒也猜不到你的项目需求呀.

当然了, 我还是很喜欢这个分隔线的, 我们来看看横着滚动的效果.

布局文件要改动:

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

    <TextView
        android:id="@+id/tv_txt"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="@string/tmp"
        android:textSize="@dimen/thirty_sp" />
</LinearLayout>
复制代码
gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL);
复制代码


展示不同布局

之前变化宽度其实还是相同条目, 现在要展示不同条目:

写一个图的条目:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/eight_dp">

    <ImageView
        android:id="@+id/iv_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />

</RelativeLayout>
复制代码
public enum ITEM_TYPE {
    ITEM_TYPE_IMAGE,
    ITEM_TYPE_TEXT
}
复制代码

这里多了判断条目类型, 还要注意返回值的变化, 用了更基类的RecyclerView.ViewHolder.

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal()) {
        return new MyRVAdapter2.MyIVHolder(mLayoutInflater.inflate(R.layout.rv_img_item, parent, false));
    } else {
        return new MyRVAdapter2.MyTVHolder(mLayoutInflater.inflate(R.layout.rv_txt_item, parent, false));
    }
}
复制代码

类继承上面也要变成RecyclerView.ViewHolder, 这些都是要对应的.

extends RecyclerView.Adapter<RecyclerView.ViewHolder>
复制代码

当然了, holder也是不能少的.

public class MyIVHolder extends RecyclerView.ViewHolder {
    ImageView mImageView;

    MyIVHolder(View view) {
        super(view);
        mImageView = (ImageView) view.findViewById(R.id.iv_img);
    }
}
复制代码
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int pos) {
    if (holder instanceof MyRVAdapter2.MyTVHolder) {
        ((MyRVAdapter2.MyTVHolder) holder).mTextView.setText(mData.get(pos));
    } else if (holder instanceof MyRVAdapter2.MyIVHolder) {
        ((MyRVAdapter2.MyIVHolder) holder).mImageView.setImageDrawable(UIUtil.getDrawable(R.mipmap.ic_launcher));
    }

    // 点击监听
    ...
}
复制代码

顺带的, 我们把之前放宽的条目变成不同的视图, 也就是对应起来:

@Override
public int getItemViewType(int position) {
    if (position == 0
            || position == (getItemCount() - 1) / 2
            || position == (getItemCount() - 1)) {
        return ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal();
    } else {
        return ITEM_TYPE.ITEM_TYPE_TEXT.ordinal();
    }
}
复制代码

看看效果:

它还能继续地复杂, 试试瀑布流StaggeredGridLayoutManager:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/eight_dp"
    card_view:cardCornerRadius="@dimen/four_dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/iv_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/tv_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/tmp"
            android:textSize="@dimen/thirty_sp" />
    </LinearLayout>
</android.support.v7.widget.CardView>
复制代码
StaggeredGridLayoutManager staggeredGridLayoutManager
        = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
mRvMain.setLayoutManager(staggeredGridLayoutManager);
复制代码

分割线又崩了, 嘿嘿, 其实用上了CardView, 分割线没什么必要再用了.


增删条目

现在适配器中添加增删方法:

public void addData(int position) {
    mData.add(position, "hello x");
    notifyItemInserted(position);
}

public void removeData(int position) {
    mData.remove(position);
    notifyItemRemoved(position);
}
复制代码

再写入点击事件中, 点击增加, 长按删除:

mAdapter.setOnItemClickListener(new MyRVAdapter2.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        mAdapter.addData(position);
    }

    @Override
    public void onItemLongClick(View view, int position) {
        mAdapter.removeData(position);
    }
});
复制代码

增删条目开源库

这里再上一个开源库recyclerview-animators, 可以修改增删动画, 种类也很丰富, 还能在它基础上自定义:

分类动画类名
CoolLandingAnimator
ScaleScaleInAnimator, ScaleInTopAnimator, ScaleInBottomAnimator, ScaleInLeftAnimator, ScaleInRightAnimator
FadeFadeInAnimator, FadeInDownAnimator, FadeInUpAnimator, FadeInLeftAnimator, FadeInRightAnimator
FlipFlipInTopXAnimator, FlipInBottomXAnimator, FlipInLeftYAnimator, FlipInRightYAnimator
SlideSlideInLeftAnimator, SlideInRightAnimator, OvershootInLeftAnimator, OvershootInRightAnimator, SlideInUpAnimator, SlideInDownAnimator

引入:

implementation 'jp.wasabeef:recyclerview-animators:2.3.0'
复制代码

使用:

mRvMain.setItemAnimator(new SlideInLeftAnimator());
复制代码

这里给大家展示两种效果, 其它的自己尝试吧.

mRvMain.setItemAnimator(new LandingAnimator());
复制代码


快速添加视图

还有像Header, Foot这样的视图, 自己写也还是要费些功夫的, 这里推荐Android大神的库baseAdapter

引入:

implementation 'com.zhy:base-rvadapter:3.0.3'
复制代码

添加头尾视图

HeaderAndFooterWrapper mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter);

TextView t1 = new TextView(this);
t1.setText("Header 1");
t1.setTextSize(30);
TextView t2 = new TextView(this);
t2.setText("Foot 1");
t2.setTextSize(30);

mHeaderAndFooterWrapper.addHeaderView(t1);
mHeaderAndFooterWrapper.addFootView(t2);

mRvMain.setAdapter(mHeaderAndFooterWrapper);
复制代码

添加更多视图

LoadMoreWrapper mLoadMoreWrapper = new LoadMoreWrapper(mAdapter);
mLoadMoreWrapper.setLoadMoreView(R.layout.rv_cv_img_txt_item);
mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
    @Override
    public void onLoadMoreRequested() {
    }
});

mRvMain.setAdapter(mLoadMoreWrapper);
复制代码

是不是感觉特别爽, 那看看更爽的, 在不写适配器的情况下快速添加条目:

final ArrayList<String> mData = new ArrayList<>();
for (int i = 0; i < 40; i++) {
    mData.add("hello " + i);
}

mRvMain.setAdapter(new CommonAdapter<String>(this, R.layout.rv_cv_txt_item, mData) {
    @Override
    protected void convert(ViewHolder holder, String s, int position) {
        holder.setText(R.id.tv_txt, mData.get(position));

    }
});
复制代码

是不是感觉省了一万个小时呢.


让RecyclerView支持复杂视图

每次加入新的视图都要对适配器进行比较大程度的改动, 这样是很容易出错的. 这里引入一个非常棒的开源库-AdapterDelegates, 降低下代码耦合性.

引入:

implementation 'com.hannesdorfmann:adapterdelegates3:3.0.1'
复制代码

先不说使用细节, 来看看实现后想加入不同视图有多简单吧:

ArrayList<Base> data = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    data.add(new B("b " + i));
}
for (int i = 0; i < 10; i++) {
    data.add(new A("a " + i));
}
BaseAdapter animalAdapter = new BaseAdapter(this, data);
mRvMain.setAdapter(animalAdapter);
复制代码

是不是惊了, 也就是说, 你只要实现了A, B这些视图类, 直接新建放入数组就完事了.

需要写基础适配器:

public class BaseAdapter extends RecyclerView.Adapter {

    private AdapterDelegatesManager<List<Base>> delegatesManager;
    private List<Base> items;

    public BaseAdapter(Activity activity, List<Base> items) {
        this.items = items;

        delegatesManager = new AdapterDelegatesManager<>();

        delegatesManager.addDelegate(new AAdapterDelegate(activity))
                .addDelegate(new BAdapterDelegate(activity));
    }

    @Override
    public int getItemViewType(int position) {
        return delegatesManager.getItemViewType(items, position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return delegatesManager.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        delegatesManager.onBindViewHolder(items, position, holder);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }
}
复制代码

需要对每个类进行进行具体设置, 这里以A为例.

public class AAdapterDelegate extends AdapterDelegate<List<Base>> {

    private LayoutInflater inflater;

    public AAdapterDelegate(Activity activity) {
        inflater = activity.getLayoutInflater();
    }

    @Override
    public boolean isForViewType(@NonNull List<Base> items, int position) {
        return items.get(position) instanceof A;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        return new CatViewHolder(inflater.inflate(R.layout.rv_cv_img_txt_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull List<Base> items, int position,
                                 @NonNull RecyclerView.ViewHolder holder, @Nullable List<Object> payloads) {

        CatViewHolder vh = (CatViewHolder) holder;
        A cat = (A) items.get(position);

        vh.name.setText(cat.getName());
    }

    static class CatViewHolder extends RecyclerView.ViewHolder {

        public TextView name;
        public ImageView img;

        public CatViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.tv_txt);
            img = (ImageView) itemView.findViewById(R.id.iv_img);
        }
    }
}
复制代码

最后

看完这篇应该是对RecyclerView有个大体认识了, 多练习练习就会得心应手起来了. 那还是有一点, 就像分隔线库的几次不理想表现, 具体项目要求还是要具体对待, 开源库也不是万能的. 最近不是又有什么开源项目套壳事件了嘛, 别人一开源就说自己有自主产权了真的好吗? 喜欢记得点赞或者关注我哦, 有意见或者建议评论区见~


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一种优雅的方式来使用RecyclerView核心思想  想必大家都遇到过,在一个列表中显示不同样式的需求。在RecyclerView中可以通过ViewType进行区分,如果样式特别多的时候就会使得代码非常冗余,不利于开发及维护。那么有没有一种优雅的方法解决这个问题呢?  技术经理给你说,接下来的项目由你负责,明天下班前把排期同步出来。这时你应该怎么做?由于你是Android端的RD,你对Android的排期是比较了解的,但是iOS端、FE端、Server端的排期怎么办呢?聪明的你立即把任务派发下去了,给每个端的负责人说,明天中午之前把排期汇总给我。  没错,在多样式列表的设计中也可以采用这种策略,给RecyclerView设置的Adapter不做具体的处理,而是由它派发出去。实现方案  1. addDelegate 向Adapter中注册委托Adapter;  2. addDataList 设置数据;  3. layout 渲染布局,Adapter查找到对应的委托Adapter,由委托Adapter去做具体渲染。如何使用引入compile 'com.kevin:delegationadapter:1.0.2' // 扩展库,使得databinding更简易,可以不引入 compile 'com.kevin:delegationadapter-extras:1.0.0'用法 1. 同一数据类型多种样式// 设置LayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); // 设置Adapter delegationAdapter = new DelegationAdapter(); // 添加委托Adapter delegationAdapter.addDelegate(new OnePicAdapterDelegate()); delegationAdapter.addDelegate(new ThreePicAdapterDelegate()); delegationAdapter.addDelegate(new MorePicAdapterDelegate()); delegationAdapter.addDelegate(new VideoAdapterDelegate()); recyclerView.setAdapter(delegationAdapter);2. 不同数据类型多种样式// 设置LayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(layoutManager); // 设置Adapter mDelegationAdapter = new DelegationAdapter(); mDelegationAdapter.addDelegate(new StringAdapterDelegate()); mDelegationAdapter.addDelegate(new IntegerAdapterDelegate()); mDelegationAdapter.addDelegate(new FloatAdapterDelegate()); mDelegationAdapter.addDelegate(new DoubleAdapterDelegate()); // 添加委托Adapter mRecyclerView.setAdapter(mDelegationAdapter); // 设置数据 List<Object> dataList = new ArrayList<>(); dataList.add("今天天气怎么样?");  // 添加一个String类型数据 dataList.add("大晴天,有点热。");  // 添加一个String类型数据 dataList.add("温度多少度呢?");    // 添加一个String类型数据 dataList.add(29);                // 添加一个int类型数据 dataList.add("具体是多少?");      // 添加一个String类型数据 dataList.add(29.5F);             // 添加一个Float类型数据 dataList.add(29.527921364978D);  // 添加一个Double类型数据 mDelegationAdapter.setDataItems(dataList); 3. 同一数据多种类型// 设置LayoutManager mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this)); // 设置LayoutManager mDelegationAdapter = new DelegationAdapter(); // 添加委托Adapter mDelegationAdapter.addDelegate(new ServiceInfoAdapterDelegate()); mDelegationAdapter.addDelegate(new BillItemAdapterDelegate()); mDelegationAdapter.addDelegate(new ChargeInfoAdapterDelegate()); mBinding.recyclerView.setAdapter(mDelegationAdapter); // 设置数据 String newsListStr = LocalFileUtils.getStringFormAsset(this, "bill.json"); Bill bill = new Gson().fromJson(newsListStr, Bill.class); List<Object> dataList = new ArrayList<>(); dataList.add(new ItemData(bill, ServiceInfoDelegateAdapter.TAG)); dataList.addAll(bill.details); dataList.add(new ItemData(bill, ChargeInfoDelegateAdapter.TAG)); mDelegationAdapter.setDataItems(dataList);4. 添加头部// 设置LayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(layoutManager); // 设置Adapter mDelegationAdapter = new DelegationAdapter(); // 添加委托Adapter mDelegationAdapter.addDelegate(new TextAdapterDelegate()); mDelegationAdapter.addDelegate(new BannerAdapterDelegate()); mRecyclerView.setAdapter(mDelegationAdapter); // 添加头部 mDelegationAdapter.addHeaderItem("这是添加的添加的头部数据"); 5. 添加尾部// 设置LayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(layoutManager); // 设置Adapter mDelegationAdapter = new DelegationAdapter(); // 添加委托Adapter mDelegationAdapter.addDelegate(new TextAdapterDelegate()); mDelegationAdapter.addDelegate(new BannerAdapterDelegate()); mRecyclerView.setAdapter(mDelegationAdapter); // 添加尾部 mDelegationAdapter.addFotterItem("这是添加的添加的尾部数据"); 6. 带兜底的委托Adapter// 设置LayoutManager LinearLayoutManager layoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(layoutManager); // 设置Adapter mDelegationAdapter = new DelegationAdapter(); // 添加委托Adapter mDelegationAdapter.addDelegate(new TextAdapterDelegate()); // 添加兜底的委托Adapter mDelegationAdapter.setFallbackDelegate(new FallbackAdapterDelegate()); mRecyclerView.setAdapter(mDelegationAdapter);THANKS TO 1. MultiItem 委托思想来源 2. AdapterDelegates 委托架子来源
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值