Android 表内可聚焦行

Android 表内可聚焦行,android,tablerow,focusable,Android,Tablerow,Focusable,我在xml中有一个ScrollView,其中包括一个TableLayout。 我的问题是,是否有可能在每次单击时都有一个可聚焦的行。 以下是我的xml代码: <ScrollView android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TableLa

我在xml中有一个ScrollView,其中包括一个TableLayout。 我的问题是,是否有可能在每次单击时都有一个可聚焦的行。 以下是我的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”,但什么也没发生


当然,先谢谢你。行具有onClick属性,您可以在其中指定onClick方法的名称


另一种方法是在xml中为此行指定一个id名称。然后您可以从代码中调用它并添加onClick事件。

如果您询问如何使表的行为类似于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>
在活动中,我声明了向表中添加行的过程:

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);
}

这段代码将在您选择的可绘制背景(我想您可以使用null来无背景)和默认的Android“selected”背景之间切换

    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;
        }
    });

可以使用GridView并将LinearLayout(水平)定义为行。将此作为指导:

如果您正在寻找一种简单有效的解决方案来在表中显示数据,这将完成这项工作


我已经完成了您所说的关于行id的操作,但它没有起作用。。我的问题是,我无法使行在单击时可聚焦//我不确定“可聚焦”是什么意思,你的意思是它应该显示它已被选中吗?因为行中有一个相对布局,所以可能是其他视图在单击时接收焦点。也许其他内部视图对于clickable应该为false。。。
    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;
        }
    });