我要在滑动时敲击文本。
我正在使用ItemTouchHelper
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT ) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
// I guess here I should highlight the text, but when I'm swiping the slide is replaced with something else. So It would be nice to have same thing, but with text striked and a checkbox checked..
}
}).attachToRecyclerView(list_list);
答案 0 :(得分:0)
您的ItemTouchHelper应该如下所示:
final List<Item> list = new ArrayList<>();
final ItemAdapter adapter = new ItemAdapter(this, list);
....
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int swipeFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
return makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
Item item = list.get(viewHolder.getAdapterPosition());
item.setChecked(!item.isChecked());
adapter.notifyItemChanged(viewHolder.getAdapterPosition());
}
});
说明:在我创建list
类的情况下,Item
是您商品的来源,
private class Item {
private String mText;
private boolean mIsChecked;
Item(String text, boolean isChecked) {
mText = text;
mIsChecked = isChecked;
}
public void setChecked(boolean isChecked) {
mIsChecked = isChecked;
}
public String getText() {
return mText;
}
public boolean isChecked() {
return mIsChecked;
}
}
Item
对象的相同列表将传递给适配器。因此,在滑动事件中,我们需要修改Item
对象的状态并通知Adapter
特定项目已更改,这将导致通过调用onBindViewHolder
重绘该项目。在我的情况下看起来像这样:
private static class ItemAdapter extends RecyclerView.Adapter<ItemVH> {
List<Item> mContent;
LayoutInflater mInflater;
public ItemAdapter(Context context, List<Item> items) {
super();
mContent = items;
mInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public ItemVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.rv_item_view, parent, false);
return new ItemVH(itemView);
}
@Override
public void onBindViewHolder(@NonNull ItemVH holder, int position) {
Item item = mContent.get(position);
holder.text.setText(item.getText());
//Make a stroke according to checked state
holder.text.setPaintFlags(getFlagsForChecked(holder.text.getPaintFlags(), item.isChecked()));
holder.checkBox.setChecked(item.isChecked());
}
private int getFlagsForChecked(int origin, boolean isChecked) {
if (isChecked) {
return origin | Paint.STRIKE_THRU_TEXT_FLAG;
} else {
return origin & (~Paint.STRIKE_THRU_TEXT_FLAG);
}
}
@Override
public int getItemCount() {
return mContent.size();
}
}
在onBindViewHolder
中,我们通过ViewHolder将Item
对象的状态绑定到专用视图。在这里,您可以对文本,样式等进行所有必要的更改,以表示项目对象的状态。