GridView设置标题视图高度

3
我正在实现一个带有HeaderView(GridViewWithHeaderAndFooter)的GridView。我希望HeaderView占据屏幕高度的1/3。问题在于,在header_view.xml中,我必须设置layout_height值,否则该视图将不可见。
以下是我的Activity:
    public class TestGVHeader extends Activity  {

    Context context = this;

    private GridViewWithHeaderAndFooter gridView;
    private ProductsGridViewAdapter gridViewAdapter;

    private ViewPager viewPager;
    private PagerAdapter viewPagerAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_gvheader);

        gridView = (GridViewWithHeaderAndFooter) findViewById(R.id.gridView);
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View headerView = layoutInflater.inflate(R.layout.test_header_view, null);

        gridView.addHeaderView(headerView);

        gridViewAdapter = new ProductsGridViewAdapter(this, getProducts());
        gridView.setAdapter(gridViewAdapter);

        viewPager = (ViewPager) headerView.findViewById(R.id.viewpager);
        viewPagerAdapter = new CataloguePagerAdapter(context, this, this.getCatalogues());
        viewPager.setAdapter(viewPagerAdapter);

    }
}

活动布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<in.srain.cube.views.GridViewWithHeaderAndFooter
    android:id="@+id/gridView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:horizontalSpacing="1sp"
    android:numColumns="5"
    android:stretchMode="columnWidth"
    android:verticalSpacing="1sp"
    android:background="@android:color/white">
</in.srain.cube.views.GridViewWithHeaderAndFooter>

以及标题视图布局:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="450dp"
        android:layout_marginBottom="5dp"
        android:gravity="center"
    android:background="@android:color/white"/>

</FrameLayout>

有人知道如何将android:layout_height="450dp"设置为屏幕高度的1/3,而不是固定值吗?谢谢。

try android:weight="0.65" - M D
你是指 android:layout_weight="0.65" 吗?如果我设置了 android:layout_weight="0.65"android:layout_height="0dp",那么 ViewPager 就不可见了 :( - Ale
android:layout_height =“wrap_content”android:layout_weight =“0.06”设置。 - M D
不起作用。如果我设置 android:layout_height="wrap_content" 或者 android:layout_height="match_parent"ViewPager 就不可见了,无论我在 android:layout_weight 中设置什么。 - Ale
1个回答

2

使用自定义的头视图 ViewGroup 很容易实现,对于您的情况,可以使用 FrameLayout。您只需要重写 onMeasure 方法并将高度计算为屏幕高度的 1/3 即可。因此,首先在您的项目中创建以下自定义视图类:

public class OneThreeScreenFrameLayout extends FrameLayout {

    public OneThreeScreenFrameLayout(Context context) {
        super(context);
    }

    public OneThreeScreenFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public OneThreeScreenFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        int screenHeight = getResources().getDisplayMetrics().heightPixels;
        int oneThreeHeight = MeasureSpec.makeMeasureSpec(screenHeight / 3,
                MeasureSpec.EXACTLY);
        super.onMeasure(widthSpec, oneThreeHeight);
    }
}

然后将其用作您的header_view根。请注意,我们将其高度设置为0dp,因为我们无论如何都会更改它:

<?xml version="1.0" encoding="utf-8"?>
<your.package.name.OneThreeScreenFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="0dp">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:gravity="center"
        android:background="@android:color/white"/>

</your.package.name.OneThreeScreenFrameLayout>

这就是全部内容了!现在你的标题视图高度是屏幕高度的1/3。以下是示例:

enter image description here


看起来它会工作。在我的情况下,主要基础是Fragment,它是一个LinearLayout,其中包含ViewPager,然后我将其作为headerView添加到GridLayout中,使用GridViewWithHeaderAndFooter。一切都按预期进行,但如果我们有更多的项并且快速上下滚动,则会在onMeasure()的最后一行崩溃。super.onMeasure(widthSpec, oneThreeHeight); - GreenROBO
所以,如果您的标题视图的根视图是 LinearLayout,那么您需要创建 OneThreeScreenLinearLayout extends LinearLayout 并在 onMeasure 方法中执行相同的操作。然后使用它来替换 LinearLayout - romtsn
好的。让我试一下。如果它能工作,我会非常高兴。 - GreenROBO
你必须这样做。onMeasure方法的实现将是相同的。 - romtsn
如果它无法正常工作,您可以在此粘贴异常日志。 - romtsn
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接