Android中RecyclerView配合BaseRecyclerViewAdapterHelper实现表格布局(四)

今天介绍了RecyclerView的表格布局,很小的一个功能点,似乎不值得为此写一篇博客,但是想想还是写一篇做个记录。

项目说明:

一,使用的AndroidStudio版本为3.2.1,gradle-4.6

二,使用的RecyclerView的adapter是BaseRecyclerViewAdapterHelper,github上对应的地址如下

github地址为:https://github.com/CymChad/BaseRecyclerViewAdapterHelper
对应的简书说明地址为:https://www.jianshu.com/p/b343fcff51b0

展示效果:

table11.gif

现在正式开始

1,这是RecyclerView的第四篇文章,基本配置如依赖包,下拉刷新功能等在RecyclerView系列(二)中已经说明,不做过多描述。

2,表格布局其实只是看起来是表格,其实还是列表。主activity代码如下,关键是第七步,增加头文件和脚文件。我用红色表示了,看图。

package com.mumu.jsrecyclerview3;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * author : zlf
 * date   : 2019/1/18
 * blog   :https://www.jianshu.com/u/281e9668a5a6
 */
public class MainActivity extends AppCompatActivity {
    @BindView(R.id.rv_table)
    RecyclerView rvTable;
    @BindView(R.id.srl_table)
    SmartRefreshLayout srlTable;
    private TableAdapter mTableAdapter;
    private ArrayList<TableEntity.ResultBean.ListBean> mTableList;
    private Unbinder unbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        unbinder = ButterKnife.bind(this);

        initView();
    }

    private void initView() {

        refreshView();
        getData(2);
        smartRefreshView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    /**
     * 刷新消息列表
     */
    private void refreshView() {
        //1,加载空布局文件,便于第五步适配器在没有数据的时候加载
        View emptyView = View.inflate(this, R.layout.empty_view, null);
        //2,设置LayoutManager,LinearLayoutManager表示竖直向下
        rvTable.setLayoutManager(new LinearLayoutManager(this));
        //3,初始化一个无数据的适配器
        mTableAdapter = new TableAdapter();
        //4,绑定recyclerView和适配器
        rvTable.setAdapter(mTableAdapter);
        //5,给recyclerView设置空布局
        mTableAdapter.setEmptyView(emptyView);
        //6,给recyclerView的每一个子列表添加点击事件
        mTableAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(MainActivity.this, "我点击了第" + position + "个子view",
                        Toast.LENGTH_SHORT).show();
            }
        });
        //7,添加头文件和脚文件
        View headView = getLayoutInflater().inflate(R.layout.item_table_header, null);
        View footView = getLayoutInflater().inflate(R.layout.item_table_footer, null);
        mTableAdapter.addHeaderView(headView);
        mTableAdapter.addFooterView(footView);
    }

    /**
     * MainActivity中增加下拉刷新和上拉加载的监听方法
     */
    private void smartRefreshView() {
        srlTable.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //下拉刷新,一般添加调用接口获取数据的方法
                getData(2);
                refreshLayout.finishRefresh();
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //上拉加载,一般添加调用接口获取更多数据的方法
                getData(3);
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        });
    }

    private void getData(int mode) {
        //添加临时数据,一般直接从接口获取
        switch (mode) {
            case 2:
                mTableList = new ArrayList<>();
                for (int i = 0; i < 15; i++) {
                    mTableList.add(new TableEntity.ResultBean.ListBean("我爱小狗", "我爱小猫","我爱小兔子"));
                }
                //更新数据
                mTableAdapter.setNewData(mTableList);
                break;
            case 3:
                for (int i = 0; i < 15; i++) {
                    mTableList.add(new TableEntity.ResultBean.ListBean("我爱小狗", "我爱小猫","我爱小兔子"));
                }
                mTableAdapter.setNewData(mTableList);
                break;
            default:
                mTableList = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    mTableList.add(new TableEntity.ResultBean.ListBean("我爱小狗", "我爱小猫","我爱小兔子"));
                }
                break;
        }
    }
}

3,头文件布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="0.75dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="#EBEBEB" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_gravity="center"
        android:gravity="center"
        android:paddingLeft="20dp"
        android:paddingRight="20dp">

        <View
            android:layout_width="0.75dp"
            android:layout_height="match_parent"
            android:background="@color/tv_eb"></View>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="小狗"
            android:textColor="@color/colorAccent"
            android:textSize="14sp" />

        <View
            android:layout_width="0.75dp"
            android:layout_height="match_parent"
            android:background="@color/tv_eb"></View>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="小猫"
            android:textColor="@color/colorAccent"
            android:textSize="14sp" />

        <View
            android:layout_width="0.75dp"
            android:layout_height="match_parent"
            android:background="@color/tv_eb"></View>

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="小兔子"
            android:textColor="@color/colorAccent"
            android:textSize="14sp" />

        <View
            android:layout_width="0.75dp"
            android:layout_height="match_parent"
            android:background="@color/tv_eb"></View>
    </LinearLayout>
</LinearLayout>

4,足文件布局,就一根补全的线。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <View
        android:layout_width="match_parent"
        android:layout_height="0.75dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="@color/colorAccent" />
</LinearLayout>

5,对应github地址

demo地址:https://github.com/mamumu/jsRecyclerView3

如果有发现错误欢迎指正我及时修改,如果有好的建议欢迎留言。如果觉得对你有帮助欢迎给小星星,谢谢。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 不知道人多为患 不去大医院 不知道挂号也难 不住大酒店 不知道客房已满 不去游乐园 不知道热闹非凡 不到上海滩 不...
    郑卫国原创诗歌阅读 442评论 0 2
  • 上周闲来无事就去某些大公司面试了一下,试试现在android这块的水!总结出结论就是,稍大点的公司问的基础的东西还...
    7b3a41b7334e阅读 249评论 0 2
  • 回到相遇之前 天空依然蔚蓝 远方的海岸也依然存在 你带来的一切 和你这个人一样 虚幻无边不可触及 再不会偶遇吧 这...
    kk77阅读 167评论 0 1
  • 凄凄风雪破蓝镜,明明山河修玉床。 孟姜哭城求夫影,田父挥锄献君王。 怨魂掀云天地黄,黄发喜乐自如常。 史泉不枯久泠...
    齐家能阅读 193评论 0 0
  • 姓名:李淑瑛 224期学员 289期志工 325期志工 423期志工 公司:绍兴翔鹰纺织品有限公司 部门:人事行政...
    李淑瑛阅读 95评论 0 0

友情链接更多精彩内容