如何通过getview为多个imageViews设置ImageResource

时间:2012-07-27 10:44:42

标签: android android-layout android-intent android-emulator

这是我的自定义列表视图的行。如您所见,有三个imageView。我应该如何覆盖

public View getView(int position, View convertView, ViewGroup parent)

我的适配器类的

,无论我在列表中有多少图像视图,我都可以设置imageView.setImageResource。非常感谢任何帮助。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
<RelativeLayout
        android:id="@+id/LinearLayout01"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
<ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10px" />
<ImageView
        android:id="@+id/image1"
        android:layout_toRightOf= "@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10px" />

<ImageView
        android:id="@+id/image2"
        android:layout_toRightOf= "@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10px" />
</RelativeLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:2)

在适配器中使用持有者类

// View Holder for fast accessing List Row
private class ViewHolder {
    public ImageView image;
    public ImageView image1;
    public ImageView image2;
}

和getView方法

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.row_layout,null);
                holder.image = (ImageView)convertView.findViewById(R.id.image);
                holder.image1 = (ImageView)convertView.findViewById(R.id.image1);
                holder.image2 = (ImageView)convertView.findViewById(R.id.image2);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            // Setting Image Resource here
            holder.image.setImageResource(R.drawable.icon);
            holder.image1.setImageResource(R.drawable.icon);
            holder.image2.setImageResource(R.drawable.icon);

            // return the listview row
            return convertView;
        }

在ListView行布局why you take LinearLayout and then RelativeLayout inside it中,只需 take RelativeLayout as a root node.