我有一个充满图像的网格视图,网格视图的行按照这样的列对齐
1_2_3
4_5_6
7_8_9
但是我想要偏移某些行来获得类似的东西
1_2_3 _
_4_5_6
7_8_9 _
这样第二行和未知数量的附加行就会移到右边。
一种解决方案是制作多个网格视图并相应地移动它们,但是当我有很多行时这不会起作用。另一种方法是以某种方式改变ImageAdapter中的边距,使用位置来获取行,但是我没有能够在没有崩溃的情况下完成这项工作。试图在ImageAdapter中编辑LayoutParams甚至是正确的方法来进行类似的事情吗?
答案 0 :(得分:1)
您可以使用两种类型的视图,其中包含两种布局,例如:
在你的适配器中,你可以实现:@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>