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

时间:2015-03-27 05:47:33

标签: android listview imageview

嗯,我不知道问题的正确名称应该是什么,但我的问题非常惊人,不知道是不是可能。这是我的问题

我有一系列图像,我想在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>

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

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(位置+ 1).getIcon())

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

那么什么是

的解决方案
  • 重复行中的图像。
  • 如何获取图像的每个ImageView ID及其资源ID,以便我可以知道哪个图像已被点击。然后我就可以将该图像设置为另一个活动的ImageView。我的意思是我想知道用户点击了哪个图像,所以我想在其他活动的ImageView中设置相同的图像。

2 个答案:

答案 0 :(得分:1)

尝试使用两个不同的阵列/ arraylist分别存储左侧和右侧图像。 settag为l +左图像的位置 settag为右图像的r +位置 您可以通过

识别点击的图像
 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

}
});

答案 1 :(得分:1)

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

<强> 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;
    }
}