首先我是基于
xRefreshView和databinding
compile 'com.huxq17.xrefreshview:xrefreshview:3.5.6'
如果不是用studio开发,那我也没办法了,早忘了那个怎么用dataBinding { enabled = true }
第一步 先写个接口
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; } }然后就是adpaterpublic 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>