您可以使用具有两种布局的两种类型的视图,例如:
在您的适配器中,您可以实现:
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (isImageLeft(position)) {
return VIEW_TYPE_LEFT_IMAGE; //TODO: you should define it
} else {
return VIEW_TYPE_RIGHT_IMAGE;//TODO: you should define it
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null){
if (getItemViewType(int position) == VIEW_TYPE_LEFT_IMAGE){
view = inflater.inflate(R.layout.layout_image_left, null);//TODO: you should have set the reference of LayoutInflater
}else{
view = inflater.inflate(R.layout.layout_image_right, null);
}
}
//.......
}
在这两个布局文件中,您可以设置一个 LinearLayout (或您喜欢的其他布局),其中 ImageView 分别带有左边距或右边距。例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp" />
</LinearLayout>