同时具有水平和垂直滚动条的 Android GridView [英] Android GridView with Both Horizontal and Vertical Scrolbars at the same time

查看:27
本文介绍了同时具有水平和垂直滚动条的 Android GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android 新手,必须将 Windows Mobile 应用程序移植到 Android 应用程序.

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

问题:我需要 Android 中的 GridView 类似于 .net GridView,它能够同时在水平和垂直方向滚动.

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.

因为我的数据中有很多行和很多列.因此,由于移动设备上的屏幕宽度较小,因此不会显示所有列.多行显示为启用垂直滚动.我想要的是这个 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. So that user could view data in tabular form.

我在 TableLayout 和 SimpleAdapter 的帮助下在 Android 中使用 GridView.以下是我的代码:

I am using GridView in Android with the help of TableLayout and SimpleAdapter. Following is my code:

主要活动布局:

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

网格布局:

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

源代码:

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.

解决方案: 我添加了 Horizo​​ntalScrollView,现在我的 LinearLayout 是它的孩子.我还在 TableLayout 中添加了 android:stretchColumns="*" .与此同时,我不得不通过以下方式更改不同控件的 android:layout_width 和 android:layout_height.

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.

更新代码如下:

主要活动布局:

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

网格布局:

仅更改以下标签:

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

源代码:

Java 代码没有变化.

No change in Java code.

我的想法来自:如何才能我让我的布局水平和垂直滚动?如何以正确的方式将很长的表格布局放入水平滚动视图中?

这篇关于同时具有水平和垂直滚动条的 Android GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆