安卓recyclerView,出来很久了,之前写了个公共adapter,希望能帮助到别人,大神勿喷

首先我是基于
xRefreshView和databinding
compile 'com.huxq17.xrefreshview:xrefreshview:3.5.6'
dataBinding {
    enabled = true
}

如果不是用studio开发,那我也没办法了,早忘了那个怎么用
第一步 先写个接口
public interface BindViewInterface {
    void bandView(ViewDataBinding binding, int position);
}

然后是PublicViewHeader 
public  class PublicViewHeader extends RecyclerView.ViewHolder {
    private ViewDataBinding viewDataBinding;

    public PublicViewHeader(View v) {
        super(v);
    }


    public ViewDataBinding getBinding() {
        return viewDataBinding;
    }

    public void setBinding(ViewDataBinding viewDataBinding) {
        this.viewDataBinding = viewDataBinding;
    }
}
然后就是adpater
public class PublicAdapter<T> extends BaseRecyclerAdapter<PublicViewHeader> {

    private BindViewInterface bindViewInterface;
    private int layoutId;
    private List<T> mDatas;
    private int size;
    private ViewDataBinding binding;
    private boolean isS = false;

    public PublicAdapter(List<T> mData, int layoutId, BindViewInterface bindViewInterface) {
        this.mDatas = mData;
        this.layoutId = layoutId;
        this.bindViewInterface = bindViewInterface;
        isS = false;
    }

    public PublicAdapter(int size, int layoutId, BindViewInterface bindViewInterface) {
        this.size = size;
        this.layoutId = layoutId;
        this.bindViewInterface = bindViewInterface;
        isS = true;
    }

    @Override
    public View setHeaderView(int id, RecyclerView recyclerView) {
        return super.setHeaderView(id, recyclerView);
    }

    @Override
    public void setHeaderView(View headerView, RecyclerView recyclerView) {
        super.setHeaderView(headerView, recyclerView);
    }


    @Override
    public PublicViewHeader onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        binding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                layoutId,
                parent,
                false);

        PublicViewHeader holder = new PublicViewHeader(binding.getRoot());
        holder.setBinding(binding);
        return holder;
    }

    @Override
    public PublicViewHeader getViewHolder(View view) {
        return new PublicViewHeader(view);
    }


    @Override
    public void onBindViewHolder(PublicViewHeader holder, int position, boolean ds) {
        bindViewInterface.bandView(holder.getBinding(), position);
    }

    @Override
    public int getAdapterItemCount() {
        if (isS) {
            return size;
        } else {
            return mDatas.size();
        }

    }

}
本来到此就结束了,哈哈 还有哦,之后我每次都要设置
LayoutManager,就有写了个
RViewUtils

public class RViewUtils {
    private RecyclerView r;
    private Context context;
    private XRefreshView x;
    private PublicAdapter adapter;
    private ListLayoutBinding binding;


    public RViewUtils(Context context, RecyclerView r, XRefreshView x) {
        this.r = r;
        this.x = x;
        this.context = context;
    }

    public RViewUtils(Context context, ListLayoutBinding binding) {
        this.context = context;
        this.binding = binding;
        this.r = binding.recyclerView;
        this.x = binding.xRefreshView;
    }

    public RViewUtils setLayout(int spanCount, int orientation) {
        r.setLayoutManager(new StaggeredGridLayoutManager(spanCount, orientation));
        return this;
    }

    public RViewUtils setLayoutMg(RecyclerView.LayoutManager layoutMg) {
        r.setLayoutManager(layoutMg);
        return this;
    }


    public RViewUtils setAdapter(PublicAdapter adapters) {
        if (r.getLayoutManager() == null) {
            r.setLayoutManager(new StaggeredGridLayoutManager(1, LinearLayout.VERTICAL));
        }
        adapter = adapters;
        r.setAdapter(adapter);
        if (x != null) {
            x.setPullLoadEnable(false);
            x.setPullRefreshEnable(false);
            x.setMoveForHorizontal(false);
            x.setAutoLoadMore(false);
        }
        r.setHasFixedSize(false);
        return this;
    }

    public RViewUtils IsRefreshOrLoad(boolean IsRefresh) {
        x.stopRefresh();
        x.stopLoadMore();
        x.setPullRefreshEnable(IsRefresh);
        x.setPullLoadEnable(IsRefresh);
        if (IsRefresh) {
            x.setPinnedTime(100);
            if (adapter != null)
                adapter.setCustomLoadMoreView(new XRefreshViewFooter(context));
        }
        return this;
    }
}

最后看看一看用法吧

new RViewUtils(mContext, binding.list)
        .setAdapter(new PublicAdapter<>(mode.getRows(), R.layout.order_list_item, new BindViewInterface() {
            @Override
            public void bandView(ViewDataBinding bb, int position) {
               OrderOrderListItemBinding bin=(OrderOrderListItemBinding )bb;
bin.xxx.settext(xxxxx); 好了 就是这样了,哈哈 第一次发,
} }));


对了  下面的是 binding.list  ,R.layout.list_item 不用解释吧,就是你自己的item ,
mode.getRows() 就是你的list 数据,移动要是list哦
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/root_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/title"
            layout="@layout/app_title" />

        <include
            android:id="@+id/list"
            layout="@layout/list_layout" />
    </LinearLayout>
</layout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值