Android

Android 封装列表模板-使得快速开发一个列表

2020-01-03  本文已影响0人  卢融霜

目录

1. baseLayout 将公共布局提取

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <!--公共头部-->
    <include layout="@layout/include_toolbar_layout_text" />
    <include layout="@layout/include_search_toolbar_layout" />
  <!--刷新组件-->
    <com.scwang.smart.refresh.layout.SmartRefreshLayout
            android:id="@+id/refreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/dp_42">
  <!--刷新头部-->
        <com.scwang.smart.refresh.header.MaterialHeader
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        <!--状态组件-->
        <com.xuexiang.xui.widget.statelayout.StatefulLayout
                android:id="@+id/ll_stateful"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:stf_animationEnabled="false"
                app:stf_inAnimation="@android:anim/slide_in_left"
                app:stf_outAnimation="@android:anim/slide_out_right">
            <!--列表-->
            <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:background="@color/listBgCol"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
        </com.xuexiang.xui.widget.statelayout.StatefulLayout>

          <!--刷新尾部-->
        <com.scwang.smart.refresh.footer.ClassicsFooter
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/split_line" />
    </com.scwang.smart.refresh.layout.SmartRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

2. 编写 ListBaseActivity

package com.hyrc99.projectByL.baseAll;


import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.hyrc99.projectByL.HYRCProject_Smaple.R;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.xuexiang.xui.widget.statelayout.CustomStateOptions;
import com.xuexiang.xui.widget.statelayout.StatefulLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * create by lrs 列表Activity 父类 继承 使得快速开发一个列表Activity  只去关心你想关心的  剩下交给我
 */
public abstract class ListBaseActivity extends BaseActivity {
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ll_stateful)
    StatefulLayout statefulLayout;
    private BaseAdapter adapter;
    @BindView(R.id.ll_toolbar_layout_search)
    RelativeLayout ll_toolbar_layout_search;
    @BindView(R.id.ll_toolbar_layout)
    LinearLayout ll_toolbar_layout;

    @Override
    protected int loadView() {
        return R.layout.listbase_activity;
    }

    /**
     * 显示搜索头 还是默认头部
     */
    public void isShowSearchTitle(boolean isShowSearch) {
        if (isShowSearch) {
            ll_toolbar_layout_search.setVisibility(View.VISIBLE);
            ll_toolbar_layout.setVisibility(View.GONE);
        } else {
            ll_toolbar_layout_search.setVisibility(View.GONE);
            ll_toolbar_layout.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 配置 上拉 下拉
     */
    public void setRefreshData() {
        Refresh(true);
        LoadMore(true);
    }

    /**
     * 是否启用上拉加载
     */
    public void LoadMore(boolean state) {
        refreshLayout.setEnableLoadMore(state);
    }

    /**
     * 是否启用下拉刷新
     */
    public void Refresh(boolean state) {
        refreshLayout.setEnableRefresh(state);
    }

    /**
     * 是否停止继续上拉
     */
    public void setNomoreData(boolean state) {
        if (refreshLayout != null) {
            refreshLayout.setNoMoreData(state);
        }
    }


    @Override
    protected void initData() {
        isShowSearchTitle(false);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        refreshLayout.setEnableAutoLoadMore(false);
        setRefreshData();
        adapter = initAdapter(adapter);
        recyclerView.setAdapter(adapter);
        loadData(adapter);
        /**
         * 下拉刷新
         */
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                listonRefresh(refreshLayout, recyclerView);
                if (recyclerView != null && recyclerView.getChildCount() > 0) {
                    recyclerView.removeAllViews();
                }
                if (adapter != null && adapter.getItemCount() > 0) {
                    adapter.getData().clear();
                    adapter.notifyDataSetChanged();
                }
                loadData(adapter);
                finishRefresh();
            }
        });
        /**
         * 上拉加载
         */
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                listOnLoadMore(refreshLayout, recyclerView);
                loadData(adapter);
                finishLoadMore();

            }

        });
    }

    /**
     * 刷新完成
     */
    public void finishRefresh() {
        refreshLayout.finishRefresh();
    }

    /**
     * 加载完成
     */
    public void finishLoadMore() {
        refreshLayout.finishLoadMore();
    }

    /**
     * 无更多数据
     */
    public void finishRefreshWithNoMoreData() {
        refreshLayout.finishRefreshWithNoMoreData();
    }

    /**
     * 显示内容
     */
    public void showContent() {
        if (statefulLayout != null) {
            statefulLayout.showContent();
        }
    }

    /**
     * 显示加载中
     *
     * @param message 提示信息
     */
    public void showLoading(String message) {
        if (message != null && statefulLayout != null) {
            statefulLayout.showLoading(message);
        }
    }

    /**
     * 显示加载中
     */
    public void showLoading() {
        if (statefulLayout != null) {
            statefulLayout.showLoading();
        }
    }

    /**
     * 显示暂无数据
     */
    public void showEmpty() {
        if (statefulLayout != null) {
            statefulLayout.showEmpty();
        }
    }

    /**
     * 显示暂无数据
     *
     * @param message 自定义信息
     */
    public void showEmpty(String message) {
        if (message != null && statefulLayout != null) {
            statefulLayout.showEmpty(message);
        }
    }

    /**
     * 显示错误
     *
     * @param clickListener 点击重试按钮
     */
    public void showError(View.OnClickListener clickListener) {
        if (statefulLayout != null && clickListener != null) {
            statefulLayout.showError(clickListener);
        }
    }

    /**
     * 显示错误
     *
     * @param message       自定义提示信息
     * @param clickListener 点击重试按钮
     */
    public void showError(String message, View.OnClickListener clickListener) {
        if (statefulLayout != null && clickListener != null && message != null) {
            statefulLayout.showError(message, clickListener);
        }
    }

    /**
     * 显示错误
     *
     * @param message       自定义提示信息
     * @param buttonText    重试按钮提示信息
     * @param clickListener 点击按钮事件
     */
    public void showError(String message, String buttonText, View.OnClickListener clickListener) {
        if (statefulLayout != null && clickListener != null && message != null && buttonText != null) {
            statefulLayout.showError(message, buttonText, clickListener);
        }
    }

    /**
     * 显示网络离线
     *
     * @param clickListener 点击按钮事件
     */
    public void showOffline(View.OnClickListener clickListener) {
        if (statefulLayout != null && clickListener != null) {
            statefulLayout.showOffline(clickListener);
        }
    }

    /**
     * 显示网络离线
     *
     * @param message       提示消息
     * @param clickListener 点击事件
     */
    public void showOffline(String message, View.OnClickListener clickListener) {
        if (statefulLayout != null && clickListener != null && message != null) {
            statefulLayout.showOffline(message, clickListener);
        }
    }

    /**
     * 显示网络离线
     *
     * @param message       提示消息
     * @param buttonText    按钮显示信息
     * @param clickListener 点击事件
     */
    public void showOffline(String message, String buttonText, View.OnClickListener clickListener) {
        if (statefulLayout != null && clickListener != null && message != null && buttonText != null) {
            statefulLayout.showOffline(message, buttonText, clickListener);
        }
    }

    /**
     * 显示自定义布局
     *
     * @param options 自定义布局
     */
    public void showCustom(final CustomStateOptions options) {
        if (statefulLayout != null && options != null) {
            statefulLayout.showCustom(options);
        }
    }

    @Override
    protected void clearData() {
    }

    /**
     * 下拉刷新
     */
    protected abstract void listonRefresh(RefreshLayout refreshLayout, RecyclerView recyclerView);

    /**
     * 上拉加载
     */
    protected abstract void listOnLoadMore(RefreshLayout refreshLayout, RecyclerView recyclerView);

    /**
     * 初始化adapter
     */
    protected abstract BaseAdapter initAdapter(BaseAdapter adapter);

    /**
     * 加载数据
     */
    protected abstract void loadData(BaseAdapter adapter);
}

如何使用呢!

package com.hyrc99.projectByL.activity.company;

import android.view.View;

import com.hyrc99.projectByL.HYRCProject_Smaple.R;
import com.hyrc99.projectByL.activity.company.adapter.QualificationsAdapter;
import com.hyrc99.projectByL.activity.company.bean.QualificationsBase;
import com.hyrc99.projectByL.baseAll.BaseAdapter;
import com.hyrc99.projectByL.baseAll.ListBaseActivity;
import com.hyrc99.projectByL.utils.http.NetWorkUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;

import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 单位资质信息
 */
public class QualificationsActivity extends ListBaseActivity {
    private int companyId;

    @Override
    protected void listonRefresh(RefreshLayout refreshLayout, RecyclerView recyclerView) {

    }

    @Override
    protected void listOnLoadMore(RefreshLayout refreshLayout, RecyclerView recyclerView) {

    }

    public void setRefreshData() {
        Refresh(true);
        LoadMore(false);
    }

    @Override
    protected BaseAdapter initAdapter(BaseAdapter adapter) {
        setTitle(true, "资质信息");
        adapter = new QualificationsAdapter(R.layout.adapter_qualifications_item, getApplicationContext());
        return adapter;
    }

    @Override
    protected void loadData(BaseAdapter adapter) {
        companyId = getIntent().getExtras().getInt("companyId");
        showLoading();
        NetWorkUtils.getNetwork2().getAptm(companyId).enqueue(new Callback<QualificationsBase>() {
            @Override
            public void onResponse(Call<QualificationsBase> call, Response<QualificationsBase> response) {
                if (response != null && response.body() != null && response.body().getData() != null && response.body().getData().size() > 0) {
                    for (QualificationsBase.DataBean bean : response.body().getData()) {
                        adapter.addData(bean);
                    }
                    showContent();
                } else {
                    showEmpty();
                }
            }

            @Override
            public void onFailure(Call<QualificationsBase> call, Throwable t) {
                showError(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        loadData(adapter);
                    }
                });
            }
        });
    }
}

package com.hyrc99.projectByL.activity.company.adapter;

import android.content.Context;

import com.chad.library.adapter.base.BaseViewHolder;
import com.hyrc99.projectByL.HYRCProject_Smaple.R;
import com.hyrc99.projectByL.activity.company.bean.QualificationsBase;
import com.hyrc99.projectByL.baseAll.BaseAdapter;

/**
 * @description 作用:
 * @date: 2019/11/6
 * @author: 卢融霜
 */
public class QualificationsAdapter extends BaseAdapter<QualificationsBase.DataBean> {
    public QualificationsAdapter(int layoutResId, Context context) {
        super(layoutResId, context);
    }

    @Override
    protected void itemInit(BaseViewHolder helper, QualificationsBase.DataBean item) {
        helper.setText(R.id.tvSpecty, item.getSPECTY());
        helper.setText(R.id.tvGrade, "等级 : " + item.getGRADE());
        helper.setText(R.id.tvApvm, "类别 : " + item.getAPVM());
        helper.setText(R.id.tvCertId, "证书号 : " + item.getCERTID());
        helper.setText(R.id.tvVtime, "有效期 : " + item.getVTIME());
    }
}

上一篇 下一篇

猜你喜欢

热点阅读