上代码吧!
public abstract class BaseRecyclerViewHolder<T> extends RecyclerView.ViewHolder { public BaseRecyclerViewHolder(View itemView) { super(itemView); } public abstract void bindData(T data); }recyclerView的用法已经很普遍,但是adapter,有很多中写法,欸用那种用哪种。
最近搭档说想用mvvm实现列表,所以有了这一篇初级讲解mvvm中的列表adapter的写法。
1 上面的代码,事viewHodler的基类,用来绑定数据的,不用懂先复制。
2 创建adapter类
public class CallHistoryAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder<CallHistory>> { private List<CallHistory> historyList = new ArrayList<>(); private WaringDialog waringDialog; private Context context; public void addCallHistory(CallHistory history) { historyList.add(history); } public void addCallHistory(Collection<CallHistory> histories) { if (histories == null) return; historyList.addAll(histories); } public void clear() { historyList.clear(); } @Override public BaseRecyclerViewHolder<CallHistory> onCreateViewHolder(ViewGroup parent, int viewType) { if (context == null) context = parent.getContext(); ItemCallHistoryBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_call_history, parent, false); return new HistoryHolder(binding); } @Override public void onBindViewHolder(BaseRecyclerViewHolder<CallHistory> holder, int position) { holder.bindData(historyList.get(position)); } @Override public int getItemCount() { return historyList.size(); } class HistoryHolder extends BaseRecyclerViewHolder<CallHistory> { private ItemCallHistoryBinding binding; public HistoryHolder(ItemCallHistoryBinding binding) { super(binding.getRoot()); this.binding = binding; } @Override public void bindData(CallHistory data) { binding.setHistory(data); } } /** * 点击item */ public void onHistoryClick(CallHistory history) { if (waringDialog == null) { waringDialog = new WaringDialog(context); waringDialog.setOnAlterClickListener(new OnAlterClickListener() { @Override public void onCancelClick() { } @Override public void onConfirmClick() { CallHistory callHistory = (CallHistory) waringDialog.getTag(); if (callHistory != null) { phone = callHistory.getCallNumber(); callName = callHistory.getCallName(); ApiByHttp.getInstance().callPhone(phone, callPhoneCallback); } } }); } waringDialog.setTitleText("是否呼叫 " + history.getCallName() + " ?\n" + history.getCallNumber()); waringDialog.setTag(history); waringDialog.show(); } private String phone; private String callName; private JsonCallback callPhoneCallback = new JsonCallback() { @Override public void onSucceed(int what, JSONObject response) throws Exception { if (response.getInt("result") == 200) { Intent intent = new Intent(context, CallingActivity.class); intent.putExtra("name", callName); intent.putExtra("phone", phone); context.startActivity(intent); } else { ToastUtil.showToast(response.getString("msg")); } } }; }
单个条目的xml文件:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="callHistoryAdapter" type="com.omi.ui.adapter.history.CallHistoryAdapter"/> <variable name="history" type="com.omi.bean.call.CallHistory"/> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:onClick="@{()->callHistoryAdapter.onHistoryClick(history)}" android:orientation="vertical" android:paddingLeft="13dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="@{history.callName??history.callNumber}" android:textColor="@color/text_color" android:textSize="16sp" android:textStyle="bold"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:text="@{history.callTime}" android:textColor="@color/text_color" android:textSize="14sp"/> <View style="@style/lineStyle" android:background="@color/group_head_dark"/> </LinearLayout> </layout>
对了,如果要使用这个方法,你需要用到databinding,引用的方法百度下。
dataBinding {
enabled true
}
放app.build.gradle
Android{}中就行
用法详解:
1
public class IndexAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder<UserInfo>> {}
viewHolder规定了范围,通过继承BaseRecyclerViewHodler来绑定条目显示的数据。
2 看看继承了BaseRecyclerViewHolder的viewHodler的写法:
class HistoryHolder extends BaseRecyclerViewHolder<CallHistory> { private ItemCallHistoryBinding binding; public HistoryHolder(ItemCallHistoryBinding binding) { super(binding.getRoot()); this.binding = binding; } @Override public void bindData(CallHistory data) { binding.setHistory(data); } }
ItemCallHistoryBinding 是item的xml布局对象
item的布局中有这么几行代码:
<variable name="history" type="com.omi.bean.call.CallHistory"/>所以,为了让history有意义,需要绑定callhistory,于是有了BindData这个方法。
3再adapter中有这个方法:
public void onHistoryClick(CallHistory history) { if (waringDialog == null) { waringDialog = new WaringDialog(context); waringDialog.setOnAlterClickListener(new OnAlterClickListener() { @Override public void onCancelClick() { } @Override public void onConfirmClick() { CallHistory callHistory = (CallHistory) waringDialog.getTag(); if (callHistory != null) { phone = callHistory.getCallNumber(); callName = callHistory.getCallName(); ApiByHttp.getInstance().callPhone(phone, callPhoneCallback); } } }); } waringDialog.setTitleText("是否呼叫 " + history.getCallName() + " ?\n" + history.getCallNumber()); waringDialog.setTag(history); waringDialog.show(); }
android:onClick="@{()->callHistoryAdapter.onHistoryClick(history)}"因为再xml中声明过callHistoryAdapter,
<variable name="callHistoryAdapter" type="com.omi.ui.adapter.history.CallHistoryAdapter"/>
所以这行代码的意思是: CallHistoryAdapter中的onHistoryClick方法,里面的参数是history
而history 也在xml文件中声明过:
<variable name="history" type="com.omi.bean.call.CallHistory"/>
所以那行代码的意思就是,CallHistoryAdapter中的onHistoryClick方法,里面的参数类型是CallHistory。
先写到这,回头再补/