【问题标题】:GridView with different column sizes具有不同列大小的 GridView
【发布时间】:2011-10-02 19:56:40
【问题描述】:

尝试制作一个可滚动的高分列表,如下所示:

foo         1000
bar         876
foobar      500
foobarfoo   1

我目前正在使用 GridView。我想将名称列宽设置为屏幕的 60%,将分数列宽设置为 40%。有可能吗?

目前我正在尝试通过服装适配器。这是它的 getview 函数:

public View getView(int position, View convertView, ViewGroup parent) {
    TextView tv;
    if (convertView == null) {
        tv = new TextView(context);
        tv.setTextSize(25);
        tv.setTextColor(Color.WHITE);              

        if (position % 2 == 0)
        {
            tv.setLayoutParams(new GridView.LayoutParams((width/10)*6, 50));
        }
        else
        {
            tv.setLayoutParams(new GridView.LayoutParams((width/10)*4, 50));
        }
    }
    else {
        tv = (TextView) convertView;
    }

    tv.setText(texts[position]);
    return tv;
}

布局由gridview和底部的按钮构建。 XML 文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/top">
    <GridView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="2"
              android:id="@+id/grid"
              android:numColumns="2"
              android:columnWidth="0dp"
              >
    </GridView>
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/backbutton" android:text="@string/backstr"></Button>

</LinearLayout>

那么问题又来了:是否可以将 GridView 设置为允许添加不同大小的列?如果是,那么我的方法好吗? (可能不是,因为它不起作用:))我错过了什么吗?

提前谢谢你!

【问题讨论】:

  • 您可以尝试将列宽设置为 wrap_content 而不是 0dp,然后看看它是否有效?
  • 遗憾的是,这不是列宽的有效值。但是感谢您的评论!
  • 在这种情况下,您可以使用 ListView 而不是 GridView,并且对于每一行,使用您在适配器的 getView 方法中填充的具有两个文本视图(最好是线性布局)的布局。
  • 用这个方法可以滚动列表吗?
  • 是的,ListViews 是可滚动的 :)

标签: android android-gridview


【解决方案1】:

我们必须为一个项目这样做,并且不想使用 GridView 以外的任何东西,因为该功能重复了,但是在一个 GridView 上,我们需要稍微不同的视图配置(但仍然使用适配器和所有其他好东西)。我们发现,如果你覆盖 GridViewlayoutChildren 函数,你可以做到这一点,尽管它的文档记录很差。

我们的实现会检查特殊视图以及新布局是否已经实现:

@Override
protected void layoutChildren(){
    super.layoutChildren();
    if(!isSpecial || layoutAlreadySet) return;
    layoutAlreadySet = true;
    //Implement special layout....
}

【讨论】:

  • 嘿史蒂夫。我在为我的问题寻找解决方案时遇到了这个线程。我尝试在我的 GridView 中显示两种不同类型的自定义视图。 A 型的宽度为 150dp,B 型的宽度为 310dp。我总是希望单元格包装其内容。你的解决方案有可能吗?
  • 这个解决方案只是为了让 GridView 做一些不同于它通常做的事情,但仍然向它提供来自适配器的信息。使用不同的布局对象(例如 LinearLayout)可能更容易执行您尝试执行的操作。也就是说,我们确实使用此代码在 GridView 中实现了一个自定义大小的单元格,所以如果这是您需要做的事情的类型,那么这是您可以采用的方法。
【解决方案2】:

像魅力一样工作!非常感谢 Abhinav,他提出了很好的建议。这是每个有同样问题的人的代码:

public View getView(int position, View convertView, ViewGroup parent) {
    TextView tv1;
    TextView tv2;
    LinearLayout ll;
    if (convertView == null) {
        tv1 = new TextView(context);
        tv1.setTextSize(25);
        tv1.setTextColor(Color.WHITE);
        tv1.setGravity(Gravity.LEFT);
        tv1.setPadding(5, 5, 5, 5);
        tv1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                                                          LinearLayout.LayoutParams.FILL_PARENT,
                                                          (float) 3.0));

        tv2 = new TextView(context);
        tv2.setTextSize(25);
        tv2.setTextColor(Color.WHITE);  
        tv2.setGravity(Gravity.RIGHT);
        tv2.setPadding(5, 5, 5, 5);
        tv2.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                                                              LinearLayout.LayoutParams.FILL_PARENT,
                                                              (float) 4.0));

        ll = new LinearLayout(context);
        ll.setOrientation(0);
        ll.setPadding(5, 5, 5, 10);

        tv1.setText(names[position]);
        tv2.setText(scores[position]);

        ll.addView(tv1);
        ll.addView(tv2);
    }
    else {
        ll = (LinearLayout) convertView;
        tv1 = (TextView) ll.getChildAt(0);
        tv2 = (TextView) ll.getChildAt(1);

        tv1.setText(names[position]);
        tv2.setText(scores[position]);
    }

    return ll;
}

还有 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/top">
    <ListView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_weight="2"
              android:numColumns="2"
              android:columnWidth="0dp"
              android:id="@+id/list">
    </ListView>
    <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/backbutton" android:text="@string/back"></Button>

</LinearLayout>

如果您认为您可以改进此代码(因为我是该领域的新手),请不要犹豫,回复!提前致谢!

【讨论】:

  • 很好的解释,但如果我有 3 个列,那么如何实现呢??
猜你喜欢
  • 2012-10-03
  • 2013-09-04
  • 2015-10-29
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多