RecyclerView拖动之ItemTouchHelper.Callback的使用

关于RecyclerView拖动效果

先看以下效果图


1.jpg
2.jpg

android给了一个官方的工具ItemTouchHelper.Callback

下面上代码,先说以下RecyclerView的使用

android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_list"/>

private RecyclerView rv_list;
private List date = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rv_list = findViewById(R.id.rv_list);
    for (int i = 0; i < 8; i++) {
        date.add(String.valueOf(i+1));
    }
    LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    ListAdapter adapter = new ListAdapter(date, this);
    rv_list.setLayoutManager(manager);
    rv_list.setAdapter(adapter);


}

适配器
public class ListAdapter extends RecyclerView.Adapter{

private final LayoutInflater inflater;
private List date;
private Context context;

public ListAdapter(List date, Context context) {
    this.date = date;
    this.context = context;
    inflater = LayoutInflater.from(context);
}

@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    return new ListViewHolder(inflater.inflate(R.layout.list_item,viewGroup,false));
}

@Override
public void onBindViewHolder(@NonNull ListViewHolder listViewHolder, int i) {
    if (!TextUtils.isEmpty(date.get(i))) {
        listViewHolder.tv.setText("条目"+i);
        if (i % 2 == 0) {
            listViewHolder.iv.setImageResource(R.mipmap.user_head_img2);
        }else {
            listViewHolder.iv.setImageResource(R.mipmap.user_head_img3);
        }
    }
}

@Override
public int getItemCount() {
    return date.size();
}



class ListViewHolder extends RecyclerView.ViewHolder{
    ImageView iv;
    TextView tv;
    public ListViewHolder(@NonNull View itemView) {
        super(itemView);
        iv = itemView.findViewById(R.id.iv);
        tv = itemView.findViewById(R.id.tv);
    }
}

}

好的。此时写完了。效果如图2;

下面开始拖动效果

自定义一个接口
ListItemTouchHelper
public interface ListItemTouchHelper {
void itemMove(int oldIndex,int newIndex);
void itemDelete(int index);
}

自定义回调

TouchHelperCallback

public class TouchHelperCallback extends ItemTouchHelper.Callback {
private ListAdapter adapter;

public TouchHelperCallback(ListAdapter adapter) {
    this.adapter = adapter;
}

@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
    int dragFlags = ItemTouchHelper.UP |ItemTouchHelper.DOWN;
    int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
    return makeMovementFlags(dragFlags,swipeFlags);
}

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
    adapter.itemMove(viewHolder.getAdapterPosition(),viewHolder1.getAdapterPosition());
    return false;
}

@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
    adapter.itemDelete(viewHolder.getAdapterPosition());
}

}

具体使用:

适配器实现接口ListItemTouchHelper

activity设置
TouchHelperCallback callback = new TouchHelperCallback(adapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(rv_list);

到此就完成了这个效果了;

下面上一下activity和adapter的完整代码。

private RecyclerView rv_list;
private List date = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rv_list = findViewById(R.id.rv_list);
    for (int i = 0; i < 8; i++) {
        date.add(String.valueOf(i+1));
    }
    LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    ListAdapter adapter = new ListAdapter(date, this);
    rv_list.setLayoutManager(manager);
    rv_list.setAdapter(adapter);
    TouchHelperCallback callback = new TouchHelperCallback(adapter);
    ItemTouchHelper helper = new ItemTouchHelper(callback);
    helper.attachToRecyclerView(rv_list);

}

public class ListAdapter extends RecyclerView.Adapterimplements ListItemTouchHelper {

private final LayoutInflater inflater;
private List date;
private Context context;

public ListAdapter(List date, Context context) {
    this.date = date;
    this.context = context;
    inflater = LayoutInflater.from(context);
}

@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    return new ListViewHolder(inflater.inflate(R.layout.list_item,viewGroup,false));
}

@Override
public void onBindViewHolder(@NonNull ListViewHolder listViewHolder, int i) {
    if (!TextUtils.isEmpty(date.get(i))) {
        listViewHolder.tv.setText("条目"+i);
        if (i % 2 == 0) {
            listViewHolder.iv.setImageResource(R.mipmap.user_head_img2);
        }else {
            listViewHolder.iv.setImageResource(R.mipmap.user_head_img3);
        }
    }
}

@Override
public int getItemCount() {
    return date.size();
}

@Override
public void itemMove(int oldIndex, int newIndex) {
    notifyItemMoved(oldIndex, newIndex);
}

@Override
public void itemDelete(int index) {
    notifyItemRemoved(index);
}

class ListViewHolder extends RecyclerView.ViewHolder{
    ImageView iv;
    TextView tv;
    public ListViewHolder(@NonNull View itemView) {
        super(itemView);
        iv = itemView.findViewById(R.id.iv);
        tv = itemView.findViewById(R.id.tv);
    }
}

}欢迎私信留言讨论。

你可能感兴趣的:(RecyclerView拖动之ItemTouchHelper.Callback的使用)