Android Studio实现RecyclerView
实现类微信界面见前篇:
AS实现类微信界面:https://blog.csdn.net/qq_44841678/article/details/105005148.
要求:
用RecyclerView实现各种布局
实现界面展示:
滑动删除:(左右滑动删除)
顶部悬浮:(吸顶)
展开和收缩:
前端页面设计:
注意要使用RecyclerView,则必须导入一个support-v7的包,在最外层的布局文件中使用RecyclerView,之后创建item.xml,主要展示出来的布局在item.xml中编辑。列如展开收缩效果的布局:
tab03.xml:
<?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" >
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcv_expandcollapse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:overScrollMode="never"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
item_expand:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_margin="5dp"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_parent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#3E8841">
<TextView
android:id="@+id/tv_team"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
tools:text="主布局" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_child"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#54CC58"
android:visibility="gone">
<TextView
android:id="@+id/tv_team_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/white"
tools:text="副布局" />
</RelativeLayout>
</LinearLayout>
避坑:注意在item.xml文件中的LinearLayout中的layout_height要设置。
后端代码:
对应每个RecyclerView的布局需要一个adapter.java文件,此时要注意在一个项目中id的代表性,不能混。
避坑:在顶部悬浮:(吸顶)时,我最初写时漏了关键的改变顶部内容的操作,导致顶部一直吸顶效果,之后排了错。myviewholder.itemView.setContentDescription(stickyData.area);
还要注意,在在Fragment下面使用LinearLayoutManager传入this和在Activity中有些不同,要传入this.getActivity();
部分代码:
context=this.getActivity();
adapter=new adapter_expand(context,mList);
LinearLayoutManager manager=new LinearLayoutManager(context);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
最后附上代码(码云):
链接: https://gitee.com/yangjy11/weixin_recyclerview/tree/master/WeiXinApplication/app/src/main/.