0

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
lawson
  • 1
  • 2
  • So I assume my question is only for those with android knowlege and not java? I'm new and guess I put the wrong tags? – lawson May 25 '19 at 08:26
  • Generally, always tag with the framework/platform if there is one. Android is effectively a required tag given the question. But because you're using Java, you can also tag java (but you can't only tag Java, because Android isn't Java. Java on Android is Java in terms of syntax, but the stdlib is re-written in several places and the language standard doesn't follow that of Java). Also, IDE tags shouldn't be used when you don't have questions about the IDE. No harm done though, it's easy to fix. You might also find [this meta post](https://meta.stackoverflow.com/q/354427/6296561) useful. – Zoe May 25 '19 at 08:53
  • TL;DR: you can use the Java tag with Android, but you also need to tag with Android to reach out to the right people. – Zoe May 25 '19 at 08:55
  • This might help you - https://stackoverflow.com/a/44327350/6430090 (just searsh the web with - StickyHeader android) – mr.kostua May 25 '19 at 09:23

0 Answers0