2017-08-17 93 views
0

我想用兩個不同的佈局使用ArrayAdapter充氣listView。Android如何充氣ListView與兩個不同的項目

我想顯示一些用戶帖子。如果它包含圖片,則使用一個佈局,如果它不使用第二個。

我這樣做,到目前爲止

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Post post = getItem(position); 

    boolean noImg = post.mImgUrl1 == null && post.mImgUrl2 == null; 

    if (convertView == null){ 

     if (noImg) 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_item_no_img, parent ,false); 
     else 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_item, parent, false); 
    } 

    if (noImg == false) { 
     mTitle = (TextView) convertView.findViewById(R.id.tv_titlePost); 
     mText = (TextView) convertView.findViewById(R.id.tv_textPost); 
     mImage = (ImageView) convertView.findViewById(R.id.iv_postimg); 
    } 

    else { 
     mNoImgTitle = (TextView) convertView.findViewById(R.id.tv_noImgTitle); 
     mNoImgText = (TextView) convertView.findViewById(R.id.tv_noImgText); 
    } 

    if (noImg) { 
     mNoImgTitle.setText(post.mTitle); 
     mNoImgText.setText(post.mText); 

     if (post.mImgUrl1 != null) 
      Glide.with(mImage.getContext()).load(post.mImgUrl1).into(mImage); 

     else Glide.with(mImage.getContext()).load(post.mImgUrl2).into(mImage); 
    } 
    else { 
     mNoImgTitle.setText(post.mTitle); 
     mNoImgText.setText(post.mText); 
    } 

    return convertView; 
} 

但我意識到,從第二佈局textviews無法通過findViewById功能被發現。它總是返回一個空指針。

我的解決方案是什麼?我應該只做一個佈局,並以某種方式使用可視性來欺騙它?或者有一種方法?

+0

的可能的複製[Android的列表視圖ArrayAdapter與兩個佈局](https://stackoverflow.com/questions/29721753/android-listview-arrayadapter-with-two-layouts) –

回答

0

有幾個不同的情景是你們等會看到當getView()叫做:

  1. convertView爲空:在這種情況下,只需創建要取決於noImg需要佈局;

  2. convertView被定義,但是類型錯誤。在這種情況下,您需要放棄舊視圖並創建您需要的視圖;

  3. convertView被定義,但它是正確的類型,在這種情況下,我們什麼也不做。

以下代碼是確保您始終使用正確佈局並可以找到您期望的ID的一種方法。此代碼在充氣佈局中使用標籤來標識可以檢查的佈局類型。

if (convertView == null || noImg != (boolean) convertView.getTag()) { 
    // Either there is no view defined or it is the wrong type. Create 
    // the type we need. 
    if (noImg) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_item_no_img, parent, false); 
    } else { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_item, parent, false); 
    } 
    convertView.setTag(noImg); 
} 
0

使用BaseAdapter可以創建更復雜的佈局。嘗試使用它。

下面是使用適合您的情況的例子:

活動:

public class TestActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    String[] data={"false","false","true","true","false","true"}; 
    ListView listViewTest=(ListView)findViewById(R.id.listViewTest); 
    TestAdapter testAdapter=new TestAdapter(data); 
    listViewTest.setAdapter(testAdapter); 
} 
} 

適配器:

public class TestAdapter extends BaseAdapter { 
private String[] data; 


TestAdapter(String data[]) { 
    this.data = data; 
} 

@Override 
public int getCount() { 
    return data.length; 
} 

@Override 
public Object getItem(int i) { 
    return data[i]; 
} 

@Override 
public long getItemId(int i) { 
    return i; 
} 


public View getView(int position, View convertView, ViewGroup parent) { 
    if (data[position].equals("false")) { 
     convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_no_img, parent, false); 
    } else if (data[position].equals("true")) { 
     convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_img, parent, false); 
    } 
    return convertView; 
} 
} 

佈局:

activity_main

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 
<ListView 
    android:id="@+id/listViewTest" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</ListView> 

list_item_no_img

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textViewTest" 
    android:gravity="center" 
    android:layout_centerInParent="true" 
    android:textSize="32sp" 
    android:text="Test"/> 
</RelativeLayout> 

list_item_img

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textViewTest" 
    android:gravity="center" 
    android:layout_centerInParent="true" 
    android:textSize="32sp" 
    android:text="Test"/> 
<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@mipmap/ic_launcher" 
    android:layout_marginStart="5dp" 
    android:layout_marginLeft="5dp" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@+id/textViewTest" 
    android:layout_toEndOf="@+id/textViewTest" /> 
</RelativeLayout> 

結果:

相關問題