如何实现连续两个并行imageview的列表视图

How to implement list view with two parallel imageview in a row

好吧,我不知道这个问题的正确标题应该是什么,但我的问题很神奇,不知道是否可能。这是我的问题

我有一系列图像,我想将它们设置在 ListView 中。以下是 ListView 行 Xml。(名为 anim_list_row)

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100"
    >


    <ImageView
        android:layout_width="0dp"
        android:layout_height="350dp"
        android:scaleType="fitXY"
        android:id="@+id/iv_dummy_left"
        android:layout_marginLeft="5dp"
        android:layout_weight="50"/>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="350dp"
        android:scaleType="fitXY"
        android:id="@+id/iv_dummy_right"

        android:layout_weight="50"
        android:layout_marginRight="5dp"/>
</LinearLayout>

在这里你可以看到我想在两个不同的图像视图中设置 2 个不同的图像,比如左右 ImageView。以下是我的适配器 class

public class MListAdapter extends BaseAdapter {


private Context context;
private ArrayList<AnimListItems> mAnimListItems;

public MListAdapter(Context context, ArrayList<AnimListItems> mAnimListItems){
    this.context = context;
    this.mAnimListItems = mAnimListItems;
}


@Override
public int getCount() {
    return mAnimListItems.size();
}

@Override
public Object getItem(int position) {
    return navDrawerItems.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.anim_list_row, null);
    }

    ImageView imgIconLeft = (ImageView) convertView.findViewById(R.id.iv_dummy_left);
    ImageView imgIconRight = (ImageView) convertView.findViewById(R.id.iv_dummy_right);
    //TextView txtTitle = (TextView) convertView.findViewById(R.id.title);


    imgIconLeft.setImageResource(navDrawerItems.get(position).getIcon());
     //if I do not do +1 here it sets same image to both left and right ImageView 
    imgIconRight.setImageResource(navDrawerItems.get(position+1).getIcon());
    // txtTitle.setText(navDrawerItems.get(position).getTitle());



    return convertView;
}

}

所以这是一个问题,这个列表视图正在工作,但它正在将相同的图像分配给行中的两个 ImageView。如果我按以下方式 +1

imgIconRight.setImageResource(navDrawerItems.get(position+1).getIcon())

然后它有助于更​​改行右侧的图像视图,但第二行在其第一个图像视图中重复图像(我的意思是第二行左侧 ImageView 中的图像与第一行右边的 ImageView。)

那么

的解法是什么

尝试使用两个不同的 array/arraylist 分别存储左右图像。 将标签设置为左图的 l+position 将标签设置为右图的 r+position 您可以通过

识别点击的图片
 imgIconLeft.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
        if(v.getTag.toString().contains("l"){
      // then it's a left button.

String position=v.getTag.toString().subString(1,v.getTag.toString().length()-1);
// to get position

}
});

您好,如果您想使用一个阵列,那么我已经针对您的问题快速制作了一个演示示例,我认为它会对您有所帮助。

MainActivity.java

public class MainActivity extends ActionBarActivity {

    Item item;
    ArrayList<Object> obj = new ArrayList<Object>();
    MListAdapter adapter;
    ListView lv;

    int[] arrEnabledImageIds = new int[] { R.drawable.ic_launcher,
            R.drawable.ic_delete_icon, R.drawable.ic_delete_icon,
            R.drawable.ic_launcher, R.drawable.ic_delete_icon,
            R.drawable.ic_launcher };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.listView1);

        for (int i = 0; i < arrEnabledImageIds.length; i++) {
            Item itemInfo = new Item();
            itemInfo.image1 = arrEnabledImageIds[i];
            i++;
            itemInfo.image2 = arrEnabledImageIds[i];

            obj.add(itemInfo);
        }

        adapter = new MListAdapter(this, R.layout.row, obj);
        lv.setAdapter(adapter);

    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

Item.java

public class Item implements Serializable {

    public static final long serialVersionUID = 1L;
    public int image1;
    public int image2;
}

row.xml

<?xml version="1.0" encoding="utf-8"?>
<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/ivLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/ivRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

MListAdapter.java

public class MListAdapter extends ArrayAdapter<Object> {

    private Context context;
    int resId;
    private ArrayList<Object> mAnimListItems;

    public MListAdapter(Context context, int textViewResourceId,
            ArrayList<Object> mAnimListItems) {
        super(context, textViewResourceId, mAnimListItems);
        this.resId = textViewResourceId;
        this.context = context;
        this.mAnimListItems = mAnimListItems;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(resId, null);
        }

        ImageView imgIconLeft = (ImageView) convertView
                .findViewById(R.id.ivLeft);
        ImageView imgIconRight = (ImageView) convertView
                .findViewById(R.id.ivRight);
        // TextView txtTitle = (TextView) convertView.findViewById(R.id.title);

        Item item = (Item) mAnimListItems.get(position);

        imgIconLeft.setImageResource(item.image1);
        // if I do not do +1 here it sets same image to both left and right
        // ImageView
        imgIconRight.setImageResource(item.image2);
        // txtTitle.setText(navDrawerItems.get(position).getTitle());

        return convertView;
    }
}