【问题标题】:How to offset row margins of a GridView如何偏移 GridView 的行边距
【发布时间】:2012-12-21 21:21:47
【问题描述】:

我有一个充满图像的网格视图,并且网格视图的行按这样的列对齐

1_2_3

4_5_6

7_8_9

但是我想偏移某些行以获得类似的东西

1_2_3_

_4_5_6

7_8_9_

以便将第二行和未知数量的附加行移到右侧。

一种解决方案是制作多个网格视图并相应地移动它们,但是当我有很多行时这将不起作用。另一种方法是以某种方式更改 ImageAdapter 中的边距,使用位置来获取行,但我无法在不崩溃的情况下完成这项工作。是否尝试编辑 ImageAdapter 中的 LayoutParams 甚至是处理此类事情的正确方法?

【问题讨论】:

    标签: java android gridview margin


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-11
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      • 2019-05-30
      • 1970-01-01
      相关资源
      最近更新 更多