Android列表视图,每行包含备用项目

时间:2012-07-05 10:44:45

标签: android android-listview

android listview第一行左侧图像和右侧文本,第二行恰好在第一行的对面..任何人都可以帮我一个示例代码吗?或者在listview中是否可能?

1 个答案:

答案 0 :(得分:1)

我们会做以下的替代项目

    row.xml
    <RelativeLayout>
    <ImageView
    android:visibility="gone"
    android:id="@+id/img1"
    android:layout_alignParentLeft="true"
    />
    <TextView
    android:id="@+id/txt1"
    android:layout_alignParentLeft="true"
    />
    <ImageView
    android:id="@+id/img2"
    android:layout_alignParentRight="true"
    />
    <TextView
    android:id="@+id/txt1"
    android:layout_alignParentRight="true"
    android:visibility="gone"
    />
    </RelativeLayout>

//In Adapter
    public View getView(int position, View convertView, ViewGroup parent) 
    {
    if(position%2==0)
    {
    //left side image and right side text
    img2.setImgResource();
    img2.setVisibility(VIEW.GONE);
    txt2.setVisibility(VIEW.GONE);
    img1.setVisibility(VIEW.VISABLE);
    txt1.setVisibility(VIEW.VISABLE);
    txt2.setText();
    }
    else
    {
    //left side text and right side image code

    img2.setImgResource();
    img1.setVisibility(VIEW.GONE);
    txt1.setVisibility(VIEW.GONE);
    img2.setVisibility(VIEW.VISABLE);
    txt2.setVisibility(VIEW.VISABLE);
    txt2.setText();
    }
    }