今天介绍了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
如果有发现错误欢迎指正我及时修改,如果有好的建议欢迎留言。如果觉得对你有帮助欢迎给小星星,谢谢。
网友评论