请注明出处: http://blog.csdn.net/qq_23179075/article/details/79230457

Android中RecyclerView点击item展开列表详细内容(超简单实现)

下面是具体实现代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><RelativeLayoutandroid:id="@+id/msg_rl"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_marginTop="10dp"android:background="@drawable/corners_alpha_white"android:gravity="center_vertical"android:padding="8dp"><TextViewandroid:id="@+id/msg_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_marginTop="10dp"android:text=""android:textColor="@color/white"android:textSize="14sp" /><TextViewandroid:id="@+id/msg_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/msg_time"android:layout_margin="5dp"android:text=""android:textColor="@color/white"android:textSize="18sp" /><LinearLayoutandroid:layout_below="@+id/msg_content"android:id="@+id/msg_ll"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><Viewandroid:layout_marginTop="4dp"android:layout_width="match_parent"android:layout_height="1dp"android:background="@color/__picker_common_primary"/><TextViewandroid:id="@+id/msg_contentMore"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="10dp"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"android:layout_marginTop="4dp"android:textAppearance="@style/TextAppearance.AppCompat.Body1"android:text=""android:textColor="@color/white" /></LinearLayout></RelativeLayout>
</LinearLayout>

核心代码 Adapter :

/**** @author zhengliang* @date 2018/2/1*/
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {private Context context;/*** 消息列表数据*/private List<MsgBean> lists;/*** 标记展开的item*/private int opened = -1;public MsgAdapter(Context context) {this.context = context;lists = new ArrayList<>();}/*** 设置列表数据* @param lists*/public void setLists(List<MsgBean> lists) {this.lists = lists;notifyDataSetChanged();}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(context).inflate(R.layout.msg_item, parent, false);return new ViewHolder(view);}@Overridepublic void onBindViewHolder(final ViewHolder holder, int position) {holder.bindView(position,lists.get(position));}@Overridepublic int getItemCount() {return lists.size();}class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {private TextView msgTime;private TextView msgContent;private TextView msgContentMore;private RelativeLayout msgRl;private LinearLayout msgLl;public ViewHolder(View itemView) {super(itemView);msgTime = (TextView) itemView.findViewById(R.id.msg_time);msgContent = (TextView) itemView.findViewById(R.id.msg_content);msgContentMore = (TextView) itemView.findViewById(R.id.msg_contentMore);msgRl = (RelativeLayout) itemView.findViewById(R.id.msg_rl);msgLl = (LinearLayout) itemView.findViewById(R.id.msg_ll);msgRl.setOnClickListener(this);}/*** 此方法实现列表数据的绑定和item的展开/关闭*/void bindView(int pos, MsgBean bean) {msgTime.setText(bean.created);msgContent.setText(bean.content);msgContentMore.setText(bean.contentMore);if (pos == opened){msgLl.setVisibility(View.VISIBLE);} else{msgLl.setVisibility(View.GONE);}}/*** item的点击事件* @param v*/@Overridepublic void onClick(View v) {if (opened == getAdapterPosition()) {//当点击的item已经被展开了, 就关闭.opened = -1;notifyItemChanged(getAdapterPosition());} else {int oldOpened = opened;opened = getAdapterPosition();notifyItemChanged(oldOpened);notifyItemChanged(opened);}}}
}

主要的代码是 ViewHolderbindView() , onClick() 这两个方法对变量 opened 的操作.

Activity中使用:

使用和 RecyclerView 平常的使用一样, 这里贴上设置 RecyclerView 在发生变化的时候的动画设置

rlv.getItemAnimator().setChangeDuration(300);
rlv.getItemAnimator().setMoveDuration(300);

这样在详细内容显示于隐藏的时候就有个打开和关闭的动画.

Android中RecyclerView点击item展开列表详细内容(超简单实现)相关推荐

  1. android listview item 展开动画,android的ListView点击item使item展开的做法的实现代码

    本文介绍了android的ListView点击item使item展开的做法的实现代码,分享给大家,具体如下: 效果图: 原理是点击item的时候,重新measure list的各个item的高度 li ...

  2. Android商城开发----点击左侧分类列表右侧更新对应列表内容

    Android商城开发----点击左侧分类列表右侧更新对应列表内容 目录 Android商城开发----点击左侧分类列表右侧更新对应列表内容 一.首先说布局: 二.主要说一下,布局完成后实现点击左侧类 ...

  3. Android中RecyclerView嵌套RecyclerView或嵌套ListView

    Android中RecyclerView嵌套RecyclerView或嵌套ListView

  4. 基于Android小巫新闻客户端开发---显示新闻详细内容UI设计

    基于Android小巫新闻客户端开发---显示新闻详细内容UI设计 2013年2月27日,天气潮湿!!! 距上一次写的主界面业务逻辑实现,已经过来11天,小巫觉得拖得太久了,所以决定尽量把所有的内容介 ...

  5. Android使用RecyclerView实现仿微信联系人列表

    现在联系人列表基本都是按照字母或者拼音来进行分类,右边有一排字母供用户快速定位到指定的字母位置,效果图如下: OK,输入的联系人类型可能有很多种,比如汉字.英文.数字.特殊符号等等,其中汉字会转化成拼 ...

  6. Android中使用JiaoZiVideoPlayer来实现视频列表播放的效果

    目的:我这边是想做类似于斗鱼直播里的视频模块的视频列表播放形式. 思路:使用第三方插件--JiaoZiVideoPlayer.GitHub地址:https://github.com/lipangit/ ...

  7. Android中模拟点击软件的实现原理探究

    简介 按键触摸类模拟点击软件最早开始于PC时代,指的是可以通过设置.脚本控制等方式去实现模拟点击,解放双手,达到自动化操作的目的.在Android中,模拟点击对应的就是触发屏幕点击事件,多用于游戏中完 ...

  8. Android中Button点击事件

    在Android中的Button控件上,有三种鼠标点击事件,分别是鼠标点击事件,长按事件,触摸事件 接下来,看一下是如何进行的 方法一: 在layout中创建一个xml文件,使用什么布局方式,你都可以 ...

  9. android手机投影电视软件,Type-C手机投屏电视/投影仪超简单,快看你的可以吗?...

    原标题:Type-C手机投屏电视/投影仪超简单,快看你的可以吗? 在智能手机普及之前,大家的娱乐主要还是看电视,或者用电脑上网.玩游戏,而现在大家最离不开的就是手机.但是教授也发现一个现象,很多机友对 ...

最新文章

  1. python使用argparse解析命令行参数
  2. C#StreamWriter的操作解析
  3. PHP数组操作——获取数组最后一个值的方法
  4. Android开发之解决NestedScrollView滑动监听兼容低版本的方法
  5. php对象底层结构,PHP 底层原理之类和对象
  6. Des与3Des加密解密
  7. PHPWeb开发入门体验学习笔记
  8. linux查看磁盘io带宽,[Linux] 磁盘IO性能查看和优化以及iostat命令
  9. jodd忽略ssl证书_关于java访问https资源时,忽略证书信任问题
  10. IOS多线程任务(综述篇)
  11. OpenShift 4 之升级集群
  12. 《计算机网络:自顶向下方法(原书第6版)》一2.7 TCP套接字编程
  13. java表白程序_Java实现表白小程序
  14. 探索艾利特机器人丨EC66在物流行业的应用
  15. du和df命令的区别
  16. 信息安全快讯丨桃李满天下,金秋谢师恩——教师节快乐!
  17. 微信小程序 - 音乐播放器源码
  18. SAP方丈 SAP常见问题与解决办法 转
  19. 自行车平衡java小游戏_自行车水上平衡赛
  20. 艺赛旗(RPA)【服务端】修改服务器访问端口

热门文章

  1. 最新java毕业设计论文参考文献第二版
  2. linux远程visualvm,visualVM远程监控JVM详解
  3. Tomcat8部署项目启动成功,访问项目报404解析
  4. 2022年,操作抖音小店无货源真能赚钱?收益如何?
  5. 【PTA】【Python】【拼题A 2022 跨年挑战赛】小孩子才做选择,大人全都要
  6. 前端vue和django后端数据交互,跨域问题的解决
  7. QGI缓冲区渐变色符号制作
  8. Nuke与Natron的区别是什么?
  9. “curl: (7) Failed to receive SOCKS4 connect request ack.”解决方法
  10. malloc内存分配详解