一、效果图
二、创建步骤:
思路:其实就是将MainActivity中写好表头布局,同时和RecyclerView的Item布局保持一致,这里面列之间使用View进行分割,边框使用layer-list和shape实现。
1:MainActivity代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView rv_sheet;
private List<entity> list;
private SheetAdapter sheetAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list =
new ArrayList<entity>();
for (
int i =
0; i <
30; i++) {
list.add(
new entity(
"Wade",
"James",
"Kobe"));
}
rv_sheet = (RecyclerView) findViewById(R.id.rv_sheet);
rv_sheet.setLayoutManager(
new LinearLayoutManager(
this));
rv_sheet.addItemDecoration(
new DividerItemDecoration(
this,
DividerItemDecoration.VERTICAL_LIST));
sheetAdapter =
new SheetAdapter(list);
rv_sheet.setAdapter(sheetAdapter);
}
}
2、MainActivity布局文件
<?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="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="vertical"
tools:context=".MainActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:singleLine="true"
android:text="表头1"
android:textColor="#000000"
android:textSize="15sp" />
<View
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:singleLine="true"
android:text="表头2"
android:textColor="#000000"
android:textSize="15sp" />
<View
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dip"
android:singleLine="true"
android:text="表头3"
android:textColor="#000000"
android:textSize="15sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_sheet"
android:name="com.example.wjm19.sheetdemo"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
3、RecyclerView适配器代码
/**
* Created by wjm on 2016/12/6.
*/
public class SheetAdapter extends RecyclerView.Adapter {
private List<entity> list;
public SheetAdapter(List<entity> list) {
this.list = list;
}
@Override
public RecyclerView.ViewHolder
onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.sheet_item_layout, parent,
false);
return new sheetViewHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder,
int position) {
sheetViewHolder vh = (sheetViewHolder) holder;
vh.getTv_sheetRow1().setText(list.get(position).getSheetRow1());
vh.getTv_sheetRow2().setText(list.get(position).getSheetRow2());
vh.getTv_sheetRow3().setText(list.get(position).getSheetRow3());
}
@Override
public int getItemCount() {
return list.size();
}
public class sheetViewHolder extends RecyclerView.ViewHolder{
public final View mView;
public final TextView tv_sheetRow1;
public final TextView tv_sheetRow2;
public final TextView tv_sheetRow3;
public sheetViewHolder(View itemView) {
super(itemView);
mView = itemView;
tv_sheetRow1 = (TextView) itemView.findViewById(R.id.tv_sheetRow1);
tv_sheetRow2 = (TextView) itemView.findViewById(R.id.tv_sheetRow2);
tv_sheetRow3 = (TextView) itemView.findViewById(R.id.tv_sheetRow3);
}
public TextView
getTv_sheetRow1() {
return tv_sheetRow1;
}
public TextView
getTv_sheetRow2() {
return tv_sheetRow2;
}
public TextView
getTv_sheetRow3() {
return tv_sheetRow3;
}
}
}
4、RecyclerView 中的Item布局文件
<?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="wrap_content"
android:background="@drawable/border">
<TextView
android:id="@+id/tv_sheetRow1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="第一列"
android:textColor="#000000"
android:textSize="15sp" />
<View
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:id="@+id/tv_sheetRow2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="第二列"
android:textColor="#000000"
android:textSize="15sp" />
<View
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="#000000" />
<TextView
android:id="@+id/tv_sheetRow3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dip"
android:text="第三列"
android:textColor="#000000"
android:textSize="15sp" />
</LinearLayout>
5、实体类
/**
* 实体类
* Created by wjm on 2016/12/7.
*/
public class entity {
private String sheetRow1;
private String sheetRow2;
private String sheetRow3;
public entity(String sheetRow1, String sheetRow2, String sheetRow3) {
this.sheetRow1 = sheetRow1;
this.sheetRow2 = sheetRow2;
this.sheetRow3 = sheetRow3;
}
public String
getSheetRow1() {
return sheetRow1;
}
public void setSheetRow1(String sheetRow1) {
this.sheetRow1 = sheetRow1;
}
public String
getSheetRow2() {
return sheetRow2;
}
public void setSheetRow2(String sheetRow2) {
this.sheetRow2 = sheetRow2;
}
public String
getSheetRow3() {
return sheetRow3;
}
public void setSheetRow3(String sheetRow3) {
this.sheetRow3 = sheetRow3;
}
}
代码都非常简单,对recyclerView稍微熟一点的应该很容易看懂。 源码地址:sheetDemo
转载请注明原文地址: https://ju.6miu.com/read-1000341.html