Two gridView with one scroll in android

允我心安 提交于 2020-01-04 15:26:29

问题


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.

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
       android:id="@+id/gridView1"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"  //as much height as it needs
       android:background="#9C5353"
       android:gravity="center"
       android:horizontalSpacing="20dp"
       android:numColumns="2"
       android:layout_weight="1"
       android:scrollbars="none"
       android:stretchMode="columnWidth"
       android:verticalSpacing="20dp" >      
       </GridView>


    <GridView 
    android:id="@+id/gridView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"   //as much height as it needs
    android:scrollbars="none"
    android:numColumns="1"
    android:layout_weight="1"
    android:horizontalSpacing="20dp"
    android:verticalSpacing="20dp"
    android:gravity="center"
    android:stretchMode="columnWidth" > 
    </GridView>

</LinearLayout>

I tried many times but cant get any solution. Please can anyone have any solution of this??


回答1:


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

public class FixedGridView extends GridView {

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

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

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

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

}

}




回答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;
    
    public class FixedGridView extends GridView {
    
      public FixedGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
      }
    
      public FixedGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      public FixedGridView(Context context) {
        super(context);
      }
    
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
      }
    }
    

Then, on your XML, do something like this:

  <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <your.product.id.views.FixedGridView
                android: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.FixedGridView
                android: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>


来源:https://stackoverflow.com/questions/29762578/two-gridview-with-one-scroll-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!