单击按钮时如何滑动RecyclerView项目?

时间:2017-05-29 08:43:30

标签: android android-recyclerview

我在recyclerView中有多个项目,每个项目都有一个十字按钮。我点击十字按钮时要删除该项目。我能够刷这个项目,但想在Button Click上实现它。

我的适配器

public class RecyclerListAdapter extends RecyclerView.Adapter<ItemViewHolder> implements ItemTouchHelperAdapter {

private static final String[] STRINGS = new String[]{
        "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"
};

private final List<String> mItems = new ArrayList<>();
private Context context;
private ItemTouchHelper.Callback callback;


public RecyclerListAdapter(Activity activity, ItemTouchHelper.Callback callback) {
    mItems.addAll(Arrays.asList(STRINGS));
    this.context = activity;
    this.callback = callback;
}

@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main, parent, false);
    return new ItemViewHolder(view);
}

@Override
public void onBindViewHolder(final ItemViewHolder holder, final int position) {
    holder.textView.setText(mItems.get(position));
    holder.ivCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        // Swipe item from here
        }
    });
}

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

@Override
public void onItemMove(int fromPosition, int toPosition) {
    if (fromPosition < toPosition) {
        for (int i = fromPosition; i < toPosition; i++) {
            Collections.swap(mItems, i, i + 1);
        }
    } else {
        for (int i = fromPosition; i > toPosition; i--) {
            Collections.swap(mItems, i, i - 1);
        }
    }
    notifyItemMoved(fromPosition, toPosition);
}

@Override
public void onItemDismiss(int position) {
    mItems.remove(position);
    notifyItemRemoved(position);
}

我的回复是

public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback {

private final ItemTouchHelperAdapter mAdapter;

public SimpleItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
    mAdapter = adapter;
}

@Override
public boolean isLongPressDragEnabled() {
    return false;
}

@Override
public boolean isItemViewSwipeEnabled() {
    return true;
}

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

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                      RecyclerView.ViewHolder target) {
    mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
    return true;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}

我的活动是

public class MainFragment extends Activity {

public ItemTouchHelper.Callback callback;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Activity activity = MainFragment.this;
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(activity));


    RecyclerListAdapter adapter = new RecyclerListAdapter(activity,callback);
    recyclerView.setAdapter(adapter);


    callback = new SimpleItemTouchHelperCallback(adapter);


    ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
    touchHelper.attachToRecyclerView(recyclerView);


}

请指导我..

1 个答案:

答案 0 :(得分:0)

在适配器onBindViewHolder方法中为十字按钮设置一个onClickListener,并从中删除数组中的项目(借助position参数)并调用notifyDatasetChanged,或者你可以在那里调用你的滑动方法。