滑动以在回收器视图android中显示另一个隐藏的布局

前端之家收集整理的这篇文章主要介绍了滑动以在回收器视图android中显示另一个隐藏的布局前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个回收站视图,我想在刷卡项目视图上显示另一个布局,它隐藏在当前布局后面.总之,我希望实现像下面的图像.

我的代码的问题是整个视图被刷,但我只想滑动到隐藏布局的宽度.

活动代码

  1. final ItemTouchHelper.Callback simpleItemTouchCallback = new ItemTouchHelper.Callback() {
  2. @Override
  3. public boolean onMove(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder,RecyclerView.ViewHolder target) {
  4. if (viewHolder.getAdapterPosition() < target.getAdapterPosition()) {
  5. for (int i = viewHolder.getAdapterPosition(); i < target.getAdapterPosition(); i++) {
  6. Collections.swap(myDataset,i,i + 1);
  7. }
  8. } else {
  9. for (int i = viewHolder.getAdapterPosition(); i > target.getAdapterPosition(); i--) {
  10. Collections.swap(myDataset,i - 1);
  11. }
  12. }
  13. mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
  14. return true;
  15. }
  16.  
  17. @Override
  18. public boolean isLongPressDragEnabled() {
  19. return true;
  20. }
  21. @Override
  22. public boolean isItemViewSwipeEnabled() {
  23. return true;
  24. }
  25. @Override
  26. public void onSwiped(final RecyclerView.ViewHolder viewHolder,final int swipeDir) {
  27.  
  28. }
  29.  
  30. @Override
  31. public void onChildDraw(Canvas c,RecyclerView recyclerView,float dX,float dY,int actionState,boolean isCurrentlyActive) {
  32. View itemView = viewHolder.itemView;
  33. ImageView delete_image=(ImageView) itemView.findViewById(R.id.delete_image);
  34. delete_image.setY(itemView.getTop());
  35. if(isCurrentlyActive) {
  36. delete_image.setVisibility(View.VISIBLE);
  37. }else{
  38. delete_image.setVisibility(View.GONE);
  39. }
  40. super.onChildDraw(c,recyclerView,viewHolder,dX,dY,actionState,isCurrentlyActive);
  41. }
  42.  
  43. @Override
  44. public int getMovementFlags(RecyclerView recyclerView,RecyclerView.ViewHolder viewHolder) {
  45. int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
  46. int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
  47. return makeMovementFlags(dragFlags,swipeFlags);
  48. }
  49. };
  50.  
  51.  
  52. ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
  53. itemTouchHelper.attachToRecyclerView(mRecyclerView);

recylerview项目的自定义布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:fresco="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:orientation="horizontal"
  7.  
  8. android:paddingBottom="@dimen/padding_xsmall">
  9.  
  10. <LinearLayout
  11. android:id="@+id/top_layout"
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:layout_gravity="center_vertical"
  15. android:background="@color/very_light_grey"
  16. android:orientation="horizontal"
  17. android:paddingBottom="@dimen/padding_xlarge"
  18. android:paddingEnd="@dimen/padding_small"
  19. android:paddingLeft="@dimen/padding_xlarge"
  20. android:paddingRight="@dimen/padding_small"
  21. android:paddingStart="@dimen/padding_xlarge"
  22. android:paddingTop="@dimen/padding_xlarge">
  23.  
  24. <com.facebook.drawee.view.SimpleDraweeView
  25. android:id="@+id/friend_image"
  26. android:layout_width="50dp"
  27. android:layout_height="50dp"
  28. android:layout_gravity="center_vertical"
  29. fresco:placeholderImage="@drawable/user_placeholder"
  30. fresco:roundAsCircle="true"
  31. fresco:roundedCornerRadius="50dp"
  32. fresco:roundingBorderColor="@android:color/white"
  33. fresco:roundingBorderWidth="2dp" />
  34.  
  35. <LinearLayout
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:layout_gravity="center_vertical"
  39. android:layout_marginEnd="@dimen/margin_small"
  40. android:layout_marginLeft="@dimen/margin_small"
  41. android:layout_marginRight="@dimen/margin_small"
  42. android:layout_marginStart="@dimen/margin_small"
  43. android:orientation="vertical">
  44.  
  45. <RelativeLayout
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content">
  48.  
  49. <bi.it.com.bio.textview.CustomTextView
  50. android:id="@+id/friend_name"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:layout_alignParentLeft="true"
  54. android:layout_alignParentStart="true"
  55. android:layout_toLeftOf="@+id/badge_text"
  56. android:text="John"
  57. android:textSize="@dimen/text_size_medium" />
  58.  
  59. <bi.it.com.bio.textview.CustomTextView
  60. android:id="@+id/badge_text"
  61. android:layout_width="16dp"
  62. android:layout_height="16dp"
  63. android:layout_alignParentRight="true"
  64. android:background="@drawable/badgeicon"
  65. android:gravity="center"
  66. android:text="24"
  67. android:textColor="@android:color/white"
  68. android:textSize="@dimen/text_size_xxsmall" />
  69. </RelativeLayout>
  70.  
  71. <bi.it.com.bio.textview.CustomTextView
  72. android:layout_width="wrap_content"
  73. android:layout_height="wrap_content"
  74. android:text="this is message from john"
  75. android:textSize="@dimen/text_size_xsmall" />
  76.  
  77. </LinearLayout>
  78.  
  79. </LinearLayout>
  80.  
  81. <ImageView
  82. android:id="@+id/delete_image"
  83. android:paddingLeft="@dimen/padding_large"
  84. android:paddingStart="@dimen/padding_large"
  85. android:paddingEnd="@dimen/padding_large"
  86. android:paddingRight="@dimen/padding_large"
  87. android:paddingTop="@dimen/padding_small"
  88. android:paddingBottom="@dimen/padding_small"
  89. android:layout_width="wrap_content"
  90. android:layout_height="match_parent"
  91. android:background="@color/red_color_list"
  92. android:src="@drawable/ic_delete_frd"
  93. android:layout_gravity="end|right"
  94. android:visibility="gone"/>
  95.  
  96. </FrameLayout>

我不想使用任何库.任何帮助都非常感谢.
提前致谢.

解决方法

您可以使用ViewPager和PagerAdapter执行此操作.您的PagerAdapter将有两个页面:“主”全宽视图和第二个包含操作按钮的视图.

关键是覆盖适配器中的getPageWidth()并为主视图返回1.0,为第二个视图返回一些分数.通过将视图的所需宽度除以ViewPager的宽度来计算分数.

用户滑动时,第二个视图无法占据整个屏幕,因此第一个视图仍然部分显示,就像在动画图像中一样.

使用ViewHolder引用ViewPager和引用PagerAdapter的ViewPager,您甚至可以将适配器与ViewPagers一起回收.

猜你在找的Android相关文章