'Problem in positioning two row layouts one below each other with multiView RecyclerView? Screenshot included

Here I use two viewHolders one for NoteViewHolder and another one for ChecklistViewHolder for a single adapter. I have two Lists with different data types in the recyclerView Adapter. I use SQLite Database and created two tables, one for notes and another one for checklists because I use different views for each row layout in the recyclerView. What I want is positioning notes and checklists according how they added. That is what I get in RecyclerView.enter image description here

Here if I add another note, it goes to note section for the yellow part, but I want it to go to after checklist for purple part.

I know how to bind these two views, I don't have problem with binding. My problem is with thee layout.

Adapter class

    public class AdapterClass extends RecyclerView.Adapter {

    List<NoteHelper> notesList;
    List<ChecklistHelper> checklistsList;
    public static final int LAYOUT_ONE = 0;
    public static final int LAYOUT_TWO = 1;


    public AdapterClass(List<NoteHelper> notesList, List<ChecklistHelper> checklistHelperList) {
        this.notesList = notesList;
        this.checklistsList = checklistHelperList;
    }


    @Override
    public int getItemViewType(int position) {
        if (position<notesList.size())
            return LAYOUT_ONE;

       else return  LAYOUT_TWO;


    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        if(viewType==LAYOUT_ONE){
            
            View view = layoutInflater.inflate(R.layout.layout_note, parent,false);
            NoteViewHolder noteViewHolder = new NoteViewHolder(view);
            return noteViewHolder;
        }

        if(viewType==LAYOUT_TWO){
            View view = layoutInflater.inflate(R.layout.layout_checklist, parent, false);
            return new ChecklistViewHolder(view);
        }
        return null;
    }
    
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

    }
    
    @Override
    public int getItemCount() {
        return notesList.size() + checklistsList.size();
    }

    class NoteViewHolder extends RecyclerView.ViewHolder{
        public NoteViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    class ChecklistViewHolder extends RecyclerView.ViewHolder{
        public ChecklistViewHolder(@NonNull View itemView) {
            super(itemView);
        }
   }}

Activity class

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    Button btnNote, btnChecklist;
    List<NoteHelper> listNotes;
    List<ChecklistHelper> checklistHelperList;
    DbHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recycler_view);
        btnNote = findViewById(R.id.btn_add_note);
        btnChecklist = findViewById(R.id.btn_add_checklist);
        recyclerView = findViewById(R.id.recycler_view);
        dbHelper = new DbHelper(this);

        listNotes = dbHelper.getNotes();
        checklistHelperList = dbHelper.getChecklists();
        
        AdapterClass adapterClass = new AdapterClass(listNotes, checklistHelperList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapterClass);
        listNotes = new ArrayList<>();

        btnNote.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,activity_add_note.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

            }
        });

        btnChecklist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, activity_add_checklist.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

            }
        });
    }}
public class activity_add_note extends AppCompatActivity {
Button btnSaveNote;
EditText etNote;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_note);

    btnSaveNote = findViewById(R.id.btn_save_note);
    etNote = findViewById(R.id.et_note);

    btnSaveNote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DbHelper  dbHelper = new DbHelper(getApplicationContext());
            dbHelper.getWritableDatabase();
            String note = etNote.getText().toString();
            dbHelper.insertNote(note);

            Intent intent = new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    });
}}

    public class activity_add_checklist extends AppCompatActivity {
    Button btnSaveChecklist;
    EditText etChecklist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_checklist);

        btnSaveChecklist = findViewById(R.id.btn_save_checklist);
        etChecklist = findViewById(R.id.et_checklist);
        btnSaveChecklist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String checklist = etChecklist.getText().toString();
                DbHelper dbHelper = new DbHelper(getApplicationContext());
                dbHelper.insertChecklist(checklist);

                Intent intent = new Intent(getApplicationContext(), MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source