简体   繁体   English

在列表视图中显示图片-Android

[英]Display Pictures in Listview - Android

I've a ListView in my app.. The ListView can contain text, images or both. 我有一个ListView在我的应用程序。该ListView可以包含文本,图像或两者兼而有之。 If there are images, I want to display the text in the first line and show the set of images in the next line.. Which one should I use?? 如果有图像,我想在第一行显示文本,在下一行显示图像集。我应该使用哪一个? ViewFlipper or ViewPager or anything else? ViewFlipperViewPager还是其他? I have been looking around for examples on how to do this. 我一直在寻找有关如何执行此操作的示例。

Any pointers would be quite helpful!! 任何指针都将非常有帮助!

Thanks. 谢谢。

EDIT: I want to display text in the first line and list of Pictures (3-5) in the second line in the listview 编辑:我想在第一行中显示文本,在列表视图的第二行中显示图片列表(3-5)

If you're using a custom adapter, which I assume you are, then you have to feed some kind of array into it. 如果您使用的是自定义适配器,那么我必须将某种数组放入其中。 I don't think you can add another row to a ListView from within the adapter itself, so I would consider parsing through the array, or whatever data you're using to populate your listview. 我认为您不能从适配器本身内部向ListView添加另一行,因此我将考虑通过数组或您用来填充listview的任何数据进行解析。 From that, create another array that has a separate entry for the text and image set (if it exists). 然后,创建另一个数组,该数组为文本和图像集(如果存在)具有单独的条目。 Then feed that final array into the adapter. 然后将最后一个阵列馈入适配器。 Using a JSONArray with JSONObjects might work better because then you can set labels for all the entries. 将JSONArray与JSONObjects一起使用可能会更好,因为您可以为所有条目设置标签。

Then, inside the adapter in the getView() method, check to see if the entry type is text, or images, and change which xml layout it inflates based on that. 然后,在适配器的getView()方法中,检查条目类型是文本还是图像,并根据其更改要膨胀的xml布局。 Or you could use the same xml and control the different views with setVisibility(). 或者,您可以使用相同的xml并通过setVisibility()控制不同的视图。

If you want to make the images scroll horizontally within their list item, you may have to use a HorizontalScrollView. 如果要使图像在列表项中水平滚动,则可能必须使用Horizo​​ntalScrollView。 I haven't ever used it, but I know it might give you some issues with having that scrolling element inside the other scrolling element (the ListView). 我从来没有使用过它,但是我知道在其他滚动元素(ListView)中放置该滚动元素可能会给您带来一些问题。

Creating a ListView with pictures has 3 main parts: 1- creating a simple listview in the main layout 用图片创建一个ListView有3个主要部分:1-在主布局中创建一个简单的listview

2- creating a custom listview item layout: 2-创建自定义listview项布局:

    <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>

and then create a custom ListView class: 然后创建一个自定义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/)

a constructor comes after that to create the customView object: 之后是构造函数来创建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;
}

The customView class has a getView method that returns the item view: 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;
}

and finally create the adapter for your list in the mainActivity: 最后在mainActivity中为您的列表创建适配器:

 yourList.setAdapter(new CustomList(MainActivity.this, countries, imageId));

you can see the full sourceCode and download it here here 您可以查看完整的sourceCode并在此处下载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM