代码来源于 代码家 的 AndroidSwipeLayout 库 (https://github.com/daimajia/AndroidSwipeLayout) 开源协议: MIT License 仅保留侧滑删除功能, 其余代码全部删除

  1. implementation 'me.luzhuo.android:library_swipe_layout:1.0.0'
  2. implementation 'androidx.appcompat:appcompat:1.3.0'
  3. implementation 'com.google.android.material:material:1.2.1'

1. 使用案例

SVID_20210728_001047_1.mp4```java public class RecyclerViewExample extends Activity {

  1. private RecyclerView recyclerView;
  2. private RecyclerView.Adapter mAdapter;
  3. private ArrayList<String> mDataSet;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.recyclerview);
  8. recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
  9. String[] adapterData = new String[]{"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"};
  10. mDataSet = new ArrayList<String>(Arrays.asList(adapterData));
  11. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  12. mAdapter = new RecyclerViewAdapter(mDataSet);
  13. ((RecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);
  14. recyclerView.setAdapter(mAdapter);
  15. }

}

  1. ```java
  2. public class RecyclerViewAdapter extends RecyclerSwipeAdapter<RecyclerViewAdapter.SimpleViewHolder> {
  3. private Context context;
  4. private ArrayList<String> mDataset;
  5. public RecyclerViewAdapter(ArrayList<String> objects) {
  6. this.mDataset = objects;
  7. }
  8. @Override
  9. public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  10. this.context = parent.getContext();
  11. return new SimpleViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false));
  12. }
  13. @Override
  14. public void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {
  15. ((SimpleViewHolder) viewHolder).bindData(mDataset.get(position));
  16. }
  17. @Override
  18. public int getItemCount() {
  19. return mDataset.size();
  20. }
  21. @Override
  22. public int getSwipeLayoutResourceId(int position) {
  23. return R.id.swipe;
  24. }
  25. public class SimpleViewHolder extends RecyclerView.ViewHolder {
  26. private SwipeLayout swipeLayout;
  27. private TextView textViewPos;
  28. private TextView textViewData;
  29. private Button buttonDelete;
  30. public SimpleViewHolder(View itemView) {
  31. super(itemView);
  32. swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);
  33. textViewPos = (TextView) itemView.findViewById(R.id.position);
  34. textViewData = (TextView) itemView.findViewById(R.id.text_data);
  35. buttonDelete = (Button) itemView.findViewById(R.id.delete);
  36. itemView.setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View view) {
  39. Log.d(getClass().getSimpleName(), "onItemSelected: " + textViewData.getText().toString());
  40. Toast.makeText(view.getContext(), "onItemSelected: " + textViewData.getText().toString(), Toast.LENGTH_SHORT).show();
  41. }
  42. });
  43. }
  44. public void bindData(String item) {
  45. swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);
  46. swipeLayout.addSwipeListener(new SimpleSwipeListener() {
  47. @Override
  48. public void onOpen(SwipeLayout layout) {
  49. }
  50. });
  51. swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {
  52. @Override
  53. public void onDoubleClick(SwipeLayout layout, boolean surface) {
  54. Toast.makeText(context, "DoubleClick", Toast.LENGTH_SHORT).show();
  55. }
  56. });
  57. buttonDelete.setOnClickListener(new View.OnClickListener() {
  58. @Override
  59. public void onClick(View view) {
  60. mItemManger.removeShownLayouts(swipeLayout);
  61. mDataset.remove(getLayoutPosition());
  62. notifyItemRemoved(getLayoutPosition());
  63. notifyItemRangeChanged(getLayoutPosition(), mDataset.size());
  64. mItemManger.closeAllItems();
  65. Toast.makeText(view.getContext(), "Deleted " + textViewData.getText().toString() + "!", Toast.LENGTH_SHORT).show();
  66. }
  67. });
  68. textViewPos.setText((getLayoutPosition() + 1) + ".");
  69. textViewData.setText(item);
  70. mItemManger.bind(itemView, getLayoutPosition());
  71. }
  72. }
  73. }
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <!-- 隐藏的布局 -->
  6. <com.daimajia.swipe.SwipeLayout xmlns:swipe="http://schemas.android.com/apk/res-auto"
  7. android:id="@+id/swipe"
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. swipe:leftEdgeSwipeOffset="0dp"
  11. swipe:rightEdgeSwipeOffset="0dp">
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="80dp"
  15. android:background="#FF5534"
  16. android:gravity="center"
  17. android:tag="Bottom3"
  18. android:weightSum="10">
  19. <ImageView
  20. android:id="@+id/trash"
  21. android:layout_width="27dp"
  22. android:layout_height="30dp"
  23. android:layout_weight="1"
  24. android:src="@drawable/trash" />
  25. <TextView
  26. android:layout_width="0dp"
  27. android:layout_height="wrap_content"
  28. android:layout_weight="5"
  29. android:text="Delete Item?"
  30. android:textColor="#fff"
  31. android:textSize="17sp" />
  32. <Button
  33. android:id="@+id/delete"
  34. android:layout_width="0dp"
  35. android:layout_height="40dp"
  36. android:layout_weight="4"
  37. android:background="#ffffff"
  38. android:text="Yes,Delete"
  39. android:textColor="#FF5534" />
  40. </LinearLayout>
  41. <!-- 正常的布局 -->
  42. <LinearLayout
  43. android:layout_width="match_parent"
  44. android:layout_height="match_parent"
  45. android:background="@drawable/item_selector"
  46. android:elevation="5dp"
  47. android:padding="10dp">
  48. <TextView
  49. android:id="@+id/position"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content" />
  52. <TextView
  53. android:id="@+id/text_data"
  54. android:layout_width="match_parent"
  55. android:layout_height="match_parent"
  56. android:tag="Hover"/>
  57. </LinearLayout>
  58. </com.daimajia.swipe.SwipeLayout>
  59. </LinearLayout>