我在我的應用程序中有ListView
.. ListView
可以包含文本,圖像或兩者。如果有圖像,我想在第一行顯示文本,並在下一行顯示圖像集合。我應該使用哪一個? ViewFlipper
或ViewPager
還是別的?我一直在四處尋找如何做到這一點的例子。在Listview中顯示圖片 - Android
任何指針會相當有幫助!
謝謝。
編輯:我想在第二行中的列表視圖
我在我的應用程序中有ListView
.. ListView
可以包含文本,圖像或兩者。如果有圖像,我想在第一行顯示文本,並在下一行顯示圖像集合。我應該使用哪一個? ViewFlipper
或ViewPager
還是別的?我一直在四處尋找如何做到這一點的例子。在Listview中顯示圖片 - Android
任何指針會相當有幫助!
謝謝。
編輯:我想在第二行中的列表視圖
如果您使用自定義適配器,我以爲你是顯示圖片的第一行和列表(3-5)的文本,那麼你必須將某種數組提供給它。我不認爲你可以在適配器本身中添加另一行到ListView,所以我會考慮通過數組解析,或者使用任何數據填充你的列表視圖。從中創建另一個數組,其中包含文本和圖像集的單獨條目(如果存在)。然後將最終數組饋入適配器。對JSONObjects使用JSONArray可能會更好,因爲可以爲所有條目設置標籤。
然後,在getView()方法中的適配器內部,檢查條目類型是文本還是圖像,並根據它改變它膨脹的xml佈局。或者你可以使用相同的XML並通過setVisibility()控制不同的視圖。
如果要使圖像在其列表項目中水平滾動,可能必須使用HorizontalScrollView。我從來沒有使用過它,但我知道它可能會給你帶來滾動元素在其他滾動元素(ListView)內的一些問題。
你現在就在:)我已經在JSONObjects上使用了JSONArray,並用適配器提供了Listview。圖片將是動態的,所以我打算在getView方法中以編程方式創建ImageView ..很好嗎?我嘗試使用ViewPager掃描圖像..但是,當我在Listview中使用ViewPager時出現性能問題。您有任何解決方案嗎? – Naveen
在ListView中執行ViewPagers可能會很麻煩,因爲它們都需要適配器。我將有兩個包含項目信息的xml佈局,一個用於文本,另一個用於圖像。這樣,您就不必擔心編程創建ImageView,並且會干擾代碼中的所有LayoutParameters垃圾。 – Wenger
謝謝@溫格..這是有道理的! – Naveen
創建帶有圖片一個ListView具有3個主要部分: 1-在主佈局
2-創建簡單列表視圖創建自定義列表視圖項佈局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</LinearLayout>
,然後創建一個自定義的ListView類:
private final Activity context; // the context view of the list
private final String[] countries; // the list of countries
private final Integer[] imageId; // the list of images that you already uploaded to your @Drawable file (res/drawable-xdpi/)
一個構造函數之後到了cre吃了customView對象:
//class constructor
public CustomList(Activity context,String[] countries, Integer[] imageId)
{
super(context, R.layout.list_item, countries);
this.context = context;
this.countries = countries;
this.imageId = imageId;
}
的customView類有一個getView方法返回的項目視圖:
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_item, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(countries[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
,並最終創建您在MainActivity列表中選擇適配器:
yourList.setAdapter(new CustomList(MainActivity.this, countries, imageId));
你可以看到完整的sourceCode並在這裏下載here
儘管此鏈接可能會回答問題,但最好在此處包含答案的重要部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – donfuxx
thx,這樣做,但這爲讀者節省了時間,並提供了更多細節和下載代碼的能力。 –
you shou ld進入可擴展列表視圖..嘗試使用此http://imobiledevelopment.blogspot.in/2011/06/expandablelistview-on-android.html – itsrajesh4uguys
瞭解如何製作自定義列表視圖:http://www.thepcwizard.in /2012/09/android-creating-custom-listview-for.html – ThePCWizard