RecyclerView Item changes after scrolling - android

I was trying to learn how to create a Recycler View, the first time when I followed a tutorial about it and hard-coded the contents of the items in the Recycler View everything worked fine; but when I moved the contents to strings.xml and sent the values something went wrong.
First the screen would be fine and appear like
this (keeping in mind that I have a total of six items), and after scrolling;
any item that goes off-Screen will change when it comes back like this, any help is grateful.
note: when I removed the sixth item from my list everything was fine; because no item went off-Screen so no changes happened.
Main Acitivity:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecycler;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<Contacts> contacts;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contacts = new ArrayList<>();
mRecycler = findViewById(R.id.recyclerTut);
mRecycler.setHasFixedSize(false);
DataEntry dataEntry = new DataEntry(getResources().getStringArray(R.array.contact_names),
getResources().getStringArray(R.array.contact_numbers),
getResources().obtainTypedArray(R.array.contact_photos));
contacts = dataEntry.individualContacts();
mLayoutManager = new LinearLayoutManager(this);
mRecycler.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(this, contacts);
mRecycler.setAdapter(mAdapter);
}
}
Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context mContext;
private List<Contacts> mDataset;
public MyAdapter(Context mContext, List<Contacts> myDataset) {
this.mContext = mContext;
mDataset = myDataset;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(mContext)
.inflate(R.layout.recycler_view_adapter, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.titleText.setText(mDataset.get(position).getName());
holder.subtitleText.setText(mDataset.get(position).getpNumber());
holder.recyclerImage.setImageResource(mDataset.get(position).getImageSource());
}
#Override
public int getItemCount() {
return mDataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView recyclerImage;
public TextView titleText;
public TextView subtitleText;
public ViewHolder(View v) {
super(v);
recyclerImage = v.findViewById(R.id.recycler_image);
titleText = v.findViewById(R.id.recycler_title);
subtitleText = v.findViewById(R.id.recycler_subtitle);
}
}
DataEntry:
public class DataEntry {
private String []names;
private String []numbers;
private TypedArray photos;
private List<Contacts> indivisuals = new ArrayList<>();
public DataEntry(String[] names, String[] numbers, TypedArray photos) {
this.names = names;
this.numbers = numbers;
this.photos = photos;
}
public List<Contacts> individualContacts(){
for(int i = 0 ; i < names.length ; i++){
int imageSource = photos.getResourceId(i, -1);
indivisuals.add(new Contacts(names[i], numbers[i], imageSource));
}
return indivisuals;
}}

Related

accessing an adapter's data via another adapter

Is it all possible to access an adapter's data via another adapter?
I want to start an activity and pass data from an adapter to a fragment which is used in TabLayout as one of three fragments, I have two adapters and a button which is clicked to start an activity, its Java code is in my first adapter and I need to pass second adapter's data via second adapter itself
here is my codes:
my first adapter:
public class RecyclerViewDataAdapter extends RecyclerView.Adapter<RecyclerViewDataAdapter.ItemRowHolder>{
private ArrayList<SectionDataModel> dataList;
private Context mContext;
private RecyclerView.RecycledViewPool recycledViewPool;
private SnapHelper snapHelper;
public RecyclerViewDataAdapter(ArrayList<SectionDataModel> dataList, Context mContext) {
this.dataList = dataList;
this.mContext = mContext;
recycledViewPool = new RecyclerView.RecycledViewPool();
}
#Override
public ItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
ItemRowHolder rowHolder = new ItemRowHolder(v);
snapHelper = new GravitySnapHelper(Gravity.START);
return rowHolder;
}
#Override
public void onBindViewHolder(ItemRowHolder holder, int position) {
ArrayList singleSectionItems = dataList.get(position).getAllItemInSection();
final String sectionName = dataList.get(position).getHeaderTitle();
holder.itemTitle.setText(sectionName);
SectionDataAdapter adapter = new SectionDataAdapter(singleSectionItems, mContext);
holder.recyclerView.setHasFixedSize(true);
holder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setAdapter(adapter);
holder.recyclerView.setRecycledViewPool(recycledViewPool);
snapHelper.attachToRecyclerView(holder.recyclerView);
holder.btnMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//here i can start the activity but..(second adapter)
Toast.makeText(view.getContext(), sectionName, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return (null != dataList ? dataList.size() : 0);
}
public class ItemRowHolder extends RecyclerView.ViewHolder {
protected ImageView mitemImage;
protected TextView mitemtext;
protected TextView itemTitle;
protected RecyclerView recyclerView;
protected Button btnMore;
public ItemRowHolder(View itemView) {
super(itemView);
this.mitemImage = itemView.findViewById(R.id.itemImage);
this.mitemtext = itemView.findViewById(R.id.tvTitle);
this.itemTitle = itemView.findViewById(R.id.itemTitle);
this.recyclerView = itemView.findViewById(R.id.recycler_view_list);
this.btnMore = itemView.findViewById(R.id.btnMore);
}
}
} '
and my second adapter:
import java.net.PortUnreachableException;
import java.util.ArrayList;
public class SectionDataAdapter extends RecyclerView.Adapter<SectionDataAdapter.SssingleItemRowHolder>{
private ArrayList<SingleItemModel> itemModels;
private Context mContext;
public SectionDataAdapter(ArrayList<SingleItemModel> itemModels, Context mContext) {
this.itemModels = itemModels;
this.mContext = mContext;
}
#Override
public SingleItemRowHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_single_card, null);
SingleItemRowHolder singleItemRowHolder = new SingleItemRowHolder(v);
return singleItemRowHolder;
}
#Override
public void onBindViewHolder(SingleItemRowHolder holder, int position) {
SingleItemModel itemModel = itemModels.get(position);
holder.tvTitle.setText(itemModel.getName());
holder.mitemImage.setImageResource(itemModel.getImage());
}
#Override
public int getItemCount() {
return (null != itemModels ? itemModels.size() : 0);
}
public class SingleItemRowHolder extends RecyclerView.ViewHolder {
protected TextView tvTitle;
protected ImageView mitemImage;
public SingleItemRowHolder(View itemView) {
super(itemView);
final Intent intent = new Intent(mContext,MainActivity.class);
this.mitemImage = itemView.findViewById(R.id.itemImage);
this.tvTitle = itemView.findViewById(R.id.tvTitle);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//... need to start the activity from here
Toast.makeText(view.getContext(), tvTitle.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
Like #MilaDroid said you simply need a getter that returns the another Adapter's ArrayList<SingleItemModel> but the problem you will face is that you need to have the same instance of the Adapter from the Activity in order to get the populated ArrayList<SingleItemModel>.
A good workaround is to used Bill Pugh's Singleton in the Adapter
public class Adapter {
private ArrayList<SingleItemModel> list;
private Adapter() {}
public static Adapter getInstance() {
return InstInit.INSTANCE;
}
// Don't forget to set the list (or NPE)
// because we can't argue with a Singleton
public void setList(ArrayList<SingleItemModel> list) {
this.list = list;
}
// You can now get the ArrayList
public ArrayList<SingleItemModel> getList() {
return list;
}
private static class InstInit {
private static final Adapter INSTANCE = new Adapter();
}
// Overrided RecyclerView.Adapter Methods
.................
}
Retrieving the ArrayList assuming that the following Adapters are Singleton
AdapterOne a1 = AdapterOne.getInstance();
AdapterTwo a2 = AdapterTwo.getInstance();
ArrayList<SingleItemModel> a1RetrievedList = a1.getList();
// You don't need to create a new instance
// creating a new instance doesn't make sense
// because you need to repopulate the list
// for the new instance.
ArrayList<SingleItemModel> a2RetrievedList = a2.getList();
// You can also retrieve from AdapterTwo

RecyclerView does not load images

I made a simple RecyclerView, but it do not load images!
Here are my codes. RecyclerView has 11 raws and each raw has 2 textviews and 1 imageview,
This is the important part of main activity,
MainActivity.java:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
String[] F_Name, D_Name;
int[] Img_res = {R.drawable.a_beautiful_mind,
R.drawable.a_seperation,
R.drawable.casablanca,
R.drawable.daei_jan_napelon,
R.drawable.game_of_thrones,
R.drawable.life_of_pie,
R.drawable.the_dictator,
R.drawable.the_godfather,
R.drawable.the_martian,
R.drawable.the_revenant,
R.drawable.v_for_vendetta};
ArrayList<DataProvider> arrayList = new ArrayList<DataProvider>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
F_Name = getResources().getStringArray(R.array.Film_Names);
D_Name = getResources().getStringArray(R.array.Director_Names);
int i = 0;
for (String name : F_Name){
DataProvider dataProvider = new DataProvider(Img_res[i], name, D_Name[i]);
arrayList.add(dataProvider);
i++;
}
adapter = new RecyclerAdapter(arrayList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
}
Here is my Adapter, I added 11 raws, and each raw contains 2 textviews and 1 ImageView,
Adapter:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
private ArrayList<DataProvider> arrayList = new ArrayList<DataProvider>();
public RecyclerAdapter(ArrayList<DataProvider> arrayList){
this.arrayList = arrayList;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recyclerview_raws, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
DataProvider dataProvider = arrayList.get(position);
holder.imageView.setImageResource(dataProvider.getImg_res());
holder.f_name.setText(dataProvider.getF_name());
holder.d_name.setText(dataProvider.getD_name());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView f_name, d_name;
public RecyclerViewHolder(View view){
super(view);
imageView = (ImageView)view.findViewById(R.id.myRecyclerViewImage);
f_name = (TextView)view.findViewById(R.id.myTextVFilmName);
d_name = (TextView)view.findViewById(R.id.myTextVDirectorName);
}
}
}
Can anyone help me please?

How i can add List of items to my RecyclerView adapter

in onLoadFinsih i get back from the server List of movies i want to
add this list to my RecyclerView adapter
i tried to create List of movies and pass it to my RecyclerView adapter
then when i get the list of movies from the server in onLoadFinish method i add
this list to the list that i passed in the adapter then notify the adapter
but not working
public class MainActivity extends AppCompatActivity
implements LoaderManager.LoaderCallbacks<List<Movie>>{
private RecyclerView.Adapter movieRecyclerViewAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<Movie> mListOfMovies;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mListOfMovies = new ArrayList<>();
movieRecyclerViewAdapter = new MovieItemAdapter(this, mListOfMovies);
mLayoutManager = new LinearLayoutManager(this);
recyclerView.setAdapter(movieRecyclerViewAdapter);
}
#Override
public void onLoadFinished(Loader<List<Movie>> loader, List<Movie> movies) {
//hide progress bar after fetch data from server
progressBar.setVisibility(View.GONE);
mListOfMovies.clear();
mListOfMovies.addAll(movies);
movieRecyclerViewAdapter.notifyItemRangeChanged();
}
public class MovieItemAdapter extends RecyclerView.Adapter<MovieItemAdapter.ViewHolder> {
private List<Movie> mListOfMovies;
private Context mContext;
public MovieItemAdapter(Context context, List<Movie> movies) {
this.mListOfMovies = movies;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext)
.inflate(R.layout.movie_list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Movie movieItem = mListOfMovies.get(position);
holder.moviePoster.setImageBitmap(movieItem.getBitmap());
holder.movieName.setText(movieItem.getTitle());
DecimalFormat format = new DecimalFormat("0.0");
holder.voteAverage.setText(format.format(movieItem.getVoteAverage()));
holder.releaseDate.setText(movieItem.getReleaseDate());
}
#Override
public int getItemCount() {
return mListOfMovies.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView moviePoster;
public TextView movieName;
public TextView voteAverage;
public TextView releaseDate;
public ViewHolder(View itemView) {
super(itemView);
moviePoster = (ImageView) itemView.findViewById(R.id.movie_poster);
movieName = (TextView) itemView.findViewById(R.id.movie_name);
voteAverage = (TextView) itemView.findViewById(R.id.vote_average);
releaseDate = (TextView) itemView.findViewById(R.id.release_date);
}
}
}
In onLoadFinish() you should update the list in your adapter with the received data.
Then you should call the adapter's adapter.notifyDataSetChanged().
EDIT:
Create a method in adapter like
setMovieList(List<Movie> movies){
mListOfMovies = movies;
this.notifyDataSetChanged();
}
and call it from onLoadFinished():
movieRecyclerViewAdapter.setMovieList(mListOfMovies)
Make your ViewHolder class non static in adapter class. Then check again.

Android Studio, RecyclerView crashes my app on start up

I am battling with the android Recyclerviewer. I followed most tutorials and also read posts here on StackOverflow, but i am still not coming right.
MainActivity
public class MainActivity extends ActionBarActivity {
RecyclerView myrecyclerView;
MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myrecyclerView = (RecyclerView)findViewById(R.id.myRecycleView);
adapter = new MyAdapter(MainActivity.this, getData());
final LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myrecyclerView.setLayoutManager(layoutManager);
myrecyclerView.setAdapter(adapter);
myrecyclerView.setItemAnimator(new DefaultItemAnimator());
//
}
//the Dataset
public static List<MyDataModel> getData()
{
List<MyDataModel> myData = new ArrayList<>();
String[] postText = {"ibm", "apple", "google", "microsoft"};
int[] postImgUrl = {R.drawable.google, R.drawable.ibm, R.drawable.microsoft, R.drawable.apple};
for(int x=0; x<postText.length && x<postImgUrl.length; x++)
{
MyDataModel myModel = new MyDataModel();
myModel.postImgUrl = postImgUrl[x];
myModel.postText = postText[x];
myData.add(myModel);
}
return myData;
}
MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
List<MyDataModel> MyDataset;
private LayoutInflater inflater;
public MyAdapter(Context cont, List<MyDataModel> MyDataset)
{
inflater = LayoutInflater.from(cont);
}
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int ViewType)
{
//get ItemLayoutView from xml
View itemLayoutView = inflater.inflate(R.layout.my_items_row, parent, false);
//pass it to the ViewHolder
ViewHolder myViewHolder = new ViewHolder(itemLayoutView);
return myViewHolder;
}
public void onBindViewHolder(ViewHolder viewHolder, int position)
{
viewHolder.postText.setText(MyDataset.get(position).postText);
viewHolder.postImage.setImageResource(MyDataset.get(position).postImgUrl);
}
public int getItemCount()
{
return MyDataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
public TextView postText;
public ImageView postImage;
public ViewHolder(View v)
{
super(v);
postText = (TextView)v.findViewById(R.id.myPostText);
postImage = (ImageView)v.findViewById(R.id.myPostImage);
}
}
}
My model Data
public class MyDataModel {
String postText;
int postImgUrl;
}
I am testing on real device through android studio, other apps i that i am also testing are fine.
Please help thank you.
You didn't initialize MyDataset inside MyAdapter class

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?
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);

Categories

Resources