代碼直接copy 需要compile
butterknife 以及 commonAdapter
效果圖
layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_item_touch_helper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.iamchan.allfunction.ui.widget.ItemTouchHelperActivity">
<include layout="@layout/toolbar"></include>
<RelativeLayout
android:layout_marginRight="12dp"
android:layout_marginLeft="12dp"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:textSize="15sp"
android:layout_centerVertical="true"
android:id="@+id/name"
android:text="我的板塊"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/edit_btn"
android:textColor="@color/red"
android:textSize="12sp"
android:background="@drawable/itemtouchhelperactivity_bianji"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:text="編輯"
android:layout_width="55dp"
android:layout_height="30dp" />
<TextView
android:visibility="invisible"
android:textSize="13sp"
android:layout_marginLeft="10dp"
android:id="@+id/an"
android:text="長按可以排序"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/name" />
</RelativeLayout>
<View
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@color/gray"
android:layout_width="match_parent"
android:layout_height="1dp"></View>
<android.support.v7.widget.RecyclerView
android:overScrollMode="never"
android:scrollbars="none"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="5dp"
android:id="@+id/recyc"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_marginTop="5dp"
android:paddingLeft="12dp"
android:paddingRight="12dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="35dp">
<TextView
android:textSize="15sp"
android:layout_gravity="center_vertical"
android:text="板塊推薦"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@color/gray"
android:layout_width="match_parent"
android:layout_height="1dp"></View>
<android.support.v7.widget.RecyclerView
android:overScrollMode="never"
android:scrollbars="none"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="5dp"
android:id="@+id/recyc2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/white"
android:gravity="center"
android:text="頭條" />
<ImageView
android:id="@+id/x"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentRight="true"
android:src="@mipmap/x"
android:visibility="invisible" />
</RelativeLayout>
</LinearLayout>
java:
@BindView(R.id.iv_imgLeft)
ImageView ivImgLeft;
@BindView(R.id.tv_title)
TextView tvTitle;
@BindView(R.id.edit_btn)
Button editBtn;
@BindView(R.id.recyc)
RecyclerView recyc;
@BindView(R.id.recyc2)
RecyclerView recyc2;
@BindView(R.id.an)
TextView an;
private int btnTag;
private List<String> recycList;
private List<String> recyc2List;
private CommonAdapter comm;
private CommonAdapter comm2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_touch_helper_test);
ButterKnife.bind(this);
tvTitle.setText("itemTouchHelper");
ivImgLeft.setImageResource(R.drawable.left);
ivImgLeft.setVisibility(View.VISIBLE);
initData();
initRecyc();
}
private void initData() {
recycList = new ArrayList<>();
recycList.add("頭條");
recycList.add("科技");
recycList.add("市場");
recycList.add("項目");
recyc2List = new ArrayList<>();
recyc2List.add("行情");
recyc2List.add("曝光");
recyc2List.add("行情");
recyc2List.add("海外");
recyc2List.add("政策");
recyc2List.add("專家");
recyc2List.add("活動");
recyc2List.add("專題");
}
private void initRecyc() {
comm=new CommonAdapter(this,R.layout.item_itemtouchhelperactivity_industry,recycList) {
@Override
protected void convert(final ViewHolder holder, Object o, final int position) {
holder.setText(R.id.name,recycList.get(position));
/*
* 右上角標的顯示與隱藏
* */
if(btnTag==1){
holder.getView(R.id.x).setVisibility(View.VISIBLE);
}else{
holder.getView(R.id.x).setVisibility(View.GONE);
}
/*
* item第0條數據不需要顯示角標
* */
if(position==0){
holder.getView(R.id.x).setVisibility(View.GONE);
}
holder.getConvertView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(btnTag==1) {
if(position!=0) {
/*
* 這裏面實現交換數據
* */
recyc2List.add(recycList.get(holder.getAdapterPosition()));
comm2.notifyDataSetChanged();
recycList.remove(holder.getAdapterPosition());
comm.notifyDataSetChanged();
}
}
}
});
/*
* 長按操作
* */
holder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
editBtn.setText("完成");
btnTag = 1;
an.setVisibility(View.VISIBLE);
comm.notifyDataSetChanged();
comm2.notifyDataSetChanged();
return true;
}
});
}
};
recyc.setAdapter(comm);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
recyc.setLayoutManager(gridLayoutManager);
comm2=new CommonAdapter(this,R.layout.item_itemtouchhelperactivity_industry,recyc2List) {
@Override
protected void convert(final ViewHolder holder, Object o, int position) {
holder.setText(R.id.name,recyc2List.get(position));
if(btnTag==1){
holder.getView(R.id.x).setVisibility(View.VISIBLE);
}else{
holder.getView(R.id.x).setVisibility(View.GONE);
}
holder.getConvertView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(btnTag==1) {
/*
* 這裏面實現交換數據
* */
recycList.add(recyc2List.get(holder.getAdapterPosition()));
comm.notifyDataSetChanged();
recyc2List.remove(holder.getAdapterPosition());
comm2.notifyDataSetChanged();
}
}
});
}
};
recyc2.setAdapter(comm2);
GridLayoutManager gridLayoutManager2 = new GridLayoutManager(this, 4);
recyc2.setLayoutManager(gridLayoutManager2);
/*
* 使用itemTouchCallBack的時候
* */
ItemTouchCallBack itemTouchCallBack=new ItemTouchCallBack(recycList,comm);
ItemTouchHelper itemTouchHelper=new ItemTouchHelper(itemTouchCallBack);
itemTouchHelper.attachToRecyclerView(recyc);
}
@OnClick({R.id.iv_imgLeft, R.id.edit_btn})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.iv_imgLeft:
finish();
break;
case R.id.edit_btn:
if(btnTag==0) {
an.setVisibility(View.VISIBLE);
editBtn.setText("完成");
btnTag=1;
comm.notifyDataSetChanged();
comm2.notifyDataSetChanged();
}else{
an.setVisibility(View.GONE);
editBtn.setText("編輯");
btnTag=0;
comm.notifyDataSetChanged();
comm2.notifyDataSetChanged();
}
break;
}
}
itemTouchCallBack:
public class ItemTouchCallBack extends ItemTouchHelper.Callback {
private List<String> strings;
private CommonAdapter recycAdapter;
/*
*構造方法
* */
public ItemTouchCallBack(List<String> strings, CommonAdapter recycAdapter) {
this.strings = strings;
this.recycAdapter = recycAdapter;
}
/*
* 1.Should return a composite flag which defines the enabled move directions in each state(idle, swiping, dragging)
* 應該返回一個複合標誌,該標誌定義了每個狀態中啓用的移動方向(空閒、滑動、拖動)
* 2.Instead of composing this flag manually, you can use {@link #makeMovementFlags(int,int)} or {@link #makeFlag(int, int)}.
* 您可以使用{@link #makeMovementFlags(int,int)}或{@link #makeFlag(int, int)}。
* 3.This flag is composed of 3 sets of 8 bits, where first 8 bits are for IDLE state, next 8 bits are for SWIPE state and third 8 bits are for DRAG state.
*他的標誌由3組8位組成,前8位表示空閒狀態,下一個8位爲滑動狀態,第三個爲拖動狀態。
* 4.makeMovementFlags 大體意思就是傳進去兩個值 第一個值爲拖拽的方向 第二個值爲滑動的方向
* 源碼
* Convenience method to create movement flags.
* <p>
* For instance, if you want to let your items be drag & dropped vertically and swiped
* left to be dismissed, you can call this method with:
* <code>makeMovementFlags(UP | DOWN, LEFT);</code>
*
* @param dragFlags The directions in which the item can be dragged.
* @param swipeFlags The directions in which the item can be swiped.
* @return Returns an integer composed of the given drag and swipe flags.
public static int makeMovementFlags(int dragFlags, int swipeFlags) {
return makeFlag(ACTION_STATE_IDLE, swipeFlags | dragFlags)
| makeFlag(ACTION_STATE_SWIPE, swipeFlags)
| makeFlag(ACTION_STATE_DRAG, dragFlags);
}*/
/*
*
* 這個方法主要是 確定一個標識 這個標識能讓你的recyclerView實現拖拽和滑動的效果 這裏面主要是實現拖拽的效果
* 這個方法返回的是一個複合標誌 可以使用makeMovementFlags(int,int)或makeFlag(int, int)作爲返回對象
* 上面是源碼中看到的
*
* */
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN;
return makeMovementFlags(dragFlags, 0);
}
/*
* 1.Called when ItemTouchHelper wants to move the dragged item from its old position to the new position.
* 當ItemTouchHelper希望將拖動的項目從原來的位置移動到新位置
* 2.If this method returns true, ItemTouchHelper assumes {@code viewHolder} has been moved to the adapter position of {@code target} ViewHolder
* 如果該方法返回true, ItemTouchHelper假設{@code viewHolder}已被移動到{@code target} ViewHolder的適配器位置 也就是true能移動 false不能移動
* */
/*
*
* 這個方法主要是從舊位置移動到一個新位置 下面這個方法就是移動位置交換數據
* 上面是源碼解釋
*
* */
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();//從哪個位置移動的
int toPosition = target.getAdapterPosition();//移動到哪個位置
if (fromPosition != 0 && toPosition != 0) {
if (fromPosition < toPosition) {//判斷交換數據位置
for (int i = fromPosition; i < toPosition; i++) {
Collections.swap(strings, i, i + 1);
}
} else {
for (int i = fromPosition; i > toPosition; i--) {
Collections.swap(strings, i, i - 1);
}
}
recycAdapter.notifyItemMoved(fromPosition, toPosition);
}
return true;
}
/*
* Called when a ViewHolder is swiped by the user.
* 這個方法滑動時調用
* */
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
}
/*
*
* 1.Called when the ViewHolder swiped or dragged by the ItemTouchHelper is changed.
* 當被ItemTouchHelper滑動或拖動的ViewHolder更改時調用
* */
/*
* 這個方法主要是實現開始拖拽的時候實現放大效果
* actionState 有三種 ACTION_STATE_IDLE,ACTION_STATE_SWIPE or ACTION_STATE_DRAG
* */
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
viewHolder.itemView.setScaleX(1.2f);
viewHolder.itemView.setScaleY(1.2f);
}
}
/*
*Called by the ItemTouchHelper when the user interaction with an element is over and it also completed its animation.
* 當用戶與元素的交互結束並結束時,ItemTouchHelper調用它也完成了動畫
* */
/*
* 這個方法結束時調用 對應上面的onSelectedChanged方法
* */
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setScaleX(1.0f);
viewHolder.itemView.setScaleY(1.0f);
}
/*
* 是否啓用長按脫拽
* */
@Override
public boolean isLongPressDragEnabled() {
return true;
}
/*
* 是否啓用滑動
* */
@Override
public boolean isItemViewSwipeEnabled() {
return false;
}
}