Android开发万能适配器介绍(简化操作)

释放双眼,带上耳机,听听看~!

先上效果图

第一步引依赖:

/** 万能RecyclerView的数据适配器  **/
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'

 第二步布局(主界面):

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_myRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:nestedScrollingEnabled="false" />

 第三步布局(适配器子项布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/cs_F6F6F6"
android:gravity="center"
android:orientation="vertical">
<!--消息提醒适配器-->
<LinearLayout
android:id="@+id/ll_message_reminder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/ds_12dp"
android:layout_marginRight="@dimen/ds_12dp"
android:layout_marginTop="@dimen/ds_12dp"
android:background="@drawable/bg_white_round_small"
android:orientation="vertical"
tools:ignore="UselessParent">
<!--上-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/ds_10dp"
android:layout_marginLeft="@dimen/ds_16dp"
android:layout_marginRight="@dimen/ds_16dp"
android:layout_marginTop="@dimen/ds_10dp"
tools:ignore="RtlHardcoded">
<ImageView
android:id="@+id/iv_apply_type_icon_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
tools:ignore="ContentDescription" />
<!--申请类型-->
<TextView
android:id="@+id/tv_notice_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv_apply_type_icon_no"
android:drawableLeft="@drawable/icon_xiaoxi_xiaoxitixing"
android:gravity="center_vertical"
android:text="通知类型"
android:textColor="@color/cs_333333"
android:textSize="@dimen/ds_14sp"
tools:ignore="HardcodedText" />
<!--申请状态-->
<TextView
android:id="@+id/tv_read_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="读取状态"
android:textColor="@color/cs_cccccc"
android:textSize="@dimen/ds_12sp"
tools:ignore="HardcodedText" />
</RelativeLayout>
<!--线-->
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/ds_1dp"
android:layout_marginLeft="@dimen/ds_10dp"
android:layout_marginRight="@dimen/ds_10dp"
android:background="@color/cs_line_F1F1F1" />
<!--中-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/ds_16dp"
android:layout_marginRight="@dimen/ds_16dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/ds_10dp"
android:layout_marginTop="@dimen/ds_10dp"
android:text="暂无内容"
android:textColor="@color/cs_666666"
android:textSize="@dimen/ds_13sp"
tools:ignore="HardcodedText" />
</LinearLayout>
<!--下-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/ds_14dp"
android:layout_marginLeft="@dimen/ds_16dp"
android:layout_marginRight="@dimen/ds_16dp"
android:layout_marginTop="@dimen/ds_14dp">
<!--申请日期-->
<TextView
android:id="@+id/tv_date_of_application"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="暂无日期"
android:textColor="@color/cs_999999"
android:textSize="@dimen/ds_12sp"
tools:ignore="HardcodedText" />
<!--根据需求及功能设计隐藏-->
<TextView
android:id="@+id/tv_btn_look_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/btn_f98746_and_white_round"
android:paddingBottom="@dimen/ds_4dp"
android:paddingLeft="@dimen/ds_15dp"
android:paddingRight="@dimen/ds_15dp"
android:paddingTop="@dimen/ds_4dp"
android:text="查看详情"
android:textColor="@color/cs_f98746"
android:textSize="@dimen/ds_12sp"
android:visibility="gone"
tools:ignore="HardcodedText,RtlHardcoded" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>

第四步主界面逻辑:

rvMyRecycler=(RecyclerView) tvTittle.findViewById(R.id.rv_myRecycler);
rvMyRecycler.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
//禁止嵌套滑动
rvMyRecycler.setNestedScrollingEnabled(false);
messageReminderAdapter = new MessageReminderAdapter(R.layout.item_message_reminder, dataBeanList);
rvMyRecycler.setAdapter(messageReminderAdapter);

在网络请求成功的时候

dataBeanList.clear();
dataBeanList.addAll(showApplyBean.getData());
messageReminderAdapter.notifyDataSetChanged();

第五步实体类:

package com.yc.stscf.model;
import java.io.Serializable;
import java.util.List;
/**
* ================================================
*
* @author :Vip
* @version :V 1.0.0
* @date :2019/7/8 11:55
* 描    述:消息提醒实体
* 修订历史:
* ================================================
*/
public class MessageReminderBean implements Serializable {
/**
* data : [{"contents":"string","id":"string","insDate":"string","msgType":"string","openFlg":"string","subject":"string"}]
* message : string
* recordsFiltered : string
* recordsTotal : string
* status : string
*/
private String message;
private String recordsFiltered;
private String recordsTotal;
private String status;
private List<DataBean> data;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getRecordsFiltered() {
return recordsFiltered;
}
public void setRecordsFiltered(String recordsFiltered) {
this.recordsFiltered = recordsFiltered;
}
public String getRecordsTotal() {
return recordsTotal;
}
public void setRecordsTotal(String recordsTotal) {
this.recordsTotal = recordsTotal;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* contents : string  消息内容
* id : string  唯一标识
* insDate : string  消息时间
* msgType : string  消息类型
* openFlg : string  读取状态0:未开封 1开封
* subject : string  消息主题
*/
private String contents;
private String id;
private String insDate;
private String msgType;
private String openFlg;
private String subject;
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getInsDate() {
return insDate;
}
public void setInsDate(String insDate) {
this.insDate = insDate;
}
public String getMsgType() {
return msgType;
}
public void setMsgType(String msgType) {
this.msgType = msgType;
}
public String getOpenFlg() {
return openFlg;
}
public void setOpenFlg(String openFlg) {
this.openFlg = openFlg;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
}

第六部适配器(核心)

/**
* ================================================
*
* @author :Vip
* @version :V 1.0.0
* @date :2019/7/8 12:02
* 描    述:
* 修订历史:消息提醒适配器
* ================================================
*/
public class MessageReminderAdapter extends BaseQuickAdapter<MessageReminderBean.DataBean, BaseViewHolder> {
public MessageReminderAdapter(int itemRecycler, List<MessageReminderBean.DataBean> mList) {
super(itemRecycler, mList);
}
@Override
protected void convert(BaseViewHolder helper, MessageReminderBean.DataBean item) {
//读取状态----0.未开封,1.开封
switch (StringUtils.NullToStr(item.getOpenFlg())) {
case "0":
helper.setText(R.id.tv_read_type, "未读").setTextColor(R.id.tv_read_type, mContext.getResources().getColor(R.color.cs_cccccc));
break;
case "1":
helper.setText(R.id.tv_read_type, "已读").setTextColor(R.id.tv_read_type, mContext.getResources().getColor(R.color.cs_FB8C4C));
break;
default:
helper.setText(R.id.tv_read_type, "未读").setTextColor(R.id.tv_read_type, mContext.getResources().getColor(R.color.cs_cccccc));
break;
}
//消息类型
helper.setText(R.id.tv_notice_type, StringUtils.NullToStr(item.getSubject()));
//消息内容
helper.setText(R.id.tv_content, StringUtils.NullToStr(item.getContents()));
//消息时间
helper.setText(R.id.tv_date_of_application, StringUtils.NullToStr(item.getInsDate()));
}
}

人已赞赏
Android文章

Android studio butterknife 8.4.0环境配置

2020-3-10 12:54:12

Android文章

Android开发运行出现Process finished with exit code 1

2020-3-10 15:58:15

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索