当我使用recycleView时如何在滑动时敲击文本

时间:2019-09-07 14:42:47

标签: android

我要在滑动时敲击文本。

我正在使用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);

1 个答案:

答案 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对象的状态绑定到专用视图。在这里,您可以对文本,样式等进行所有必要的更改,以表示项目对象的状态。