Android Cursor RecyclerView does a flick on swipe

2019-07-23 13:06发布

问题:

I want to implement swipeable items in my recycler view.

I'm using a custom recycler adapter populated with a Loader (Github link here) and a custom listener for swipe (Github link here).

Data is stored locally with a SQLite and retrieved with a call to a filtered Cursor.

I think I'm doing something wrong with the implementations, because what happens when I try to swipe is that my recycler item get like "flashed" for a very short time and then disappear.

Everything works fine except for this strange behavior.

This is my method for delete the item from the database:

public void swipeItem(int position) {
    mDatabase.open();
    String cursor_string = recyclerFilter.getText().toString();
    final Cursor filterCursor = mDatabase.fetchVisible(cursor_string);
    filterCursor.moveToPosition(position);
    int pID = filterCursor.getColumnIndex("_id");
    final String iD = filterCursor.getString(pID);
    mDatabase.deleteItem(iD);

    getLoaderManager().restartLoader(MAIN_LOADER_ID, null, mCallbacks);
}

I think that this flash is due to the fact that the animation performs faster than the swipeItem(position) method, but I'm not really sure about it.

At this point, any help would be very very appreciated, thanks in advance.

回答1:

Apparently, I've managed to find a solution to this. Using the Wrapper provided by Federico Ponzi in this question.

In my swipe listener, I call an AsyncTask class which does the removing work directly on the cursor in background, then onPostExecute I call getLoaderManager.restartLoader(...);.

In the meantime, I set a temporary CursorWrapper to show my new data. As soon as the AsyncTask finish his work, getLoaderManager get called in my main activity and my recyclerView get the fresh adapter with the new data.

Not sure if it's enough clear, my english doesn't help for sure.