-2

android Dynamic RecyclerView using two viewHolder

kanaga
  • 9
  • 4

1 Answers1

2

Have look on below code

add this code in your onCreate()

 mLayout = findViewById(R.id.layout);// your main xml ViewGroup like Linear or Relative or any other.
        int id=2;//use this varibale to identified your array list or array

        if(id==2)
            addTwoCard();
        else
            addThreeCard(); 

add these in your class.

private void addThreeCard() {
        View view = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.two_child, null);
        mLayout.addView(view);
    }

    private void addTwoCard() {
        View view = ((LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.three_child, null);
        mLayout.addView(view);
    }

and these XML in your project

two_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:padding="20dp"
        android:layout_marginTop="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:elevation="10dp"
        android:layout_height="100dp"/>

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:padding="20dp"
        android:layout_marginTop="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:elevation="10dp"
        android:layout_height="100dp"/>


</LinearLayout>

output:- enter image description here

three_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:layout_width="0dp"
        android:padding="20dp"
        android:layout_marginTop="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginLeft="40dp"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:elevation="10dp"
        android:layout_height="100dp"/>

        <android.support.v7.widget.CardView
            android:layout_width="0dp"
            android:padding="20dp"
            android:layout_marginTop="40dp"
            android:layout_marginRight="40dp"
            android:layout_marginLeft="40dp"
            android:layout_marginBottom="10dp"
            android:layout_weight="1"
            android:elevation="10dp"
            android:layout_height="100dp"/>


    </LinearLayout>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:padding="20dp"
        android:elevation="10dp"
        android:layout_margin="40dp"
        android:layout_height="100dp"/>



</LinearLayout>

output:- enter image description here

Hope your problem solved....

sushildlh
  • 8,986
  • 4
  • 33
  • 77