Recyclerview实现按时间划分展示图片

发布于:2023-01-09 ⋅ 阅读:(519) ⋅ 点赞:(0)

Recyclerview实现按时间划分展示图片

大致效果:

在这里插入图片描述

实现思路

主要用recyclerview实现,每个item的话是TextView+Gridlayout实现的,然后动态把图片加入到gridlayout中,下次用RecyclerView+RecyclerView实现下

代码实现

1. layout布局
RecyclerView item的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/tv_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="2dp"
            android:drawableTint="#d3d3d3"
            android:gravity="center"
            android:text="2022-8-18"
            android:textColor="#000"
            android:textSize="16sp"
            android:textStyle="bold" />

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:src="@mipmap/round_check"
            android:layout_centerVertical="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="10dp"
            android:id="@+id/check"
            />
    </RelativeLayout>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/grid"
        android:columnCount="3"
        />
</LinearLayout>

GridLayout 子View的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingVertical="2dp"
    android:paddingRight="2dp"
    android:paddingLeft="2dp"
    android:layout_marginVertical="2dp"
    android:layout_marginRight="2dp">

    <com.google.android.material.imageview.ShapeableImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        android:scaleType="fitXY"
        android:id="@+id/item_img"
        app:shapeAppearance="@style/RoundedStyle"
        />

    <CheckBox
        android:id="@+id/item_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@id/item_img"
        android:gravity="center"
        android:padding="0dp"
        android:layout_marginLeft="5dp"
        />
</RelativeLayout>

2. 主要代码

//BaseQuickAdapter  implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4'
    public static class RvAdapter extends BaseQuickAdapter<BeanAlbum, BaseViewHolder>{

        public RvAdapter(@Nullable List<BeanAlbum> data) {
            super(R.layout.item_lv_timeline, data);
            addChildClickViewIds(R.id.check);
        }

        @Override
        protected void convert(BaseViewHolder helper, BeanAlbum item) {
            helper.setText(R.id.tv_date,item.getDate());
            ImageView check = helper.getView(R.id.check);
            GridLayout container = helper.getView(R.id.grid);
            if (item.isCheck()) {
                check.setImageResource(R.mipmap.round_check2);
            }else {
                check.setImageResource(R.mipmap.round_check);
            }
            container.removeAllViews();
            for (BeanImage beanImage : item.getList()) {
                View inflate = LayoutInflater.from(getContext()).inflate(R.layout.item_img_timeline, null, false);
                container.addView(inflate);

                ImageView imageView = inflate.findViewById(R.id.item_img);
                //
                imageView.setImageResource(beanImage.getMipmap());
//                Glide.with(getContext())
//                        .asBitmap()
//                        .diskCacheStrategy(DiskCacheStrategy.NONE)
//                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(20)))
//                        .load(item.getPath())
//                        .into(image);

                CheckBox checkBox = inflate.findViewById(R.id.item_check);
                checkBox.setChecked(beanImage.isCheck);
                checkBox.setOnCheckedChangeListener((compoundButton, b) -> {
                    checkBox.setChecked(b);
                });
            }
        }
    }

网站公告

今日签到

点亮在社区的每一天
去签到