我的应用程序中有一个ListView ..ListView可以包含文本和/或图像。如果有图像,我想在第一行显示文本,在下一行显示一组图像。我应该使用哪一个??ViewFlipper还是ViewPager或其他任何东西?我一直在四处寻找如何做到这一点的例子。
任何指针都会很有帮助!
谢谢。
编辑:我想在listview的第一行显示文本,在第二行显示图片列表(3-5)
发布于 2013-01-07 22:08:09
如果您使用的是自定义适配器(我假设您是这样的),那么您必须向它提供某种类型的数组。我不认为可以从适配器本身向listview添加另一行,因此我会考虑解析整个数组,或者您用来填充列表视图的任何数据。在此基础上,创建另一个数组,该数组具有单独的文本和图像集条目(如果存在)。然后将最后的数组输入适配器。使用带有JSONObjects的JSONArray可能会更好,因为这样您就可以为所有条目设置标签。
然后,在适配器内部的getView()方法中,检查条目类型是文本还是图像,并基于此更改它所膨胀的布局。或者,您可以使用相同的xml并使用setVisibility()控制不同的视图。
如果要使图像在其列表项内水平滚动,则可能必须使用HorizontalScrollView。我从未使用过它,但我知道它可能会让您在其他滚动元素( ListView)中使用滚动元素时遇到一些问题。
发布于 2014-04-06 17:41:54
创建一个带有图片的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/)后面是一个构造函数,用于创建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类有一个返回item视图的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));您可以在here查看完整的sourceCode并进行下载
https://stackoverflow.com/questions/14196641
复制相似问题