RecycleView 实现多标签展开收起效果

最近的项目需要实现一个多标签展开收起的效果,具体效果如下

收起效果

展开效果
拿到效果图,很自然会想到用 RecyclerView 来实现,但是至于具体怎么去收起和展开,我这里用了个取巧的办法,可能不是最好的办法,但也能达到要求,在此,主要作为记录用,大神轻喷。 废话不多说,直接上代码才是最实在的。 外层布局代码:

       <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_play"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/base10dp"
            android:layout_marginRight="@dimen/base10dp"/>
        <TextView
            android:id="@+id/tv_open_tips"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="@dimen/base20dp"
            android:text="展开全部"
            android:drawableRight="@drawable/detail_open"
            android:drawablePadding="@dimen/base4dp"
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_marginTop="@dimen/base6dp"
            android:layout_marginBottom="@dimen/base10dp"
            android:textSize="@dimen/base10dp"
            />
复制代码

布局预览效果

这里点击展开和收起的按钮,我单独用了个 TextView 来实现,因为这样操作起来最简单,少了很多逻辑处理。(没办法,有点懒……) 接下来是 RecyclerView 中 item 的布局实现,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlRoot"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="@dimen/base80dp"
        android:minHeight="@dimen/base30dp"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:maxLength="4"
        android:background="@drawable/btn_common_oval_normal"
        android:maxLines="1"
        android:textColor="@color/common_black"
        android:textSize="@dimen/base16dp" />
</RelativeLayout>
复制代码

布局都写完了之后,我们直接看 adapter 适配器的实现代码:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
    private Context context;
    private List<Test> list;

    public MyAdapter(Context context) {
        this.context = context;
    }

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

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        String name = list.get(position).getName() ;
        holder.tv.setText(name);
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    //隐藏
    public void setHideList(List<Test> newList) {
        this.list = newList;
        notifyDataSetChanged();
    }

    //展开
    public void setOpenList(List<Test> openList) {
        this.list = openList;
        notifyDataSetChanged();
    }

    //不需要隐藏/展开
    public void setRealList(List<Test> realList) {
        this.list = realList;
        notifyDataSetChanged();
    }

    //清除数据
    public void clearData() {
        if (list != null) {
            this.list.clear();
            notifyDataSetChanged();
        }
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView tv;

        public MyHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tvName);
        }
    }
}
复制代码

TextView 绑定展开收起事件

        /**
         * 处理展开收缩的逻辑
         */
        tvOpenTips.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOpen) {
                    adapter.setHideList(hideList);
                    tvOpenTips.setText("展开全部");
                    tvOpenTips.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.detail_open, 0);
                    isOpen = false;
                } else {
                    adapter.setOpenList(openList);
                    tvOpenTips.setText("收起全部");
                    tvOpenTips.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.detail_close, 0);
                    isOpen = true;
                }
            }
        });
复制代码

设置展开收起的数据源

    private void setData() {
        if (list.size() > 4) { //设置大于多少条数据开始隐藏
            for (int i = 0, j = list.size(); i < j; i++) {
                openList.add(list.get(i));
            }
            for (int i = 0; i < 4; i++) {
                hideList.add(list.get(i));
            }
            adapter.setHideList(hideList);
        } else {
            adapter.setRealList(list);
        }
        tvOpenTips.setVisibility(list.size() > 4 ? View.VISIBLE : View.GONE);
        rv.setAdapter(adapter);
    }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现抖音评论收起功能,可以使用以下步骤: 1. 在布局文件添加BottomSheet和RecycleView。 2. 创建一个Adapter类来处理RecycleView的数据和布局。 3. 在Adapter,添加一个方法用于控制评论的展开收起。 4. 在Activity,将Adapter设置给RecycleView,并在点击收起按钮时调用Adapter的方法。 5. 在BottomSheet的回调函数,设置BottomSheet的高度为RecycleView的高度。 以下是示例代码: 1. 布局文件 ``` <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 底部弹出的BottomSheet --> <com.google.android.material.bottomsheet.BottomSheetBehavior android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" app:behavior_hideable="true" app:behavior_peekHeight="0dp" app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </com.google.android.material.bottomsheet.BottomSheetBehavior> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 2. Adapter类 ``` public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> { private List<Comment> commentList; private boolean isExpanded = false; public CommentAdapter(List<Comment> commentList) { this.commentList = commentList; } @NonNull @Override public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false); return new CommentViewHolder(view); } @Override public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) { Comment comment = commentList.get(position); holder.tvComment.setText(comment.getCommentText()); } @Override public int getItemCount() { if (isExpanded) { return commentList.size(); } else { return Math.min(commentList.size(), 2); // 只显示前两条评论 } } public void expandComments() { isExpanded = true; notifyDataSetChanged(); } static class CommentViewHolder extends RecyclerView.ViewHolder { TextView tvComment; CommentViewHolder(View itemView) { super(itemView); tvComment = itemView.findViewById(R.id.tv_comment); } } } ``` 3. Activity代码 ``` public class MainActivity extends AppCompatActivity { private CommentAdapter commentAdapter; private BottomSheetBehavior bottomSheetBehavior; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = findViewById(R.id.recyclerView); recyclerView.setLayoutManager(new LinearLayoutManager(this)); List<Comment> commentList = generateDummyComments(); commentAdapter = new CommentAdapter(commentList); recyclerView.setAdapter(commentAdapter); Button btnCollapse = findViewById(R.id.btn_collapse); btnCollapse.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { commentAdapter.expandComments(); } }); // 获取BottomSheetBehavior实例 View bottomSheet = findViewById(R.id.bottom_sheet); bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); bottomSheetBehavior.setPeekHeight(0); bottomSheetBehavior.setHideable(true); bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { @Override public void onStateChanged(@NonNull View bottomSheet, int newState) { if (newState == BottomSheetBehavior.STATE_HIDDEN) { finish(); } } @Override public void onSlide(@NonNull View bottomSheet, float slideOffset) { // 设置BottomSheet的高度为RecycleView的高度 View recyclerView = bottomSheet.findViewById(R.id.recyclerView); ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams(); layoutParams.height = (int) (recyclerView.getHeight() * (1 - slideOffset)); bottomSheet.setLayoutParams(layoutParams); } }); } private List<Comment> generateDummyComments() { List<Comment> commentList = new ArrayList<>(); for (int i = 0; i < 10; i++) { Comment comment = new Comment("这是第" + (i + 1) + "条评论"); commentList.add(comment); } return commentList; } } ``` 这样就可以实现抖音评论收起功能了。当点击收起按钮时,RecycleView展开显示所有评论;当底部弹出的BottomSheet向上滑动时,会动态改变BottomSheet的高度,实现平滑的动画效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值