2016-06-23 11 views
0

Ich habe ein cardView xmlaufpumpen zwei Ansichten für ein Array-Element in einem Recycler Adapter

Mein Modell Person ist

enter image description here

Mein Adapter ist recht generisch und Anbringen Code zeigt Fehler so Anbringen es nicht

initialisiere ich eine Arraylist für Adapter

persons = new ArrayList<>(); 
persons.add(new Person("Emma Wilson", "25 years old", 1)); 
persons.add(new Person("Lavery Maiss", "27 years old", 2)); 
persons.add(new Person("Lillie Watts", "35 years old", 3)); 

Wenn Alter = 25 wie Emma Wilson für eine Person, ich die Ansicht (cardView.xml) zweimal aufblasen wollen.

Ich kann die ArrayList nicht ändern.

Ich möchte es mit dem Adapter selbst tun. Ist das möglich und wie?

+0

Können Sie den Code aus dem Adapter teilen? –

+0

@KevinWallis: Ich habe es versucht, aber es zeigt Fehler, dass 4 Leerzeichen erforderlich.Könnte es nicht lösen. [link] (http://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465) – user3423235

Antwort

1

Sie könnten eine Ansicht erstellen, die zweimal cardView.xml enthält.

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

    <include android:id="@+id/cv1" layout="@layout/card_view.xml" /> 
    <include android:id="@+id/cv2" layout="@layout/card_view.xml" /> 

</LinearLayout> 

Dann in Ihrem Adapter,

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // Get the data item for this position 
    Person person = getItem(position);  
    // Check if an existing view is being reused, otherwise inflate the view 
    if (person.age == 25) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.twice.xml, parent, false); 
    }else{ 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.once.xml, parent, false); 
    } 

    //populate views... 


}