androdistudio之mvvm初级adapter详解

上代码吧!

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{}中就行


用法详解:

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();
}



这个方法再xml布局中调用的方式是:

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。

先写到这,回头再补/











  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值