Android GridView with Both Horizontal and Vertical Scrolbars at the same time

前端 未结 3 1862
借酒劲吻你
借酒劲吻你 2020-12-10 09:05

I am new to Android and Have to Port a Windows Mobile Application to an Android Application.

Issue: I need GridView in Android similar to .net GridV

相关标签:
3条回答
  • 2020-12-10 09:17

    I am not sure, if it's possible to scroll in both direction at the same time, but here's a library that helps you to scroll a GridView horizontally. https://github.com/jess-anders/two-way-gridview

    Also you can have a look in here, How to make a 2-dimension image gallery with both horizontal and vertical scrolling?

    0 讨论(0)
  • 2020-12-10 09:24

    The simplest solution is

    1. Use a horizontal scroll view to contain the GridView
    2. Then dynamically re-size the GridView width based on the number of columns that you set for the GridView
    3. Lastly, multiply number of columns with the width of each columns

      GridView gridView = (GridView) findViewById(R.id.grid_view);
      int numberOfColumns = 3;
      int sizeOfWidthPerColumn = 20;
      gridView.setNumColumns(numberOfColumns);
      ViewGroup.LayoutParams layoutParams = gridView.getLayoutParams();
      layoutParams.width = convertDpToPixels(numberOfColumns * sizeOfWidthPerColumn, this);
      gridView.setLayoutParams(layoutParams);
      

    Now you are able to scroll the GridView horizontally and vertically.

    0 讨论(0)
  • 2020-12-10 09:35

    I changed some code and finally able get tabular view.

    Solution: I added HorizontalScrollView and now my LinearLayout is its child. Also I added android:stretchColumns="*" in TableLayout. Along with this i had to chnage android:layout_width and android:layout_height of different controls in following ways.

    Updated code is below:

    Main Activity Layout:

    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <!-- This Table Layout is header followed by the gridview -->
        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:stretchColumns="*" >
    
            <TableRow android:layout_width="wrap_content" >
    
                <TextView
                    android:id="@+id/schemeTitle"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:text="Scheme"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/nameTitle"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:text="Name"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/productTitle"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:text="Product"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/channelTitle"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:text="Channel"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/typeTitle"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Type"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/customerSelectionTitle"
                    android:layout_width="150dp"
                    android:layout_height="wrap_content"
                    android:text="Customer Selection"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/brandTitle"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:text="Brand"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/wsTitle"
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:text="W/S"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/startDateTitle"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:text="Start Date"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/endDateTitle"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:text="End Date"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/codeTitle"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:text="Code"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/tsidTitle"
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:text="TSID"
                    android:textStyle="bold" />
            </TableRow>
        </TableLayout>
    
        <GridView
            android:id="@+id/schemeGridView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:numColumns="1" >
        </GridView>
    </LinearLayout>
    
    </HorizontalScrollView>
    

    Grid Layout:

    Only following tag is chnaged:

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:stretchColumns="*">
    

    Source Code:

    No change in Java code.

    I got idea from: How can I make my layout scroll both horizontally and vertically? and How to put a very long table layout inside the horizontal Scroll view in the CORRECT way?

    0 讨论(0)
提交回复
热议问题