Upgrade to Pro — share decks privately, control downloads, hide ads and more …

EfficientAdapter

Stan Kocken
September 22, 2015

 EfficientAdapter

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

Stan Kocken

September 22, 2015
Tweet

More Decks by Stan Kocken

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  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 ? only on some items? add an element

    ? Different layouts ? One adapter like that per list ?
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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);
  15. add clicks listeners - not everywhere public class MyViewHolder extends

    EfficientViewHolder<String> { ... @Override public boolean isClickable() { return true; } @Override public boolean isLongClickable() { return false; } }
  16. 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
  17. different layout / item EfficientAdapter adapter = new EfficientRecyclerAdapter(myDataset) {

    @Override public int getItemViewType(int position) { return ? } };
  18. 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; } };
  19. 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 ? } };
  20. 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; } };