Posts

Showing posts with the label RecyclerView

RecyclerView reusing or recycling view holders

As the user scrolls the list, the  RecyclerView  creates new view holders as necessary. It also saves the view holders which have scrolled off-screen, so they can be reused. If the user switches the direction they were scrolling, the view holders which were scrolled off the screen can be brought right back. On the other hand, if the user keeps scrolling in the same direction, the view holders which have been off-screen the longest can be rebound to new data. The view holder does not need to be created or have its view inflated; instead, the app just updates the view's contents to match the new item it was bound to.

Android: Dynamic column/grid for ListView

public class DynamicColumnGridManager extends GridLayoutManager {     private int minItemWidth;     public DynamicColumnGridManager(Context context, int minItemWidth) {         super(context, 1);         this.minItemWidth = minItemWidth;     }     @Override     public void onLayoutChildren(RecyclerView.Recycler recycler,                                  RecyclerView.State state) {         updateSpanCount();         super.onLayoutChildren(recycler, state);     }     private void updateSpanCount() {         int spanCount = getWidth() / minItemWidth;         if (spanCount < 1) {             spanCount = 1;         }         thi...

Android: animate/translate RecyclerView items using ItemTouchHelper

ItemTouchHelper.Callback callback = new ItemMoveHelper(ItemTouchHelper. UP |  ItemTouchHelper. DOWN , ItemTouchHelper. START | ItemTouchHelper. END ); ItemTouchHelper helper = new ItemTouchHelper(callback); helper.attachToRecyclerView(m RecyclerView ); ItemTouchHelper moves the items' of a recyclerView to translateX/Y properties to  reposition them class ItemMoveHelper extends ItemTouchHelper.SimpleCallback { public ItemMoveHelper( int dragDirs, int swipeDirs) { super (dragDirs, swipeDirs); } @Override public boolean onMove(RecyclerView recyclerView,  RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { Collections. swap ( mBagList , viewHolder.getAdapterPosition(), target.getAdapterPosition()); mAdapter .notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition()); return false ; } @Override public void onSwiped(RecyclerView.ViewHolder viewH...