AppBarLayout+recycleview的使用和注意事项

MainActivity 中的布局main_activity

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tl="http://schemas.android.com/tools"
    android:id="@+id/displaygood_fra_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:splitMotionEvents="true">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/displaygood_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             app:layout_behavior="com.huigu.newshangchaopj.view.CustomBehavior">
          

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/displaygood_collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="252dp"
                android:background="@color/cffffff"//可以不设置
                android:minHeight="67.5dp"//可以设置可以折叠的高
                app:contentScrim="@color/cffffff"//可以不设置
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
                   <!--|snap-->
                <!--app:layout_collapseMode="parallax"-->//去掉这个属性 就不会叠着上去 而是顶上去的
                <!--  app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"-->
                <!--android:fitsSystemWindows="true"-->
             <ImageView
                 android:id="@+id/myhome_iv_headimg"
                 android:layout_width="72dp"
                 android:layout_height="72dp"
                 android:layout_centerInParent="true"
                 android:src="@mipmap/icon_moren_xiaoyan" />

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/cffffff"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            
              <com.flyco.tablayout.SlidingTabLayout
                    android:id="@+id/dispaly_good_tabLayout"
                    android:layout_width="187dp"
                    android:layout_height="@dimen/dp_48"
                    android:layout_gravity="left"
                    app:tl_divider_padding="@dimen/dp_13"
                    app:tl_divider_width="1dp"
                    app:tl_indicator_color="@color/cdb0008"
                    app:tl_indicator_height="@dimen/dp_2"
                
  • 1
    点赞
  • 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、付费专栏及课程。

余额充值