Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating sections within a RecyclerView

I have worked with listviews for some time now and as I began to understand them, they come out with recyclerview. I have a simple working example containing all of the items. I would like to create sections within the recyclerview using the same layout. For example, each section is filtered by data (Breakfast, lunch, dinner). How would I create sections within the recyclerview? If you need more information, or believe it is a bad question, I will update my question or supply more code.

How I would filter the adapters by 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 I get all of the items in the adapter:

FoodAdapter mAdapter = new FoodAdapter(LogSet.all(), R.layout.row, this);

Here is a working example of all:

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);
        /*
        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);
        */
    }
}

Here 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 the 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);
        /*
        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);
        */
    }
}

Again, How would I create sections within the RecyclerView with the Breakfast, Lunch and Dinner?

like image 236
Eugene H Avatar asked Mar 12 '15 18:03

Eugene H


1 Answers

You can achieve it with only one adapter from this library: SectionedRecyclerViewAdapter.

First create a Section class to group your items:

class MySection extends StatelessSection {

    String title;
    List<LogSet> list;

    public MySection(String title, List<LogSet> list) {
        // call constructor with layout resources for this Section header, footer and items 
        super(R.layout.section_header, R.layout.section_item);

        this.title = title;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return list.size(); // number of items of this section
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

        // bind your view here
        itemHolder.tvItem.setText(list.get(position).getLiftName());
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new SimpleHeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

        // bind your header view here
        headerHolder.tvItem.setText(title);
    }
}

Then you set up the RecyclerView with 3 instances of your section class (Breakfast, Lunch and Dinner):

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data
MySection breakfastSection = new MySection("Breakfast", LogSet.filterBy("Breakfast"));
MySection lunchSection = new MySection("Lunch", LogSet.filterBy("Lunch"));
MySection dinnerSection = new MySection("Dinner", LogSet.filterBy("Dinner"));

// Add your Sections to the adapter
sectionAdapter.addSection(breakfastSection);
sectionAdapter.addSection(lunchSection);
sectionAdapter.addSection(dinnerSection);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
like image 67
Gustavo Avatar answered Nov 11 '22 07:11

Gustavo