简体   繁体   English

Android GridView同时具有水平和垂直Scrolbars

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

I am new to Android and Have to Port a Windows Mobile Application to an Android Application. 我是Android新手,必须将Windows Mobile应用程序移植到Android应用程序。

Issue: I need GridView in Android similar to .net GridView which is able to scroll in both Horizontal and Vertical directions at the same time. 问题:我需要Android中的GridView类似于.net GridView,它能够同时在水平和垂直方向上滚动。

As there are many rows and many columns in my data. 由于我的数据中有很多行和很多列。 So, all the columns are not shown as screen width is small on mobile. 因此,所有列都没有显示,因为移动设备上的屏幕宽度很小。 Multiple rows are shown as vertical scroll is enabled. 启用垂直滚动时会显示多行。 What i want is that this GridView is somehow scrollable in both directions at the same time . 我想要的是这个GridView在某种程度上可以同时在两个方向上滚动 So that user could view data in tabular form . 这样用户就可以以表格形式查看数据。

I am using GridView in Android with the help of TableLayout and SimpleAdapter. 我在TableLayout和SimpleAdapter的帮助下在Android中使用GridView。 Following is my code: 以下是我的代码:

Main Activity Layout: 主要活动布局:

<LinearLayout 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: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" >

    <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="wrap_content"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:numColumns="1"
    android:columnWidth="900dp" >
</GridView>

</LinearLayout>

Grid Layout: 网格布局:

<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="fill_parent" >

<TableRow android:layout_width="wrap_content" >

    <TextView
        android:id="@+id/scheme"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/name"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/product"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/channel"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/type"
        android:layout_width="100dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/customerSelection"
        android:layout_width="150dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/brand"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/ws"
        android:layout_width="50dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/startDate"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/endDate"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/code"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tsid"
        android:layout_width="50dp"
        android:layout_height="wrap_content" />
</TableRow>

</TableLayout>

Source Code: 源代码:

public class Schemes extends BaseMainActivity {

private String obID = "";
private String obName = "";
ArrayList<HashMap<String, Object>> dtSchemes = null;
GridView schemeGridView = null;
Activity thisAct = this;

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

    ProgressDialog d = ProgressDialog.show(this, "Inventory",
            "Loading Inventory...");

    OrderBookingDAL dl = new OrderBookingDAL();
    obID = dl.getConfigValue("OBID");
    obName = dl.getConfigValue("OBName");
    dtSchemes = dl.getHHTSchemes(obID, obName, dtSchemes);

    schemeGridView = (GridView) findViewById(R.id.schemeGridView);

    SimpleAdapter sa = new SimpleAdapter(this, dtSchemes,
            R.layout.schemes_grid, new String[] { "Scheme", "Name",
                    "Product", "Channel", "Type", "CustomerSelection",
                    "Brand", "[W/S Approved]", "[Start Date]",
                    "[End Date]", "Code", "TSID" }, new int[] {
                    R.id.scheme, R.id.name, R.id.product, R.id.channel,
                    R.id.type, R.id.customerSelection, R.id.brand, R.id.ws,
                    R.id.startDate, R.id.endDate, R.id.code, R.id.tsid });
    schemeGridView.setAdapter(sa);
    schemeGridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            Log.d("Grid Item Click", "Clicked pos:"+position);
            TextView txt = (TextView) view.findViewById(R.id.tsid);
            if (txt != null && txt.getText() != null) {
                String tsid = txt.getText().toString();
                Intent intent = new Intent(thisAct,SchemeDetails.class); 
                intent.putExtra(TSID_PARAM, tsid);
                thisAct.startActivity(intent);
            }
        }
    });
    d.dismiss();
}
}

I changed some code and finally able get tabular view. 我更改了一些代码,最终得到了表格视图。

Solution: I added HorizontalScrollView and now my LinearLayout is its child. 解决方案:我添加了Horizo​​ntalScrollView,现在我的LinearLayout就是它的子节点。 Also I added android:stretchColumns="*" in TableLayout. 另外我在TableLayout中添加了android:stretchColumns =“*”。 Along with this i had to chnage android:layout_width and android:layout_height of different controls in following ways. 与此同时,我必须通过以下方式更新android:layout_width和android:layout_height的不同控件。

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: 只有以下标记是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. Java代码没有变化。

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? 以及如何在CORRECT方式中将非常长的表格布局放在水平滚动视图中?

The simplest solution is 最简单的解决方案是

  1. Use a horizontal scroll view to contain the GridView 使用水平滚动视图包含GridView
  2. Then dynamically re-size the GridView width based on the number of columns that you set for the GridView 然后根据您为GridView设置的列数动态地重新调整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. 现在,您可以水平和垂直滚动GridView。

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. 我不确定,如果可以同时向两个方向滚动,但这里有一个库,可以帮助您水平滚动GridView。 https://github.com/jess-anders/two-way-gridview 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? 您还可以在这里查看, 如何制作水平和垂直滚动的二维图库?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM