本文实例为大家分享了如何使用ItemTouchHelper实现侧滑删除和拖拽的具体代码,供大家参考,具体内容如下
1. 定义一个简单bean类:
代码语言:javascript复制public class ImgText {
public int resId;
public String des;
}
2. 实现一个RecyclerView.Adapter
代码语言:javascript复制public class SwipeRecyclerAdapter extends RecyclerView.Adapter<SwipeRecyclerAdapter.MyHolder {
private List<ImgText mList;
public SwipeRecyclerAdapter(List<ImgText list) {
mList = list;
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_swipe_recycler, parent, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
ImgText imgText = mList.get(position);
holder.img.setImageResource(imgText.resId);
holder.des.setText(imgText.des);
}
@Override
public int getItemCount() {
return mList.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
private ImageView img;
private TextView des;
public MyHolder(View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.img);
des = (TextView) itemView.findViewById(R.id.text);
}
}
}
3.绘制分割线:
代码语言:javascript复制public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{android.R.attr.listDivider};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i ) {
final View child = parent.getChildAt(i);
RecyclerView v = new RecyclerView(
parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() params.bottomMargin;
final int bottom = top mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i ) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() params.rightMargin;
final int right = left mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition,
RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
4. 实现ItemTouchHelper.Callback接口:
代码语言:javascript复制public class SwipeCallback<T extends ItemTouchHelper.Callback {
private RecyclerView.Adapter<? extends RecyclerView.ViewHolder mAdapter;
private List<T mList;
public SwipeCallback(RecyclerView.Adapter adapter, List<T list) {
mAdapter = adapter;
mList = list;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlag=ItemTouchHelper.UP|ItemTouchHelper.DOWN;
int swipeFlags=ItemTouchHelper.END|ItemTouchHelper.START;
return makeMovementFlags(dragFlag,swipeFlags);
}
//用于移动item的位置
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
Collections.swap(mList, viewHolder.getAdapterPosition(), target.getAdapterPosition());
mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
//一般用于滑动删除
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mList.remove(viewHolder.getAdapterPosition());
mAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
}
5.编写侧滑删除和拖拽Activity
代码语言:javascript复制public class SwipeRecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private List<ImgText mList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_recycler);
mRecyclerView= (RecyclerView) findViewById(R.id.recycler);
mList=new ArrayList< ();
for (int i = 0; i < 20; i ) {
ImgText it= new ImgText();
it.resId=R.mipmap.ic_launcher;
it.des=i "侧滑删除文字";
mList.add(it);
}
final SwipeRecyclerAdapter adapter= new SwipeRecyclerAdapter(mList);
RecyclerView.LayoutManager manager= new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
mRecyclerView.setAdapter(adapter);
ItemTouchHelper swipeItemHelper= new ItemTouchHelper(new SwipeCallback< (adapter,mList));
swipeItemHelper.attachToRecyclerView(mRecyclerView);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。