I am currently learning Android Studio as a beginner so forgive me if I am asking a question they may seem to easy for professionals. What is the correct way to add a StickyHeader to a recyclerView? I am trying to develop an example template that will has around 25 rows but the items should be grouped under (5) different stickyHeaders. This is where the problem lies. There are no clear directions on how to accomplish this and a bit confusing.
Currently, there are (5) total files associated with the code which are:(1)Adapter.java, (2) Item.java (3)MainActivity.java, (4) activity_main.xml and finally (5)item.xml. I am not sure how to accomplish this stickyHeader coding.
(1) Here is the Adapter.java file:
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageView);
mTextView1 = itemView.findViewById(R.id.textView);
mTextView2 = itemView.findViewById(R.id.textView2);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList) {
mExampleList = exampleList;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}
(2) Here is the item.java file:
public class ExampleItem {
private int mImageResource;
private String mText1;
private String mText2;
public ExampleItem(int imageResource, String text1, String text2) {
mImageResource = imageResource;
mText1 = text1;
mText2 = text2;
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
public String getText2() {
return mText2;
}
}
(3) Here is the MainActivity.java file:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 1", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 2", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 3", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 4", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 5", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 6", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 7", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 8", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 9", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 10", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 11", "SubTitle"));
exampleList.add(new ExampleItem(R.drawable.ic_android, "Title 12", "SubTitle"));
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}
I would like to have a stickyHeader between a few topics, but don't know how to implement the stickyHeader codes. Everyone is stating that it is very confusing.