Android通过RecyclerView实现手风琴效果

Android通过RecyclerView实现手风琴效果

  • Android中RecyclerView动态列表中点击每一项Item展开列表详细内容
  • 关于RecyclerView使用二,通过RecyclerView 实现简单的手风琴折叠效果的一个记录。
  • 对于ExpandableListView又称可扩展的ListView,即每一个一级布局下都可以展开对应的二级布局。每一级布局都需要单独一个布局文件。
  • 对于简单的手风琴效果展示的列表内容,喜欢用RecyclerView实现,这样可以偷懒,少写布局文件。
  • 只是想用RecyclerView实现ExpandableListView的效果,提供一种实现方法和思路。
  • 简单实现手风琴效果如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-od1gTPZk-1667052911532)(https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/6728d7807b3b41d48b318810f9273fe0~tplv-k3u1fbpfcp-watermark.image?)]

  • RecyclerView页面布局:
          <?xml version="1.0" encoding="utf-8"?>
          <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context=".ui.equipment.EquipmentJobLoggingActivity">
    
              <androidx.recyclerview.widget.RecyclerView
                  android:id="@+id/wareHouse_scrap_fra"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:layout_constraintEnd_toEndOf="parent"
                  app:layout_constraintStart_toStartOf="parent"
                  app:layout_constraintTop_toTopOf="parent" />
    
          </androidx.constraintlayout.widget.ConstraintLayout>
    
  • RecyclerView列表的每一项布局文件
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.cardview.widget.CardView
            android:id="@+id/Card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="5dp"
            android:translationZ="2dp"
            app:cardCornerRadius="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
            <TextView
                android:id="@+id/person_time"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:background="@color/aurantius"
                android:gravity="center"
                android:rotation="45"
                android:text="唐代"
                android:textColor="@color/white"
                android:textSize="10sp"
                android:translationX="13dp"
                android:translationY="7dp"
                android:translationZ="10dp" />
            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="5dp"
                android:paddingStart="@dimen/f_mp_10_lr"
                android:paddingEnd="@dimen/f_mp_10_lr"
                android:translationZ="2dp">
                <TextView
                    android:id="@+id/title"
                    android:layout_width="wrap_content"
                    android:layout_height="30dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="10dp"
                    android:gravity="center_vertical"
                    android:text=""
                    android:textSize="18sp"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
                <TextView
                    android:id="@+id/title_person"
                    android:layout_width="wrap_content"
                    android:layout_height="25dp"
                    android:layout_marginTop="25dp"
                    android:layout_marginEnd="10dp"
                    android:gravity="center_vertical"
                    android:text=""
                    android:textSize="14sp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
                <LinearLayout
                    android:id="@+id/msg_ll"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="10dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginEnd="10dp"
                    android:layout_marginBottom="10dp"
                    android:orientation="vertical"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/title">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginTop="4dp"
                        android:background="@color/divider" />
                    <TextView
                        android:id="@+id/msg_contentMore"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="4dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginBottom="10dp"
                        android:text=""
                        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
                </LinearLayout>
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • 设置适配器 ,实现点击折叠展示(其实是通过控制隐藏和显示布局实现手风琴折叠效果)
      //数据源 实体类
      public class MsgBean {
         
        private  String person_time;
        private  String person;
        private  String content;
        private  String contentMore;
        //构造函数 ,getter 、setter 方法省略不展示、
      }
    
      import android.content.Context;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.widget.LinearLayout;
      import android.widget.RelativeLayout;
      import android.widget.TextView;
    
      import androidx.cardview.widget.CardView;
      import androidx.recyclerview.widget.Rec
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值