I have been working with lists for some time, and when I began to understand them, they come out with recycling. I have a simple working example containing all the elements. I would like to create sections in recyclerview using the same layout. For example, each section is filtered by data (breakfast, lunch, dinner). How do I create sections in recyclerview? If you need more information or if I think this is a bad question, I will update my question or put more code.
How I will filter adapters for breakfast, lunch and dinner:
FoodAdapter mAdapterBreakfast = new FoodAdapter(LogSet.filterBy("Breakfast"), R.layout.row, this); FoodAdapter mAdapterLunch = new FoodAdapter(LogSet.filterBy("Lunch"), R.layout.row, this); FoodAdapter mAdapterDinner = new FoodAdapter(LogSet.filterBy("Dinner"), R.layout.row, this);
How to get all the elements in the adapter:
FoodAdapter mAdapter = new FoodAdapter(LogSet.all(), R.layout.row, this);
The following is a working example:
Adapter
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder> { private List<LogSet> meals; private int rowLayout; private Context mContext; View v; public FoodAdapter(List<LogSet> mMeals, int rowLayout, Context context) { this.meals = mMeals; this.rowLayout = rowLayout; this.mContext = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { LogSet meal = meals.get(i); viewHolder.countryName.setText(meal.getLiftName()); } @Override public int getItemCount() { return meals == null ? 0 : meals.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView countryName; public ViewHolder(View itemView) { super(itemView); countryName = (TextView) itemView.findViewById(R.id.foodName); } } }
Mainactivity
public class MainActivity extends ActionBarActivity { RecyclerView mRecyclerView; Date mDate = new Date(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_testing_rec); mRecyclerView = (RecyclerView) findViewById(R.id.list); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); FoodAdapter mAdapter = new FoodAdapter(LogSet.all(), R.layout.row, this); mRecyclerView.setAdapter(mAdapter); } }
This is how I would send each list value to the adapter:
Adapter
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder> { private List<LogSet> meals; private List<LogSet> breakfast; private List<LogSet> lunch; private List<LogSet> dinner; private int rowLayout; private Context mContext; View v; public FoodAdapter(List<LogSet> mMeals, List<LogSet> mBreakfast, List<LogSet> mLunch, List<LogSet> mDinner, int rowLayout, Context context) { this.meals = mMeals; this.breakfast = mBreakfast; this.lunch = mLunch; this.dinner = mDinner; this.rowLayout = rowLayout; this.mContext = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { LogSet meal = meals.get(i); viewHolder.countryName.setText(meal.getLiftName()); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView countryName; public ViewHolder(View itemView) { super(itemView); countryName = (TextView) itemView.findViewById(R.id.foodName); } } @Override public int getItemCount() { return meals == null ? 0 : meals.size(); } }
MainActivity minus all ()
public class MainActivity extends ActionBarActivity { RecyclerView mRecyclerView; Date mDate = new Date(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_testing_rec); mRecyclerView = (RecyclerView) findViewById(R.id.list); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.setItemAnimator(new DefaultItemAnimator()); FoodAdapter mAdapter = new FoodAdapter(LogSet.all(), LogSet.filterBy("Breakfast"), LogSet.filterBy("Lunch"), LogSet.filterBy("Dinner"), R.layout.row, this); mRecyclerView.setAdapter(mAdapter); } }
Again, how do I create sections in RecyclerView with breakfast, lunch and dinner?