0

I'm trying to catch the point when a Recyclerview item is dropped after a drag&drop operation, e.g. to read out the coordinates.

Thought that prepareForDrop event of LinearLayoutManager would be the right thing for that:

public class OverviewActivity extends AppCompatActivity implements OnStartDragListener {

private RecyclerView recyclerView;
private RecyclerAdapter myAdapter;
private LinearLayoutManager linearLayoutManager;
private ItemTouchHelper mItemTouchHelper;

private dbhelper db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_overview);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    linearLayoutManager = new LinearLayoutManager(this) {
        @Override
        public void prepareForDrop(View view, View target, int x, int y) {
            // do something
            return;
        }
    };
    recyclerView.setLayoutManager(linearLayoutManager);

    myAdapter = new RecyclerAdapter(this, this);
    recyclerView.setAdapter(myAdapter);
    myAdapter.setOnScrollToListener(new OnScrollToListener() {
        @Override
        public void scrollTo(int position) {
            recyclerView.scrollToPosition(position);
        }
    });

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

    db = new dbhelper(this);
    db.open();

    myAdapter.addRootCards(3);
}
.......

LinearLayoutManager is created and attached to RecyclerView and Override for prepareForDrop event is implemented.

The problem is: prepareForDrop() is never called, but why, what's wrong?

Pravin Divraniya
  • 3,642
  • 2
  • 26
  • 45
Reini
  • 1
  • 2

1 Answers1

0

According to the source code prepareForDrop() is only called from onMoved if the LayoutManager instanceOf ViewDropHandler. In your case, it is. Which makes me believe that onMoved() is not being called. According to its javadocs onMoved() is only called if onMove() returns true. You haven't shown the implementation of your SimpleItemTouchHelperCallback but my guess is that its implementation of onMove() returns false.

Clark Battle
  • 410
  • 5
  • 12