感谢大神,赐给我力量
implementation 'com.github.mcxtzhang:SwipeDelMenuLayout:V1.3.0'
布局文件如下:
<com.mcxtzhang.swipemenulib.SwipeMenuLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="98px"
android:clickable="true"
app:ios="false"
app:leftSwipe="true"
app:swipeEnable="true">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="48dp">
<TextView
android:layout_width="wrap_content"
android:text="正常布局内容"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/btnDelete"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="#ff4a57"
android:text="删除"
android:textColor="@android:color/white"/>
</com.mcxtzhang.swipemenulib.SwipeMenuLayout>
adapter如下
class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
ViewHolder holder = null;
private onSwipeListener mOnSwipeListener;
public void setOnDelListener(onSwipeListener mOnDelListener) {
this.mOnSwipeListener = mOnDelListener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(GoldMajorListActivity2.this).inflate(R.layout.item_gold2, parent, false);
holder = new ViewHolder(view);
holder.number = view.findViewById(R.id.tv_number);
holder.btnDelete = view.findViewById(R.id.btnDelete);
return holder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
CandidateWishsBean candidateWishsBean = candidateWishs.get(position);
holder.number.setText(1 + position + "");
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (null != mOnSwipeListener) {
//且如果想让侧滑菜单同时关闭,需要同时调用
//((SwipeMenuLayout) holder.itemView).quickClose();
mOnSwipeListener.onDel(holder.itemView, position);
}
}
});
}
@Override
public int getItemCount() {
return candidateWishs == null ? 0 : candidateWishs.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView number;
Button btnDelete;
public ViewHolder(View itemView) {
super(itemView);
}
}
}
activity调用
adapter.setOnDelListener(new onSwipeListener() {
@Override
public void onDel(View view, int pos) {
SwipeMenuLayout swipeMenuLayout = (SwipeMenuLayout) view;
swipeMenuLayout.quickClose();
Log.e("syw", "pos:" + pos);
}
});