Android中RecyclerView点击item展开列表详细内容(超简单实现) - Go语言中文社区

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


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

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

效果.gif

下面是具体实现代码:

<?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">
    <RelativeLayout
        android: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">
        <TextView
            android: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" />
        <TextView
            android: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" />
        <LinearLayout
            android:layout_below="@+id/msg_content"
            android:id="@+id/msg_ll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <View
                android:layout_marginTop="4dp"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/__picker_common_primary"/>
            <TextView
                android: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();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.msg_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.bindView(position,lists.get(position));
    }

    @Override
    public 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
         */
        @Override
        public 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);

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

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_23179075/article/details/79230457
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢