代码来源于 代码家 的 AndroidSwipeLayout 库 (https://github.com/daimajia/AndroidSwipeLayout) 开源协议: MIT License 仅保留侧滑删除功能, 其余代码全部删除
implementation 'me.luzhuo.android:library_swipe_layout:1.0.0'implementation 'androidx.appcompat:appcompat:1.3.0'implementation 'com.google.android.material:material:1.2.1'
1. 使用案例
```java
public class RecyclerViewExample extends Activity {
private RecyclerView recyclerView;private RecyclerView.Adapter mAdapter;private ArrayList<String> mDataSet;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.recyclerview);recyclerView = (RecyclerView) findViewById(R.id.recycler_view);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"};mDataSet = new ArrayList<String>(Arrays.asList(adapterData));recyclerView.setLayoutManager(new LinearLayoutManager(this));mAdapter = new RecyclerViewAdapter(mDataSet);((RecyclerViewAdapter) mAdapter).setMode(Attributes.Mode.Single);recyclerView.setAdapter(mAdapter);}
}
```javapublic class RecyclerViewAdapter extends RecyclerSwipeAdapter<RecyclerViewAdapter.SimpleViewHolder> {private Context context;private ArrayList<String> mDataset;public RecyclerViewAdapter(ArrayList<String> objects) {this.mDataset = objects;}@Overridepublic SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {this.context = parent.getContext();return new SimpleViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false));}@Overridepublic void onBindViewHolder(final SimpleViewHolder viewHolder, final int position) {((SimpleViewHolder) viewHolder).bindData(mDataset.get(position));}@Overridepublic int getItemCount() {return mDataset.size();}@Overridepublic int getSwipeLayoutResourceId(int position) {return R.id.swipe;}public class SimpleViewHolder extends RecyclerView.ViewHolder {private SwipeLayout swipeLayout;private TextView textViewPos;private TextView textViewData;private Button buttonDelete;public SimpleViewHolder(View itemView) {super(itemView);swipeLayout = (SwipeLayout) itemView.findViewById(R.id.swipe);textViewPos = (TextView) itemView.findViewById(R.id.position);textViewData = (TextView) itemView.findViewById(R.id.text_data);buttonDelete = (Button) itemView.findViewById(R.id.delete);itemView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Log.d(getClass().getSimpleName(), "onItemSelected: " + textViewData.getText().toString());Toast.makeText(view.getContext(), "onItemSelected: " + textViewData.getText().toString(), Toast.LENGTH_SHORT).show();}});}public void bindData(String item) {swipeLayout.setShowMode(SwipeLayout.ShowMode.LayDown);swipeLayout.addSwipeListener(new SimpleSwipeListener() {@Overridepublic void onOpen(SwipeLayout layout) {}});swipeLayout.setOnDoubleClickListener(new SwipeLayout.DoubleClickListener() {@Overridepublic void onDoubleClick(SwipeLayout layout, boolean surface) {Toast.makeText(context, "DoubleClick", Toast.LENGTH_SHORT).show();}});buttonDelete.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {mItemManger.removeShownLayouts(swipeLayout);mDataset.remove(getLayoutPosition());notifyItemRemoved(getLayoutPosition());notifyItemRangeChanged(getLayoutPosition(), mDataset.size());mItemManger.closeAllItems();Toast.makeText(view.getContext(), "Deleted " + textViewData.getText().toString() + "!", Toast.LENGTH_SHORT).show();}});textViewPos.setText((getLayoutPosition() + 1) + ".");textViewData.setText(item);mItemManger.bind(itemView, getLayoutPosition());}}}
<?xml version="1.0" encoding="utf-8" ?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"><!-- 隐藏的布局 --><com.daimajia.swipe.SwipeLayout xmlns:swipe="http://schemas.android.com/apk/res-auto"android:id="@+id/swipe"android:layout_width="match_parent"android:layout_height="wrap_content"swipe:leftEdgeSwipeOffset="0dp"swipe:rightEdgeSwipeOffset="0dp"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="80dp"android:background="#FF5534"android:gravity="center"android:tag="Bottom3"android:weightSum="10"><ImageViewandroid:id="@+id/trash"android:layout_width="27dp"android:layout_height="30dp"android:layout_weight="1"android:src="@drawable/trash" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="5"android:text="Delete Item?"android:textColor="#fff"android:textSize="17sp" /><Buttonandroid:id="@+id/delete"android:layout_width="0dp"android:layout_height="40dp"android:layout_weight="4"android:background="#ffffff"android:text="Yes,Delete"android:textColor="#FF5534" /></LinearLayout><!-- 正常的布局 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/item_selector"android:elevation="5dp"android:padding="10dp"><TextViewandroid:id="@+id/position"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/text_data"android:layout_width="match_parent"android:layout_height="match_parent"android:tag="Hover"/></LinearLayout></com.daimajia.swipe.SwipeLayout></LinearLayout>
