EfficientAdapter

30ec7b2d4dae5107b36c52fff7a29894?s=47 Stan Kocken
September 22, 2015

 EfficientAdapter

Create a new adapter for a RecyclerView or ViewPager is now much easier.

30ec7b2d4dae5107b36c52fff7a29894?s=128

Stan Kocken

September 22, 2015
Tweet

Transcript

  1. @stan_kocken EfficientAdapter use of RecyclerView much easier

  2. RecyclerView

  3. RecyclerView LayoutManager

  4. RecyclerView LayoutManager Adapter Dataset ViewHolder

  5. w er Adapter ViewHolder public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private String[] mDataset; public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } } public MyAdapter(String[] myDataset) { mDataset = myDataset; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(mDataset[position]); } @Override public int getItemCount() { return mDataset.length; } } https://developer.android.com/training/material/lists-cards.html
  6. Click listeners ?

  7. w er Adapter ViewHolder public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private String[] mDataset; public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } } public MyAdapter(String[] myDataset) { mDataset = myDataset; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(mDataset[position]); } @Override public int getItemCount() { return mDataset.length; } } https://developer.android.com/training/material/lists-cards.html
  8. Click listeners ? only on some items?

  9. w er Adapter ViewHolder public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private String[] mDataset; public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } } public MyAdapter(String[] myDataset) { mDataset = myDataset; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(mDataset[position]); } @Override public int getItemCount() { return mDataset.length; } } https://developer.android.com/training/material/lists-cards.html
  10. Click listeners ? only on some items? add an element

    ?
  11. w er Adapter ViewHolder public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private String[] mDataset; public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } } public MyAdapter(String[] myDataset) { mDataset = myDataset; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(mDataset[position]); } @Override public int getItemCount() { return mDataset.length; } } https://developer.android.com/training/material/lists-cards.html
  12. Click listeners ? only on some items? add an element

    ? Different layouts ?
  13. w er Adapter ViewHolder public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private String[] mDataset; public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } } public MyAdapter(String[] myDataset) { mDataset = myDataset; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(mDataset[position]); } @Override public int getItemCount() { return mDataset.length; } } https://developer.android.com/training/material/lists-cards.html
  14. Click listeners ? only on some items? add an element

    ? Different layouts ? One adapter like that per list ?
  15. EfficientAdapter

  16. w er Adapter ViewHolder EfficientAdapter EfficientViewHolder

  17. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { ... public static class

    ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } } ... @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(mDataset[position]); } ... } Made in Google ViewHolder
  18. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { ... public static class

    ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(TextView v) { super(v); mTextView = v; } } ... @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(mDataset[position]); } ... } public class MyViewHolder extends EfficientViewHolder<String> { public MyViewHolder(View v) { super(v); } @Override protected void updateView(Context context, String object) { TextView textView = (TextView) getView(); textView.setText(object); } } Made in Google with EfficientAdapter ViewHolder
  19. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private String[] mDataset; ...

    public MyAdapter(String[] myDataset) { mDataset = myDataset; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } ... @Override public int getItemCount() { return mDataset.length; } } recyclerView.setAdapter(new MyAdapter()); Made in Google Adapter
  20. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private String[] mDataset; ...

    public MyAdapter(String[] myDataset) { mDataset = myDataset; } @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } ... @Override public int getItemCount() { return mDataset.length; } } recyclerView.setAdapter(new MyAdapter()); Made in Google recyclerView.setAdapter( new EfficientRecyclerAdapter<String>(R.layout.my_text_view, MyViewHolder.class, myDataset)); with EfficientAdapter Adapter
  21. Advanced topics • access view into the layout

  22. public class MyViewHolder extends EfficientViewHolder<String> { ... @Override protected void

    updateView(Context context, String object) { TextView textView = findViewByIdEfficient(R.id.my_textview); textView.setText(object); } } findViewByIdEfficient
  23. findViewByIdEfficient public class MyViewHolder extends EfficientViewHolder<String> { ... @Override protected

    void updateView(Context context, String object) { TextView textView = findViewByIdEfficient(R.id.my_textview); textView.setText(object); } } auto-hold views
  24. findViewByIdEfficient public class MyViewHolder extends EfficientViewHolder<String> { ... @Override protected

    void updateView(Context context, String object) { TextView textView = findViewByIdEfficient(R.id.my_textview); textView.setText(object); } } auto-cast
  25. Advanced topics • add clicks listeners

  26. EfficientAdapter adapter = new EfficientRecyclerAdapter<String>(...); adapter.setOnItemClickListener(this); adapter.setOnItemLongClickListener(this); add clicks listeners

    void onItemClick(EfficientAdapter<String> parent, View view, String object, int position); void onLongItemClick(EfficientAdapter<String> parent, View view, String object, int position);
  27. add clicks listeners - not everywhere public class MyViewHolder extends

    EfficientViewHolder<String> { ... @Override public boolean isClickable() { return true; } @Override public boolean isLongClickable() { return false; } }
  28. Advanced topics • add / remove elements

  29. add / remove elements adapter.add(“example1”); // add one object adapter.addAll(“example

    2”, “example 3”); // add from an array adapter.addAll(list); // add from a collection adapter.remove(“example1”); // remove element adapter.removeAt(1); // remove element at position 1 adapter.move(1, 2); // invert element 1 and 2 Auto-animated
  30. Advanced topics • different layout / item

  31. different layout / item EfficientAdapter adapter = new EfficientRecyclerAdapter(myDataset);

  32. different layout / item EfficientAdapter adapter = new EfficientRecyclerAdapter(myDataset) {

    @Override public int getItemViewType(int position) { return ? } };
  33. different layout / item private static final int VIEW_TYPE_BOOK =

    0; private static final int VIEW_TYPE_PLANE = 1; EfficientAdapter adapter = new EfficientRecyclerAdapter(myDataset) { @Override public int getItemViewType(int position) { return (get(position) instanceof Plane) ? VIEW_TYPE_PLANE : VIEW_TYPE_BOOK; } };
  34. different layout / item private static final int VIEW_TYPE_BOOK =

    0; private static final int VIEW_TYPE_PLANE = 1; EfficientAdapter adapter = new EfficientRecyclerAdapter(myDataset) { @Override public int getItemViewType(int position) { return (get(position) instanceof Plane) ? VIEW_TYPE_PLANE : VIEW_TYPE_BOOK; } @Override public Class<? extends AbsViewHolder> getViewHolderClass(int viewType) { return ? } @Override public int getLayoutResId(int viewType) { return ? } };
  35. different layout / item private static final int VIEW_TYPE_BOOK =

    0; private static final int VIEW_TYPE_PLANE = 1; EfficientAdapter adapter = new EfficientRecyclerAdapter(myDataset) { @Override public int getItemViewType(int position) { return (get(position) instanceof Plane) ? VIEW_TYPE_PLANE : VIEW_TYPE_BOOK; } @Override public Class<? extends EfficientViewHolder> getViewHolderClass(int viewType) { return viewType == VIEW_TYPE_PLANE ? PlaneViewHolder.class : BookViewHolder.class; } @Override public int getLayoutResId(int viewType) { return viewType == VIEW_TYPE_PLANE ? R.layout.item_plane : R.layout.item_book; } };
  36. ViewPager Also compatible with

  37. github.com/ StanKocken/EfficientAdapter ★268 Gradle: dependencies { compile 'com.skocken:efficientadapter:2.0' } @stan_kocken