从RecyclerView GridLayout中删除项目

时间:2016-04-17 04:35:06

标签: android drag-and-drop

我有像下图所示的RecyclerView GridLayout 但我不知道如何删除项目。我想创建新的ImageView,它出现在LongClick项目中,如果我在ImageView上移动此元素,则删除此元素。请帮帮我,怎么做?

enter image description here

2 个答案:

答案 0 :(得分:3)

首先下载GitHub project,就像@zahidulislam一样。 编辑文件。

<强> MainActivity:

public class MainActivity extends AppCompatActivity {

private RecyclerListAdapter adapter;
private ImageView trashImage;
private RecyclerView recyclerView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

    trashImage = (ImageView) findViewById(R.id.trash_can);
    adapter = new RecyclerListAdapter(this);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setAdapter(adapter);

    GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(layoutManager);

    recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
        }

        @Override
        public void onItemLongClick(final View view, final int position) {
            final ImageView trashCan = (ImageView) trashImage.findViewById(R.id.trash_can);
            trashCan.setVisibility(View.VISIBLE);

            recyclerView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    int y = SimpleItemTouchHelperCallback.y;
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_MOVE:
                            if (y < -50) {
                                trashCan.setImageDrawable(getResources().getDrawable(R.drawable.trashcan_hover));
                            } else {
                                trashCan.setImageDrawable(getResources().getDrawable(R.drawable.trashcan));
                            }
                            break;
                        case MotionEvent.ACTION_UP:
                            if (y < -50) {
                                RecyclerListAdapter.mItems.remove(position);
                                adapter.notifyDataSetChanged();
                                trashCan.setImageDrawable(getResources().getDrawable(R.drawable.trashcan));
                                trashCan.setVisibility(View.GONE);
                                return true;
                            }
                            trashCan.setVisibility(View.GONE);
                        case MotionEvent.ACTION_CANCEL:
                            break;
                    }
                    return false;
                }
            });
        }
    }));

    ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);
    ItemTouchHelper mItemTouchHelper = new ItemTouchHelper(callback);
    mItemTouchHelper.attachToRecyclerView(recyclerView);
    }
}

SimpleItemTouchHelperCallback 添加字段:

public static int y;

并在方法public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive)

中表达
y = (int) viewHolder.itemView.getY();

只需放在顶部(垃圾桶)。

答案 1 :(得分:1)

要在recyclerview检查this库中滑动以进行删除。 它有非常好的api。