2016-06-25 99 views
1

我已經在我的MainActivity創建一個列表視圖的自定義ListView和另一個佈局文件:實現該行佈局,看起來像這樣在Android的

my row_layout

的問題是,我該如何實現呢?我在哪裏設置所有必須進入文字的字符串?哪裏去注意

我在互聯網上看過,但我可以找到一個與我的類似listView的人。

我知道對於字符串我可以使用數組,但我可以設置一個ImageViews數組嗎?

我會很感激,如果你能提供給我的,你給我解釋一下如何「把東西放在」我的ListView裏面詳細的解答

編輯:添加row.xml

<?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="wrap_content"> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="text" 
     android:textAllCaps="true" 
     android:textSize="36sp" 
     android:layout_marginTop="10dp" 
     android:fontFamily="sans-serif-condensed" 
     android:textStyle="bold" 
     android:textColor="@color/black" /> 

    <TextView 
     android:id="@+id/note" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/text" 
     android:text="note note note" 
     android:layout_marginTop="2dp" 
     android:fontFamily="sans_serif_medium" 
     android:textSize="24sp"/> 

    <ImageView 
     android:layout_width="200dp" 
     android:layout_height="140dp" 
     android:scaleType="fitCenter" 
     android:layout_alignParentEnd="true"/> 

</RelativeLayout> 
+0

你有沒有使用過的ListView或GridView的getview? –

+0

@janki不,這是我第一次使用listView – Daniele

+0

閱讀本文[教程](http://www.vogella.com/tutorials/AndroidListView/article.html) – user3502626

回答

3

您應該創建

  1. 一個類,(讓我們把它命名爲DataModel.java,你可以給出你想要的任何名字)代表你的數據:String text, note, image url;以及getter和setter方法。
  2. 類延伸ArrayAdapter。覆蓋getView()方法。將ArrayList<DataModel>傳遞給創建的自定義陣列適配器。創建一個構造函數和調用super調用ArrayAdapter的「構造:ArrayAdapter(Context context, int resource, List<T> objects)

在getView方法膨脹行佈局,並設置數據[與getView方法的位置參數匹配來自ArrayList中的索引]

在你的問題的情況下,我建議你學習更多關於view holder patternRecyclerView,這將有助於你瞭解的東西是如何工作的,以及他們如何應該高效地完成。

+0

@Daniele , 不用客氣。 –

0

你想要做的是創建一個自定義類並讓它擴展ArrayAdapter或BaseAdapter。在它的構造函數中,您可以將它傳遞給文本和圖像的String數組。在getView方法,虛增您的自定義行:

LayoutInflater layoutInflater = LayoutInflater.from(context); 
    View yourView = layoutInflater.inflate(R.layout.added_row, parent, false); 

然後,您可以使用陣列設置行的值,你傳遞給設定值:

TextView text = (TextView) yourView.findViewById(R.id.text); 
text.setText(text[position]); 
0

使用自定義適配器的方式來加載數據自定義列表視圖