欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

每天记录学习的新知识 :鸿洋的RecyclerView BaseAdapter

程序员文章站 2023-02-02 08:19:24
首先学习使用大神封装的BaseAdapter,希望自己可以坚持把源码研究明白,感谢大神。...

参考地址

Github地址:https://github.com/hongyangAndroid/baseAdapter

Android 万能的Adapter for ListView,RecyclerView,GridView等,支持多种Item类型的情况。

使用

    implementation 'com.zhy:base-rvadapter:3.0.3'

展示普通列表

使用方式

mRecyclerView.setAdapter(new CommonAdapter<String>(this, R.layout.item_list, mDatas)
{
    @Override
    public void convert(ViewHolder holder, String s)
    {
        holder.setText(R.id.id_item_list_title, s);
    }
});

每天记录学习的新知识 :鸿洋的RecyclerView BaseAdapter
    class MainAdapter extends CommonAdapter<RcBean> {

        public MainAdapter(Context context, int layoutId, List<RcBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, RcBean rcBean, int position) {
            holder.setText(R.id.id_item_list_title, rcBean.getTitle());
            holder.setText(R.id.id_item_list_msg, rcBean.getMsg());
        }
    }
recyclerView.setAdapter(new MainAdapter(this, R.layout.item_list, rcBeanList));

除此,Adapter提供Item点击的事件

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

具体到某一个控件的点击,需要自己在 @Override protected void convert 内监听。

展示多种类型的Item列表

使用方法

MultiItemTypeAdapter adapter = new MultiItemTypeAdapter(this,mDatas);
adapter.addItemViewDelegate(new MsgSendItemDelagate());
adapter.addItemViewDelegate(new MsgComingItemDelagate());
public class MsgComingItemDelagate implements ItemViewDelegate<ChatMessage>
{

    @Override
    public int getItemViewLayoutId()
    {
        return R.layout.main_chat_from_msg;
    }

    @Override
    public boolean isForViewType(ChatMessage item, int position)
    {
        return item.isComMeg();
    }

    @Override
    public void convert(ViewHolder holder, ChatMessage chatMessage, int position)
    {
        holder.setText(R.id.chat_from_content, chatMessage.getContent());
        holder.setText(R.id.chat_from_name, chatMessage.getName());
        holder.setImageResource(R.id.chat_from_icon, chatMessage.getIcon());
    }
}

每天记录学习的新知识 :鸿洋的RecyclerView BaseAdapter
        MultiItemTypeAdapter adapter = new MultiItemTypeAdapter(this, rcBeanList);
        adapter.addItemViewDelegate(new MsgSendItemDelagate());
        adapter.addItemViewDelegate(new MsgComingItemDelagate());
        recyclerView.setAdapter(adapter);
    public class MsgComingItemDelagate implements ItemViewDelegate<RcBean> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_list;
        }

        @Override
        public boolean isForViewType(RcBean item, int position) {
            return item.isComMeg();
        }

        @Override
        public void convert(ViewHolder holder, RcBean rcBean, int position) {
            holder.setText(R.id.id_item_list_title, rcBean.getTitle());
            holder.setText(R.id.id_item_list_msg, rcBean.getMsg());
        }
    }

    public class MsgSendItemDelagate implements ItemViewDelegate<RcBean> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_list_send_msg;
        }

        @Override
        public boolean isForViewType(RcBean item, int position) {
            return !item.isComMeg();
        }

        @Override
        public void convert(ViewHolder holder, RcBean rcBean, int position) {
            holder.setText(R.id.tv_text, rcBean.getDesc());
        }
    }

其中,@Override public boolean isForViewType用于标识,使用哪种Delegate。

    public int getItemViewType(T item, int position)
    {
        int delegatesCount = delegates.size();
        for (int i = delegatesCount - 1; i >= 0; i--)
        {
            ItemViewDelegate<T> delegate = delegates.valueAt(i);
            if (delegate.isForViewType( item, position))
            {
                return delegates.keyAt(i);
            }
        }
        throw new IllegalArgumentException(
                "No ItemViewDelegate added that matches position=" + position + " in data source");
    }

通过@Override public boolean isForViewType的调用,Adapter就可以知道使用哪种布局。

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewDelegate itemViewDelegate = mItemViewDelegateManager.getItemViewDelegate(viewType);//here
        int layoutId = itemViewDelegate.getItemViewLayoutId();
        ViewHolder holder = ViewHolder.createViewHolder(mContext, parent, layoutId);
        onViewHolderCreated(holder,holder.getConvertView());
        setListener(parent, holder, viewType);
        return holder;
    }

ItemViewDelegate 作用是将布局抽出,在使用的地方现取。

添加HeaderView、FooterView

如果你的需求,需要你添加一键已读的功能?这个需求对应的就是HeaderView。

使用方法

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

mRecyclerView.setAdapter(mLoadMoreWrapper);

每天记录学习的新知识 :鸿洋的RecyclerView BaseAdapter
        HeaderAndFooterWrapper mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(adapter);

        TextView t1 = new TextView(this);
        t1.setText("Header 1");
        TextView t2 = new TextView(this);
        t2.setText("Header 2");
        mHeaderAndFooterWrapper.addHeaderView(t1);
        mHeaderAndFooterWrapper.addHeaderView(t2);

        recyclerView.setAdapter(mHeaderAndFooterWrapper);
        mHeaderAndFooterWrapper.notifyDataSetChanged();

添加LoadMore

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

mRecyclerView.setAdapter(mLoadMoreWrapper);

添加EmptyView

空页面

mAdapter.getDatas().clear();//清除数据,才会显示空页面哦
mEmptyWrapper = new EmptyWrapper(mAdapter);//HEADER FOOTER本身就会清理
mEmptyWrapper.setEmptyView(R.layout.empty_view);

mRecyclerView.setAdapter(mEmptyWrapper);

另一个用法不知道

        EmptyViewWrapper  mAdapter = new EmptyViewWrapper(
                new LoadMoreWrapper(
                        new HeaderAndFooterWrapper(mOriginAdapter)));

回调

onViewHolderCreated

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewDelegate itemViewDelegate = mItemViewDelegateManager.getItemViewDelegate(viewType);
        int layoutId = itemViewDelegate.getItemViewLayoutId();
        ViewHolder holder = ViewHolder.createViewHolder(mContext, parent, layoutId);
        onViewHolderCreated(holder,holder.getConvertView());
        setListener(parent, holder, viewType);
        return holder;
    }

    public void onViewHolderCreated(ViewHolder holder,View itemView){

    }

如上,是封装的源代码。onViewHolderCreated方法是提供给外面,用于布局操作。

如下,例:

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {
        ViewHolder holder = super.onCreateViewHolder(viewGroup, type);
        
        holder.setLayoutParams(R.id.notice_content_layout, 
        	new LinearLayout.LayoutParams((
        		ScreenUtils.getScreenWidth(getContext()) - dp2px(40)), 
        		LinearLayout.LayoutParams.MATCH_PARENT));
        		
        return holder;
    }

当需要对某一控件设置固定长度时。

本文地址:https://blog.csdn.net/weixin_35691921/article/details/107353324

相关标签: GitHub 开源项目