- LinearLayoutManager效果图:
滑动排序实现的方式,是通过继承ItemTouchHelper.Callback 重写其的方法,而在这些方法中需要通过接口获取:
public interface OnItemMoveListener {
void onItemMove(int fromPosition, int toPosition);
void onItemDismiss(int position);
}
So 我实例了以上的接口为了方便在adapter中使用的时候处理我需要做的操作。
接下来直接上ItemTouchHelper.Callback,我分别做了GridLayoutManager和LinearLayoutManager的可拖滑动方向以及侧滑删除判断
public class MoveItem extends ItemTouchHelper.Callback {
private OnItemMoveListener adapter;//因为要在adaper中使用所以直接赋值此名
public MoveItem(OnItemMoveListener adapter) {
this.adapter = adapter;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//主要设置屏幕中Item可拖动的方向,以及侧滑的方向。
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
return makeMovementFlags(dragFlags, swipeFlags);
} else if (layoutManager instanceof LinearLayoutManager) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
if (linearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL) {
int dragFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
int swipeFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
return makeMovementFlags(dragFlags, swipeFlags);
} else {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
return makeMovementFlags(dragFlags, swipeFlags);
}
}
return makeMovementFlags(0, 0);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
adapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());//移动
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
adapter.onItemDismiss(viewHolder.getAdapterPosition());//侧滑
}
@Override
public boolean isLongPressDragEnabled() {
return true;//必须为true才支持
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;//必须为true才支持
}
}
接口的方式也是为了进行传值,接下来是adapter中的操作。为adapter实现接口,重写方法
public class RecyclerMoveAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements
OnItemMoveListener {
private List<String> list;
private Context mContext;
private LayoutInflater inflater;
public RecyclerMoveAdapter(Context mContext) {
this.mContext = mContext;
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setData(List<String> list) {
this.list = list;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_recycler_move, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
MyViewHolder myViewHolder = (MyViewHolder) holder;
myViewHolder.text.setText(list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public void onItemMove(int fromPosition, int toPosition) {
Collections.swap(list, fromPosition, toPosition);
notifyItemMoved(fromPosition, toPosition);
}
@Override
public void onItemDismiss(int position) {
list.remove(position);
notifyDataSetChanged();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView text;
public MyViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
}
}
}
adapter实现接口,并进行了item顺序的调换以及即时刷新。
使用
private RecyclerView recycler;
private List<String> recyclerData = new ArrayList<>();
private RecyclerMoveAdapter recyclerMoveAdapter;
private MoveItem moveItem;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_banner);
initView();
initData();
}
public void initView() {
recycler = (RecyclerView) findViewById(R.id.recycler);
recyclerMoveAdapter = new RecyclerMoveAdapter(this);
moveItem = new MoveItem(recyclerMoveAdapter);
public void initData() {
for (int i = 0; i < 20; i++) {
recyclerData.add(i + "");
}
//在itemTouch里面已经进行了布局管理判断。
//GridLayoutManager layoutManager = new GridLayoutManager(mActivity, 3);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recycler.setLayoutManager(linearLayoutManager);
recycler.setHasFixedSize(true);
recycler.setAdapter(recyclerMoveAdapter);
recyclerMoveAdapter.setData(recyclerData);
ItemTouchHelper helper = new ItemTouchHelper(moveItem);
helper.attachToRecyclerView(recycler);
}