如何在没有 header 行的情况下滚动行?

How to scroll rows without the header row?

我的activity有这个xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner_strassenfuehrer_fb_screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TableLayout
        android:id="@+id/tablelayout_strassenfuehrer_fb_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" >

        <TableRow
            android:scrollbars="vertical"
            android:background="#A6A6A6"
            android:padding="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Störcode" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Bezeichnung" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Letztes Auftreten" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Anlagen ID" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Projekt" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Benötigte Ersatzteile" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Benötigtes Werkzeug" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAllCaps="false"
                android:layout_weight="1"
                android:text="Handlungsleitfaden" />

        </TableRow>
    </TableLayout>

我的代码是:

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

    //read database and put values dynamically into table
    tableLayout = (TableLayout) findViewById(R.id.tablelayout_strassenfuehrer_fb_screen);
    for (int i = 0; i < 100; i++) {

        TableRow row = new TableRow(this);
        row.canScrollVertically(1);

        //column 1
        TextView tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 2
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 3
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 4
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 5
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 6
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 7
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        //column 8
        tv = new TextView(this);
        tv.setText("TEST");
        row.addView(tv);

        tableLayout.addView(row, i + 1);
    }
}

我是 android 的新手,我想让这些行可滚动,但不是 xml-layout 中的 "first" 行。

我还想调整元素,我将其动态放入我的 TableLayout 中,因为这些元素不适合该列。

我是 android 的新人,如果有人能帮助我,我会很高兴:)

谢谢

虽然这不是一个很好的解决方案,但我建议将两个分开 TableLayout。一个用于 header,另一个用于滚动。所以最终的布局将是这样的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Spinner
        android:id="@+id/spinner_strassenfuehrer_fb_screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TableLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <TableRow
            android:background="#A6A6A6"
            android:padding="5dp"
            android:scrollbars="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Störcode"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Bezeichnung"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Letztes Auftreten"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Anlagen ID"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Projekt"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Benötigte Ersatzteile"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Benötigtes Werkzeug"
                android:textAllCaps="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Handlungsleitfaden"
                android:textAllCaps="false" />

        </TableRow>
    </TableLayout>

    <TableLayout
        android:id="@+id/tablelayout_strassenfuehrer_fb_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp" />
</LinearLayout>

只需通过 id header 获取 header,然后先填充 header。然后将视图添加到第二个 TabLayout。我希望你明白了。

但是,我强烈建议使用 GridView or RecyclerView with GridLayoutManager 来实现它。

更新

这只是一个示例代码。我猜该代码将不起作用。我只是放了一个样本给你一个想法。

tableLayout = (TableLayout) findViewById(R.id.tablelayout_strassenfuehrer_fb_screen);

tableLayoutHeader = (TableLayout) findViewById(R.id.header);

for (int i = 0; i < 100; i++) {

    TableRow row = new TableRow(this);
    row.canScrollVertically(1);

    //column 1
    TextView tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 2
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 3
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 4
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 5
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 6
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 7
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    //column 8
    tv = new TextView(this);
    tv.setText("TEST");
    row.addView(tv);

    if(i == 0) tableLayoutHeader.addView(row, i + 1);
    else tableLayout.addView(row, i + 1);
}