Android : GridView set header View height

on Wednesday, January 28, 2015


I'm implementing a GridView with a Header View (GridViewWithHeaderAndFooter) . I want that the HeaderView takes 1/3 of the screen height. The problem is that in the header_view.xml I have to set a layout_height value, otherwise the view is not visible.


Here is my 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);

}
}


The activity layout:



<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>


and the header view layout:



<?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>


Does anybody knows how to set the android:layout_height="450dp" to 1/3 of the screen height instead of a fixed value? Thanks.


0 comments:

Post a Comment