Depending on the type of object (record), I need to fill the RecyclerView different view , earlier when I did it with the ListView , I did it, but here I don’t even know how to organize it, the view is created immediately in onCreateViewHolder but not taking into account the current position I cannot create several different view there.

How can this be organized?

    3 answers 3

    To create different kind of items, you need to override the getItemViewType() adapter method, which will, depending on the condition, determine what kind of item is required in this position. In RecyclerView this mechanism has been improved and the onCreateViewHolder() method returns the ViewType value - which type of item is required at this position:

     class MyAdapter extends RecyclerView.Adapter <MyAdapter.ItemHolder> { private final int TYPE_ITEM1 = 0; private final int TYPE_ITEM2 = 1; public MyAdapter () { // конструктор Π°Π΄Π°ΠΏΡ‚Π΅Ρ€Π° } @Override public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v; switch (viewType) { // ΠΈΠ½Ρ„Π»Π΅ΠΉΡ‚ΠΈΠΌ Π½ΡƒΠΆΠ½ΡƒΡŽ Ρ€Π°Π·ΠΌΠ΅Ρ‚ΠΊΡƒ Π² зависимости ΠΎΡ‚ Ρ‚ΠΎΠ³ΠΎ, // ΠΊΠ°ΠΊΠΎΠΉ Ρ‚ΠΈΠΏ Π°ΠΉΡ‚Π΅ΠΌΠ° Π½ΡƒΠΆΠ΅Π½ Π² Π΄Π°Π½Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ case TYPE_ITEM1: v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item1, parent, false); break; case TYPE_ITEM2: v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item2, parent, false); } return new ItemHolder(v); } @Override public void onBindViewHolder( ItemHolder holder, int position) { // ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Ρ‚ΠΈΠΏ Π°ΠΉΡ‚Π΅ΠΌΠ° Π² Π΄Π°Π½Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ для заполнСния Π΅Π³ΠΎ Π΄Π°Π½Π½Ρ‹ΠΌΠΈ int type = getItemViewType(position); switch (type) { case TYPE_ITEM1: holder.mText1.setText("Π±ΠΈΠ½Π΄ΠΈΠΌ Π΄Π°Π½Π½Ρ‹Π΅ Π² Π°ΠΉΡ‚Π΅ΠΌ Ρ‚ΠΈΠΏΠ° 1"); break; case TYPE_ITEM2: holder.mText2.setText("Π±ΠΈΠ½Π΄ΠΈΠΌ Π΄Π°Π½Π½Ρ‹Π΅ Π² Π°ΠΉΡ‚Π΅ΠΌ Ρ‚ΠΈΠΏΠ° 2"); break; } } @Override public int getItemViewType(int position) { // условиС для опрСдСлСния Π°ΠΉΡ‚Π΅ΠΌ ΠΊΠ°ΠΊΠΎΠ³ΠΎ Ρ‚ΠΈΠΏΠ° Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ΡŒ Π² ΠΊΠΎΠ½ΠΊΡ€Π΅Ρ‚Π½ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ if (position == <условиС>) return TYPE_ITEM1; return TYPE_ITEM2; } public static class ItemHolder extends RecyclerView.ViewHolder{ TextView mText1; TextView mText2; public ItemHolder(View v) { super(v); mText1 = (TextView) v.findViewById(R.id.text1); mText2 = (TextView) v.findViewById(R.id.text2); } } 
    • And is it possible to use different Holder'y not to dump all in one? - wpbloger
    • @wpbloger You can use any number of holders you need and other optimizations and patterns. This is just a minimal example, the main purpose of which is the principle of using the getItemViewType() method - pavlofff
    • @wpbloger See, say, this example - pavlofff
    • And what if I get the data lists? I will explain: private List <TextNumeric> android; public DataAdapter (List <TextNumeric> android) {this.android = android; } Just now I’m getting not one type, but two, as in your example. Tell me how it is possible to correct please. - Morozov
    • Hmm, it is inconvenient that the developers made the return value of type int ... - Gleb

    You can write your adapter, as answered @pavlofff, you can use ready-made libraries: https://github.com/vivchar/RendererRecyclerViewAdapter https://github.com/sockeqwe/AdapterDelegates

      Who has questions for the example given above (or someone wants to understand in more detail), in addition to the example link to you!

      • It is not the answer to the question. To leave your comments or ask the author to clarify, leave a comment to the appropriate post. - From the queue of checks - Denis Bubnov
      • threw the link where the question is consecrated in more detail (it helped me, I decided to share). - Morozov