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


    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

土狗的想法

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值