Skip to content

XRecyclerViewAdapter详解

王兴春 edited this page Feb 21, 2017 · 4 revisions

一个用于简化RecyclerView Adapter的开发库,包含添加Header,Footer,加载更多,加载失败,加载到底和支持多种布局,分割线工具类。 我们这里封装的adapter为了保证使用者的可定制性,并没有去改动去继承RecyclerView,而是通过改动adapter的方式来实现, 使用者不需要为了本框架没有某个功能而纠结,你完全可以和其他自定义RecyclerView配合使用。

####方法使用

  • 添加分割线
recyclerView.addItemDecoration(new DividerDecoration(Color.parseColor("#C4C4C4"),1));
  • 添加/移除header,footer
final View headerView = LayoutInflater.from(this).inflate(R.layout.header, recyclerView, false);
final View footerView = LayoutInflater.from(this).inflate(R.layout.footer, recyclerView, false);
adapter.addHeaderView(headerView);
adapter.addFooterView(footerView);
adapter.removeFooterView(footerView);
  • 点击事件
//点击事件
adapter.setOnItemClickListener(new XRecyclerViewAdapter.OnItemClickListener() {
   @Override
   public void onItemClick(View v, int position) {
                
   }
});
       
adapter.isLoadMore(true);//开启加载更多功能,默认关闭
//加载更多事件
adapter.setOnLoadMoreListener(new XRecyclerViewAdapter.OnLoadMoreListener() {
   @Override
   public void onRetry() {//加载失败,重新加载回调方法
               
   }

   @Override
   public void onLoadMore() {//加载更多回调方法
                
   }
});

recyclerView.setAdapter(adapter);

####单一布局Adapter使用示例

 class TestAdapter extends XRecyclerViewAdapter<String> {

        public TestAdapter(RecyclerView recyclerView, List<String> datas) {
            super(recyclerView, datas, R.layout.dome_item);
        }

        @Override
        public void bindData(XViewHolder holder, String data, int position) {
            //方法一:
            holder.setText(R.id.text, data);
            //方法二:
            //TextView textView = holder.getView(R.id.text);
            //textView.setText(data);
        }
    }

####多状态布局Adapter使用示例

public class News {
    /** 单图布局样式 */
    public static final int TYPE_SINGLE_PICTURE   = 0;
    /** 无图布局样式 */
    public static final int TYPE_NONE_PICTURE     = 1;

    private int newsType;
    private String title;
    private String imageUrl;
    private String author;
    private String time;
}



 class MultiTypeAdapter extends XRecyclerViewAdapter<News> {

        public MultiTypeAdapter(@NonNull RecyclerView mRecyclerView, List<News> dataLists) {
            super(mRecyclerView, dataLists);
        }

        @Override
        public int getItemLayoutResId(News data, int position) {
            int layoutResId = -1;
            switch (data.getNewsType()){
                case News.TYPE_NONE_PICTURE:
                    layoutResId = R.layout.dome_item;
                    break;
                case News.TYPE_SINGLE_PICTURE:
                    layoutResId = R.layout.dome_image_item;
                    break;
            }
            return layoutResId;
        }

        @Override
        public void bindData(XViewHolder holder, News data, int position) {
            switch (data.getNewsType()){
                case News.TYPE_NONE_PICTURE:
                    holder.setText(R.id.text, data.getTitle());
                    break;
                case News.TYPE_SINGLE_PICTURE:
                    holder.setText(R.id.newTitle, data.getTitle())
                            .setText(R.id.newAuthor,data.getAuthor())
                            .setText(R.id.newTime,data.getTime());
                    ImageView view=holder.getView(R.id.newImage);
                    Glide.with(XFrame.getContext())
                            .load(data.getImageUrl())
                            .crossFade()
                            .transform(new GlideCircleTransform(XFrame.getContext()))
                            .into(view);
                    break;
            }
        }
    }