Skip to content Skip to sidebar Skip to footer

Two Gridview With One Scroll In Android

In a activity i need two GridView each of them take as much as it needs as height. No gridview has separate scrollbar but they have only one scrollbar. I tried something like this.

Solution 1:

you can set the GridView Fixed ,and use ScrollView include them

publicclassFixedGridViewextendsGridView {

publicFixedGridView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

publicFixedGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

publicFixedGridView(Context context) {
    super(context);
}

@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    intexpandSpec= MeasureSpec.makeMeasureSpec(
            Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);

}

}

Solution 2:

Extending on tesla1984's answer, here is my complete working code. It works as expected:

  • Create a new class named FlexGridView as follows, and save it on a known location inside your project. (I used a folder called "views")

    package your.project.id.views;
    
    publicclassFixedGridViewextendsGridView {
    
      publicFixedGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
      }
    
      publicFixedGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      publicFixedGridView(Context context) {
        super(context);
      }
    
      @OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        intexpandSpec= View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
      }
    }
    

Then, on your XML, do something like this:

<ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><your.product.id.views.FixedGridViewandroid:id="@+id/gridViewPublic"android:layout_width="fill_parent"android:layout_height="wrap_content"android:numColumns="1"android:stretchMode="columnWidth"android:horizontalSpacing="10"android:verticalSpacing="10"android:background="@color/transparent_color" /><your.product.id.views.FixedGridViewandroid:id="@+id/gridViewPrivate"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginTop="10"android:layout_marginBottom="10"android:numColumns="2"android:stretchMode="columnWidth"android:horizontalSpacing="10"android:verticalSpacing="10"android:background="@color/transparent_color" /></LinearLayout></ScrollView>

Post a Comment for "Two Gridview With One Scroll In Android"