既然它是一個列表,能夠拖動,又須要動畫,那這裏就使用recyclerView+ItemTouchHelper來實現整個頁面的效果java
這裏須要新建一個類繼承ItemTouchHelper.Callback,並重寫其中的幾個方法git
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
}
複製代碼
但已選頻道這裏是有一個或多個固定頻道的,並且咱們是一個recyclerView去實現整個頁面,tab及tab下面的item都是不能拖動的,因此還須要處理一下:github
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//固定位置及tab下面的channel不能拖動
if (viewHolder.getLayoutPosition() < mAdapter.getFixSize() + 1 || viewHolder.getLayoutPosition() > mAdapter.getSelectedSize()) {
return makeMovementFlags(0, 0);
}
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
int swipeFlags = 0;
return makeMovementFlags(dragFlags, swipeFlags);
}
複製代碼
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition(); //拖動的position
int toPosition = target.getAdapterPosition(); //釋放的position
//固定位置及tab下面的channel不能拖動
if (toPosition < mAdapter.getFixSize() + 1 || toPosition > mAdapter.getSelectedSize())
return false;
mAdapter.itemMove(fromPosition, toPosition);
return true;
}
複製代碼
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if(actionState==ACTION_STATE_DRAG){
//長按時調用
ChannelAdapter.ChannelHolder holder= (ChannelAdapter.ChannelHolder) viewHolder;
holder.name.setBackgroundColor(Color.parseColor("#FDFDFE"));
holder.delete.setVisibility(View.GONE);
holder.name.setElevation(5f);
}
}
複製代碼
這裏須要注意一下,不能當actionState爲ACTION_STATE_IDLE時重置item的狀態,viewHolder有可能爲空指針bash
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
ChannelAdapter.ChannelHolder holder= (ChannelAdapter.ChannelHolder) viewHolder;
holder.name.setBackgroundColor(Color.parseColor("#f0f0f0"));
holder.name.setElevation(0f);
holder.delete.setVisibility(View.VISIBLE);
}
複製代碼
虛線方框須要繪製,只能找跟draw相關的方法羅ide
public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
if (dX != 0 && dY != 0 || isCurrentlyActive) {
//長按拖拽時底部繪製一個虛線矩形
c.drawRect(viewHolder.itemView.getLeft(),viewHolder.itemView.getTop()-mPadding,viewHolder.itemView.getRight(),viewHolder.itemView.getBottom(),mPaint);
}
}
複製代碼
多佈局什麼的我想你們都懂的,就不細說了。tab這裏爲了方便直接使用了兩個textView實現。 着重說一下要注意的幾個地方吧:佈局
private void setChannel(final ChannelHolder holder, ChannelBean bean) {
final int position = holder.getLayoutPosition();
holder.name.setText(bean.getName());
holder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.getLayoutPosition() < selectedSize + 1) {
//tab上面的 點擊移除
removeFromSelected(holder);
} else {
//tab下面的 點擊添加到已選頻道
selectedSize++;
itemMove(holder.getLayoutPosition(), selectedSize);
notifyItemChanged(selectedSize);
if (onItemRangeChangeListener != null) {
onItemRangeChangeListener.refreshItemDecoration();
}
}
}
});
holder.name.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//返回true 防止長按拖拽事件跟點擊事件衝突
return true;
}
});
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeFromSelected(holder);
}
});
}
private void removeFromSelected(ChannelHolder holder) {
int position = holder.getLayoutPosition();
holder.delete.setVisibility(View.GONE);
ChannelBean bean = mList.get(position);
if ((isRecommend && bean.isRecommend()) || (!isRecommend && !bean.isRecommend())) {
//移除的頻道屬於當前tab顯示的頻道,直接調用系統的移除動畫
itemMove(position, selectedSize + 1);
notifyItemRangeChanged(selectedSize + 1, 1);
if (onItemRangeChangeListener != null) {
//若是設置了itemDecoration,必須調用recyclerView.invalidateItemDecorations(),不然間距會不對
onItemRangeChangeListener.refreshItemDecoration();
}
} else {
//不屬於當前tab顯示的頻道
removeAnimation(holder.itemView, isRecommend ? mRight : mLeft, mTabY, position);
}
selectedSize--;
}
void itemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition) {
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(mList, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(mList, i, i - 1);
}
}
notifyItemMoved(fromPosition, toPosition);
}
}
複製代碼
因爲使用了textView代替tab,因此會有一些計算用於藍色線條的位置改變。 動畫
上面只是我我的的實現方式,若是有更好的方式,能夠在下方留言。 最後,奉上源碼java版ui
kotlin版spa
kotlin版本的語法可能有點問題,畢竟 翻譯