让代码更简单

使用RecyclerView来绘制Android表格

重要:本文最后更新于2022-11-08 19:26:05,某些文章具有时效性,若有错误或已失效,请在下方留言或联系代码狗

很多安卓APP中都有表格的应用,前面我也分享了一些安卓的表格框架,那些框架都是大神们封装好,迭代好几个版本的,大家不必担心不好用。对于简单表格,表格框架不能满足我们的情况,咱们就可以按照下面的方法使用RecyclerView来绘制一些我们需要的表格。先看下图效果:

使用RecyclerView来绘制Android表格

使用RecyclerView来绘制Android表格

要实现上图效果,我们需要先实现布局。

上图的布局是由textview与RecyclerView组成的,代码如下:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/content_bg"
        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>

然后我们在RecyclerView中显示的item一行一行的数据布局是这样的

复制
<?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:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <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"
        tools:ignore="Suspicious0dp" />

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

布局完成了,我们需要对我们的RecyclerView中每一行实现适配数据,我们可以将一行看做一个实体对象,分别存储对应数据,然后在适配器中取出对象数据,显示在item布局的textview中。首先看实体对象类

复制
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设置数据

复制
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.tableitem, 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;
        }
    }
}

还有一个辅助类DividerItemDecoration,为功能做铺垫

复制
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;

    public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;

    private Drawable mDivider;

    private int mOrientation;

    public DividerItemDecoration(tableActivity context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        setOrientation(orientation);
    }

    public void setOrientation(int orientation) {
        if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
            throw new IllegalArgumentException("invalid orientation");
        }
        mOrientation = orientation;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent) {
        // Log.v("recyclerview - itemdecoration", "onDraw()");

        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else {
            drawHorizontal(c, parent);
        }

    }


    public void drawVertical(Canvas c, RecyclerView parent) {
        final int left = parent.getPaddingLeft();
        final int right = parent.getWidth() - parent.getPaddingRight();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            android.support.v7.widget.RecyclerView v = new android.support.v7.widget.RecyclerView(parent.getContext());
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getHeight() - parent.getPaddingBottom();

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    }
}

最后,我们只需在需要使用表格的activity中使用如下代码即可显示表格

复制
List<entity> list = new ArrayList<entity>();
for (int i = 0; i < 30; i++) {
    list.add(new entity("工号", "姓名", "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(getdata());

rv_sheet.setAdapter(sheetAdapter);

参考网友分享在CSDN上的教程,具体记不住了。

 

感觉很棒!可以赞赏支持我哟~

0 打赏

评论 (1)

登录后评论
代码真的复杂啊
QQ咨询 邮件咨询 狗哥推荐