Slide 1

Slide 1 text

RECYCLERVIEW ANDROID LISTENER NOVEMBER 2014

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

A FLEXIBLE VIEW FOR PROVIDING A LIMITED WINDOW INTO A LARGE DATA SET.

Slide 4

Slide 4 text

Yeah, that's called a LISTVIEW!

Slide 5

Slide 5 text

REDUCES COUPLING

Slide 6

Slide 6 text

DELEGATION

Slide 7

Slide 7 text

MAKES IT EASY TO DO THE RIGHT THING

Slide 8

Slide 8 text

THE 5 CLASSES YOU'LL MEET...

Slide 9

Slide 9 text

ADAPTER

Slide 10

Slide 10 text

2 ROLES

Slide 11

Slide 11 text

PROVIDING DATA

Slide 12

Slide 12 text

PROVIDING LAYOUTS

Slide 13

Slide 13 text

public VH onCreateViewHolder(ViewGroup parent, int viewType) public void onBindViewHolder(VH holder, int position) public int getItemCount()

Slide 14

Slide 14 text

VIEW HOLDER

Slide 15

Slide 15 text

public final static class ListItemViewHolder extends RecyclerView.ViewHolder { TextView label; TextView dateTime; public ListItemViewHolder(View itemView) { super(itemView); label = (TextView) itemView.findViewById(R.id.txt_label_item); dateTime = (TextView) itemView.findViewById(R.id.txt_date_time); } }

Slide 16

Slide 16 text

LAYOUT MANAGER

Slide 17

Slide 17 text

ATTACHING MEASURING LAYING OUT

Slide 18

Slide 18 text

public class RecyclerView extends ViewGroup { public static abstract class LayoutManager { // The required bits to implement public abstract LayoutParams generateDefaultLayoutParams(); public void scrollToPosition(int position) { if (DEBUG) { Log.e(TAG, "You MUST implement scrollToPosition. It will soon become abstract"); } } public void onLayoutChildren(Recycler recycler, State state) { Log.e(TAG, "You must override onLayoutChildren(Recycler recycler, State state) "); } } }

Slide 19

Slide 19 text

DEFAULT IMPLEMENTATION /** * A {@link android.support.v7.widget.RecyclerView.LayoutManager} implementation which provides * similar functionality to {@link android.widget.ListView}. */ public class LinearLayoutManager extends RecyclerView.LayoutManager { /* REALLY INTERESTING CODE HERE */ }

Slide 20

Slide 20 text

ITEM DECORATION

Slide 21

Slide 21 text

public static abstract class ItemDecoration { /* drawn before the item views */ public void onDraw(Canvas c, RecyclerView parent, State state) {} /* drawn after the item views */ public void onDrawOver(Canvas c, RecyclerView parent, State state) {} /* adjusts the item insets to give space if necessary for the decoration */ public void getItemOffsets(Rect outRect, View view, RecyclerView parent, State state) {} } BIG NOTA BENE : These methods are not called of each item individually but just once for the RecyclerView

Slide 22

Slide 22 text

ITEM ANIMATOR

Slide 23

Slide 23 text

public static abstract class ItemAnimator { abstract public void runPendingAnimations(); abstract public boolean animateRemove(ViewHolder holder); abstract public boolean animateAdd(ViewHolder holder); abstract public boolean animateMove(ViewHolder holder, int fromX, int fromY, int toX, int toY); abstract public boolean animateChange(ViewHolder oldHolder, ViewHolder newHolder, int fromLeft, int fromTop, int toLeft, int toTop); abstract public void endAnimation(ViewHolder item); abstract public void endAnimations(); abstract public boolean isRunning(); }

Slide 24

Slide 24 text

DEFAULT IMPLEMENTATION /** * This implementation of {@link RecyclerView.ItemAnimator} provides basic * animations on remove, add, and move events that happen to the items in * a RecyclerView. RecyclerView uses a DefaultItemAnimator by default. */ public class DefaultItemAnimator extends RecyclerView.ItemAnimator { /* REALLY INTERESTING CODE HERE */ }

Slide 25

Slide 25 text

CODE

Slide 26

Slide 26 text

LINKS ▸ https://github.com/kevinmcmahon/android-RecyclerView ▸ http://lucasr.org/2014/07/31/the-new-twowayview/ ▸ http://wiresareobsolete.com/2014/09/building-a-recyclerview- layoutmanager-part-1/

Slide 27

Slide 27 text

KEVIN MCMAHON @KLMCMAHON HTTP://ABOUT.ME/KEVINMCMAHON