Using RecyclerView to show multiple different kinds of items by John Brohan

Using RecyclerView to show multiple different kinds of items by John Brohan

RecyclerViews are very fast and offer a way to show a part of a larger database without concern for the full size of the database. It is an added complication to have more than one view to show, but this is well supported and works well.

My app gathers observations for a whole day and shows this as a recyclerView. Swiping moves up or down within a day and right to left changes days. I will use this app as the backbone of my presentation


GDG Montreal

February 26, 2019


  1. RecyclerView: Introduction Large Database ... Just show relevant part. Management

    and scrolling done by Magic. John Brohan
  2. Subject matter is Urology. Record Pees and Drinks.

  3. Strategy of RecyclerView Sqlite database with Table of Pees and

    Table of Drinks ArrayList of ... DiaryEntries (D and P) for one day public DiaryAdapter(Context context, Activity activity, DiaryEntries diaryEntries, DataBase dataBase){ diaryAdapter.notifyDataSetChanged(); diaryAdapter.notifyItemInserted(0);
  4. public class DiaryEntry { int entryType; long rowId; long secs;

    long days; DiaryEntry (int entryType,long rowId,long secs,long days){ this.entryType = entryType; this.rowId = rowId; this.secs = secs; this.days = days; } public String toString(){ return String.format("rowId = %d entryType = %d secs =%d", rowId, entryType, secs); } }
  5. Diary Fragment On Create View ... ... recyclerView = view.findViewById(;

    recyclerView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent motionEvent) { Logd(String.format("in On Touch Listener...event = %s",motionEvent.toString())); return moveEventProcessor( motionEvent); } }); diaryEntries = new DiaryEntries(); dataBase = new DataBase(context, diaryEntries); UflowDbHelper uflowDbHelper = new UflowDbHelper(context); diaryEntries.initializDiaryEntryArrayList(); // initializes DiaryEntries list with peeing man diaryAdapter = new DiaryAdapter(context, activity, diaryEntries , dataBase); recyclerView.setAdapter(diaryAdapter);
  6. Structure onCreateViewHolder (ViewGroup parent, int viewType){... This makes a ViewHolder

    for this ViewType. onBindViewHolder(ViewHolder, holder, int position) This uses position to get the data to fill out the view diaryAdapter.notifyDataSetChanged(); diaryAdapter.notifyItemInserted(0);
  7. public static class DrinkViewHolder extends RecyclerView.ViewHolder { TextView timeDrinkView; TextView

    drinkVolumeView; TextView drinkTypeView; TextView drinkCommentView; public DrinkViewHolder(View itemView) { super(itemView); this.timeDrinkView = itemView.findViewById(; this.drinkVolumeView = itemView.findViewById(; this.drinkTypeView = itemView.findViewById(; this.drinkCommentView = itemView.findViewById(; } }
  8. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="6dp" >

    <TextView android:id="@+id/timedrinkview" android:layout_width="56dp" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" android:layout_gravity="bottom" android:textColor="@color/DarkGray" /> <TextView android:id="@+id/drinkvolumeview" android:layout_width="53dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_gravity="bottom" android:textSize="14sp" android:textColor="@color/DarkBlue" /> ... ...This is drink_item an xml file in res/layout There is another for pee_item and for chat_item
  9. onCreateViewHolder @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View

    view; switch (viewType){ case DRINKITEM: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.drink_item,parent,false); return new DrinkViewHolder(view); case PEEITEM: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pee_item,parent,false); return new PeeViewHolder(view); case CHATITEM: view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item,parent,false); return new ChatViewHolder(view); } return null; }
  10. onBindViewHolder @Override public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition)

    { int viewType = getItemViewType(listPosition); DiaryEntry de; switch (viewType) { case DRINKITEM: de = diaryEntries.getDiaryEntryAt(listPosition); Drink d = dataBase.readDrink(de.rowId); if (d == null) { Logd("nope not yet no Drink record..."); } else { ((DrinkViewHolder) holder).timeDrinkView.setText(d.time); ((DrinkViewHolder) holder).drinkVolumeView.setText(String.format("%d ml",d.volume)); ((DrinkViewHolder) holder).drinkTypeView.setText(String.format("%-14s",d.type)); ((DrinkViewHolder) holder).drinkCommentView.setText(String.format("%s",d.comment)); } break; case PEEITEM: ... default: Logd("wtf viewtype = 0"); break; } }