android中view使用头文件,Android中RecyclerView配合BaseRecyclerViewAdapterHelper实现表格布局(四)...

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

项目说明:

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

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

展示效果:

a53972b87cdc

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 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,头文件布局。

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ffffff"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="0.75dp"

android:layout_marginLeft="20dp"

android:layout_marginRight="20dp"

android:background="#EBEBEB" />

android:layout_width="match_parent"

android:layout_height="35dp"

android:layout_gravity="center"

android:gravity="center"

android:paddingLeft="20dp"

android:paddingRight="20dp">

android:layout_width="0.75dp"

android:layout_height="match_parent"

android:background="@color/tv_eb">

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" />

android:layout_width="0.75dp"

android:layout_height="match_parent"

android:background="@color/tv_eb">

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" />

android:layout_width="0.75dp"

android:layout_height="match_parent"

android:background="@color/tv_eb">

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" />

android:layout_width="0.75dp"

android:layout_height="match_parent"

android:background="@color/tv_eb">

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#ffffff"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="0.75dp"

android:layout_marginLeft="20dp"

android:layout_marginRight="20dp"

android:background="@color/colorAccent" />

5,对应github地址

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值