简体   繁体   中英

ItemTouchHelper and SwipeRefreshLayout (RecyclerView)

I want to use the new ItemTouchHelper from the v7 design library. I use it inside of a SwipeRefreshLayout.

The problem is that with the SwipeRefreshLayout the animation for the swipe to dismiss is wrong and buggy.

Anyone knows how to fix this?

You could disable your SwipeRefreshLayout as long as you are swiping.

final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
    @Override
    public int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder) {
        return makeMovementFlags(0, ItemTouchHelper.START | ItemTouchHelper.END);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(ViewHolder viewHolder, int direction) {
        // do something
    }

    @Override
    public void onSelectedChanged(ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        final boolean swiping = actionState == ItemTouchHelper.ACTION_STATE_SWIPE;
        swipeRefreshLayout.setEnabled(!swiping);
    }
});
itemTouchHelper.attachToRecyclerView(recyclerView);

one of many:

 /**
 * Notify the widget that refresh state has changed. 
 * method to avoid bug in setRefreshing(boolean)     
 * Do not call this when refresh is triggered by a swipe gesture.
 * @param swipeRefreshLayout - layout, take care not to pass null parameter
 * @param isRefreshing - whether or not the view should show refresh progress.
 *               
 */
public static void setRefreshing(final SwipeRefreshLayout swipeRefreshLayout, final boolean isRefreshing) {
    swipeRefreshLayout.post(new Runnable() {
        @Override
        public void run() {
            swipeRefreshLayout.setRefreshing(isRefreshing);
        }
    });
}

some other possibilities:

posted bug with some workarounds

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM