Android开发RecyclerView左右侧滑

释放双眼,带上耳机,听听看~!

recyclerview在Android中应用的也很多了。废话不说多,先上图:

1.向右侧滑
这里写图片描述
2.向左侧滑
这里写图片描述
下面开始讨论一下关于这样的效果实怎么实现的:
1.首先创建一个侧滑的接口:

public interface ItemTouchHelperAdapter {
/**
* Called when an item has been dragged far enough to trigger a move. This is called every time
* an item is shifted, and <strong>not</strong> at the end of a "drop" event.<br/>
* <br/>
* Implementations should call {@link RecyclerView.Adapter#notifyItemMoved(int, int)} after
* adjusting the underlying data to reflect this move.
*
* @param fromPosition The start position of the moved item.
* @param toPosition   Then resolved position of the moved item.
* @return True if the item was moved to the new adapter position.
*
* @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
* @see RecyclerView.ViewHolder#getAdapterPosition()
*/
boolean onItemMove(int fromPosition, int toPosition);
/**
* Called when an item has been dismissed by a swipe.<br/>
* <br/>
* Implementations should call {@link RecyclerView.Adapter#notifyItemRemoved(int)} after
* adjusting the underlying data to reflect this removal.
*
* @param position The position of the item dismissed.
*
* @see RecyclerView#getAdapterPositionFor(RecyclerView.ViewHolder)
* @see RecyclerView.ViewHolder#getAdapterPosition()
*/
void onItemDismiss(int position);

}

2.主要的实现类:

public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {
public static final float ALPHA_FULL = 1.0f;
private final ItemTouchHelperAdapter mAdapter;
private int socllType = 0;
public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter, int scollType) {
mAdapter = adapter;
this.socllType = scollType;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
// Set movement flags based on the layout manager
if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
final int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
} else {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
if (socllType == 0) {
return makeMovementFlags(dragFlags, swipeFlags);
} else if (socllType == 1) {
return makeMovementFlags(0, swipeFlags);
} else if (socllType == 2) {
return makeMovementFlags(dragFlags, 0);
} else {
return makeMovementFlags(0, 0);
}
}
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
if (source.getItemViewType() != target.getItemViewType()) {
return false;
}
// Notify the adapter of the move
mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {
// Notify the adapter of the dismissal
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// Fade out the view as it is swiped out of the parent's bounds
final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
viewHolder.itemView.setAlpha(alpha);
viewHolder.itemView.setTranslationX(dX);
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
// We only want the active item to change
if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
if (viewHolder instanceof ItemTouchHelperViewHolder) {
// Let the view holder know that this item is being moved or dragged
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemSelected();
}
}
super.onSelectedChanged(viewHolder, actionState);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setAlpha(ALPHA_FULL);
if (viewHolder instanceof ItemTouchHelperViewHolder) {
// Tell the view holder it's time to restore the idle state
ItemTouchHelperViewHolder itemViewHolder = (ItemTouchHelperViewHolder) viewHolder;
itemViewHolder.onItemClear();
}
}

}

3.callback中的另外的一个接口:

public interface ItemTouchHelperViewHolder {
/**
* Called when the {@link ItemTouchHelper} first registers an item as being moved or swiped.
* Implementations should update the item view to indicate it's active state.
*/
void onItemSelected();
/**
* Called when the {@link ItemTouchHelper} has completed the move or swipe, and the active item
* state should be cleared.
*/
void onItemClear();
}

4.使用,实现这个接口

public class SingleNumOutAdapter extends BaseRecycleAdapter<Order> implements ItemTouchHelperAdapter {
//定义这个接口
private OnSingleNumOutLinsenter onSingleNumOutLinsenter;
public void setOnSingleNumOutLinsenter(OnSingleNumOutLinsenter onSingleNumOutLinsenter) {
this.onSingleNumOutLinsenter = onSingleNumOutLinsenter;
}
//自己写个接口
public interface OnSingleNumOutLinsenter {
void onRemove(int position);
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
return false;
}
@Override
public void onItemDismiss(int position) {
if (onSingleNumOutLinsenter != null) {
onSingleNumOutLinsenter.onRemove(position);
}
}
}

5.Activity的使用,在onCreate()中

    ItemTouchHelper.Callback helperCallback = new SimpleItemTouchHelperCallback(adapter, 1);
ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(helperCallback);
mItemTouchHelper.attachToRecyclerView(rvSingleOrder);
recyclerView.setAdapter(adapter);
adapter.setOnSingleNumOutLinsenter(new SingleNumOutAdapter.OnSingleNumOutLinsenter() {
@Override
public void onRemove(int position) {
}
});

——————————— 骚气的分割线——————————————————————————————————
这样就实现了这个侧滑的效果了,打完收工,嘿嘿

人已赞赏
Android文章

Android开发实现左右两侧两个RecyclerView互相联动,右侧是网格的布局,写出最流畅的列表

2020-2-1 9:55:17

Android文章

Android开发WaveLoadingView画图

2020-2-1 10:35:16

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索