4

私は1つのTableLayoutを含むScrollViewをxmlに持っています。私の質問は、クリックするたびにフォーカス可能な行を持つことが可能かどうかです。ここに私のxmlコードがあります:

    <ScrollView 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TableLayout 
            android:id="@+id/table2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <TableRow>
                <RelativeLayout
                    android:id="@+id/rowlayout"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:background="@drawable/rowbackground2" >
                        <ImageView
                            android:id="@+id/icon"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/icon_code_contact" 
                            android:padding="7dip"
                            android:layout_alignParentLeft="true" />

                        <TextView
                            android:id="@+id/contacts"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:text="Contacts"
                            android:textColor="#000"
                            android:textSize="18dip"
                            android:layout_toRightOf="@id/icon"
                            android:paddingTop="10dip" />

                </RelativeLayout>
            </TableRow>


            <TableRow>
                <Button
                    android:id="@+id/contacts_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/contact_button" />
            </TableRow>

「focusable="true"」と「focusableInTouchMode="true"」を既に試しましたが、何も起こりませんでした..

前もって感謝します

4

5 に答える 5

24

テーブルを ListView のように動作させる方法について質問している場合は、次のことを行いました。

アクティビティのレイアウトを定義する xml ファイルで、テーブルを定義しました。

<ScrollView
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" >
    <TableLayout
        android:id="@+id/my_table"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:stretchColumns="1"
        android:shrinkColumns="1"/>
</ScrollView>

table_row_item.xml次に、表の行レイアウトを含む別のファイルを作成しました。

<?xml version="1.0" encoding="UTF-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:focusable="true"
    android:focusableInTouchMode="false"
    android:clickable="true"
    android:background="@android:drawable/list_selector_background">

    <TextView
        android:id="@+id/cell_1"
        ...
    />
    ...
    <TextView
        android:id="@+id/cell_N"
        ...
    />
</TableRow>

アクティビティで、テーブルに行を追加する手順を宣言しました。

private void addTableRow() {
    final TableLayout table = (TableLayout) findViewById(R.id.my_table);
    final TableRow tr = (TableRow) getLayoutInflater().inflate(R.layout.table_row_item, null);

    TextView tv;
    // Fill out our cells
    tv = (TextView) tr.findViewById(R.id.cell_1);
    tv.setText(...);
    ...
    tv = (TextView) tr.findViewById(R.id.cell_N);
    tv.setText(...);
    table.addView(tr);

    // Draw separator
    tv = new TextView(this);
    tv.setBackgroundColor(Color.parseColor("#80808080"));
    tv.setHeight(/*height of separaor line. 2dip will be enough*/);
    table.addView(tv);

    // If you use context menu it should be registered for each table row
    registerForContextMenu(tr);
}
于 2011-03-10T10:24:35.533 に答える
1

GridView を使用して、LinearLayout (水平) を行として定義できます。これをガイドとして使用してください: http://www.learn2crack.com/2014/01/android-custom-gridview.html

于 2014-08-06T14:44:06.607 に答える
0

このコードは、選択した描画可能な背景 (背景なしに null を使用できると思います) とデフォルトの Android の「選択された」背景を切り替えます。

    row.setClickable(true);
    row.setOnTouchListener(new OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            final int action = event.getAction();
            Log.d("Touched!", "" + action);
            switch (action & MotionEvent.ACTION_MASK)
            {
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                // case MotionEvent.ACTION_MOVE:
                v.setBackgroundResource(R.drawable.border_bottom);
                v.invalidate();
                break;
            default:
                v
                        .setBackgroundResource(android.R.drawable.list_selector_background);
                v.invalidate();

            }
            return false;
        }
    });
于 2011-04-08T00:23:39.480 に答える
0

もちろん。行には、onclick メソッドの名前を指定できる onClick プロパティがあります。

もう 1 つの方法は、この行に id - name を xml で指定することです。次に、コードから呼び出して onClick イベントを追加できます。

于 2011-02-02T12:33:09.403 に答える
0

テーブルにデータを表示するためのシンプルで効果的なソリューションを探している場合は、このTableViewが役に立ちます。

SortableTableView の例

于 2015-07-30T19:42:29.717 に答える