TabLayout 显示更多 拖拽排序























#实现思路

点击”更多”显示包含RecyclerViewPopupWindow,只要让RecyclerView选中item的position和ViewPager的currentItem对应就好了;当RecyclerView拖拽排序之后只要控制好当前选中的item的position的逻辑就好了。

不管怎么拖拽排序,只要当前选中的item的position没有变,则排序之后选中的position还是原position;如果排序之后当前选中的item的position发生变化,则根据具体情况+1-1;如果当前拖拽的是选中的item,则拖拽排序之后当前选中item的position就是拖拽之后的position。

Viewpager切换的时候,相应改变 RecyclerView适配器中记录的当前选中的item的值mCurrentItem,并更新适配器。

1
2
3
4
5
@Override
public void onPageSelected(int position) {
mRVAdapter.setCurrentItem(position);
mRVAdapter.notifyDataSetChanged();
}

继承ItemTouchHelper.Callback重写其方法,处理拖拽的效果,并让适配器实现OnItemMoveCallback接口,把拖拽的相关逻辑放到适配器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class TabItemTouchCallback extends ItemTouchHelper.Callback {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
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 if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
final int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
} else {
return 0;
}
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();
if (mOnItemMoveCallback != null) {
mOnItemMoveCallback.onMove(fromPosition, toPosition);
}
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
if (mOnItemMoveCallback != null) {
mOnItemMoveCallback.onSwiped(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) {
viewHolder.itemView.setTranslationX(dX);
} else {
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
mOnItemMoveCallback.onFinished();
}
private OnItemMoveCallback mOnItemMoveCallback;
public void setOnItemMoveCallback(OnItemMoveCallback callback) {
mOnItemMoveCallback = callback;
}
public interface OnItemMoveCallback {
void onMove(int fromPosition, int toPosition);
void onSwiped(int position);
void onFinished();
}
}

重点看onMove方法:

1
2
3
4
5
public abstract int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder);
public abstract boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder target);
public abstract void onSwiped(ViewHolder viewHolder, int direction);
public void onSelectedChanged(ViewHolder viewHolder, int actionState) {}
public void clearView(RecyclerView recyclerView, ViewHolder viewHolder) {}