2

ListView在我的应用程序中有一个..ListView可以包含文本、图像或两者兼而有之。如果有图像,我想在第一行显示文本并在下一行显示图像集。我应该使用哪个?ViewFlipperViewPager其他什么?我一直在寻找有关如何执行此操作的示例。

任何指针都会很有帮助!

谢谢。

编辑:我想在列表视图的第二行中显示第一行中的文本和图片列表(3-5)

4

2 回答 2

1

如果您使用的是自定义适配器,我假设您是,那么您必须向其中提供某种数组。我认为您不能从适配器本身向 ListView 添加另一行,因此我会考虑解析数组或您用于填充列表视图的任何数据。从那里,创建另一个数组,该数组具有单独的文本和图像集条目(如果存在)。然后将最终数组输入适配器。将 JSONArray 与 JSONObjects 一起使用可能会更好,因为您可以为所有条目设置标签。

然后,在 getView() 方法中的适配器内部,检查条目类型是文本还是图像,并根据它更改它膨胀的 xml 布局。或者您可以使用相同的 xml 并使用 setVisibility() 控制不同的视图。

如果您想让图像在其列表项中水平滚动,您可能必须使用 Horizo​​ntalScrollView。我从未使用过它,但我知道在另一个滚动元素(ListView)中包含该滚动元素可能会给您带来一些问题。

于 2013-01-07T14:08:09.083 回答
0

创建带有图片的 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 类有一个返回项目视图的 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));

您可以在此处查看完整的源代码并在此处下载

于 2014-04-06T09:41:54.217 回答