android使用BottomShee+RecycleView 实现抖音评论收起
时间: 2023-05-28 15:05:12 浏览: 341
要实现抖音评论收起功能,可以使用以下步骤:
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的高度,实现平滑的动画效果。
阅读全文