收起左侧 [Android界面] 左右slidingMenu+iewpager冲突

加个OnPageChangeListener监听,当滑到第一页时把SlidingMenu设置为setMode(SlidingMenu.LEFT),当滑到最后一页时把SlidingMenu设置为setMode(SlidingMenu.RIGHT),滑到中间时把SlidingMenu设置为setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);



vpContent.setOnPageChangeListener(new OnPageChangeListener() {

                        
                        @Override
                        public void onPageSelected(int arg0) {
                              if(adapter!=null && adapter.getCount()>1) {
                                          if(arg0==0) {//第一页                                           
                                                menu.setMode(SlidingMenu.LEFT);                                                                                                                                                                                            
                                                menu .setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                                         }  else if(position==adapter.getCount()-1){ //最后一页
                                                 menu .setMode(SlidingMenu.RIGHT);
                                                 menu .setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                                        } else { //中间页面
                                                  menu  .setMode(SlidingMenu.LEFT_RIGHT);
                                                  menu .setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
                                        } 
                              } 
                                
                        }
}
  • 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、付费专栏及课程。

余额充值