Java 安卓系统使整个表格行可点击

Java 安卓系统使整个表格行可点击,java,android,tablerow,clickable,Java,Android,Tablerow,Clickable,我有点小问题。 我想创建一个包含可单击表格行的表格。 其中大部分工作正常,但OnClickListener不适用于整个Tablerow。 在垂直滚动视图元素所在的位置,无法单击TableRow。 有没有办法为整行设置clicklistener 下面是活动的代码片段: for (int i = 0; i <anzahl; i++) { TableRow row = (TableRow) LayoutInflater.from(this).inflate(R.layout.p

我有点小问题。 我想创建一个包含可单击表格行的表格。 其中大部分工作正常,但OnClickListener不适用于整个Tablerow。 在垂直滚动视图元素所在的位置,无法单击TableRow。 有没有办法为整行设置clicklistener

下面是活动的代码片段:

 for (int i = 0; i <anzahl; i++) {
        TableRow row = (TableRow) LayoutInflater.from(this).inflate(R.layout.pnunterhaltungslist, null);
        TextView UnterhaltungsBetreff = (TextView) row.findViewById(R.id.Unterhaltungsbetreff);
        TextView UnterhaltungsBeteiligte = (TextView) row.findViewById(R.id.Unterhaltungvon);
        TextView UnterhaltungsUhrzeit = (TextView) row.findViewById(R.id.Uhrzeit);
        TextView UnterhaltungsDatum = (TextView) row.findViewById(R.id.Datum);
        row.setId(1000+i);
        row.setClickable(true);
        row.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       Intent myIntent = new Intent(PNs.this, Main.class);
                                       PNs.this.startActivity(myIntent);
                                   }
                               });
        UnterhaltungsBetreff.setText(Unterhaltungslist.get(i * 4 + 4) + "  ");
        UnterhaltungsBeteiligte.setText(Unterhaltungslist.get(i*4+2)+"  ");
        Date Datum = new Date(Integer.parseInt(Unterhaltungslist.get(i*4+3))*1000L);
        DateFormat Datumf = new SimpleDateFormat("dd.MM.yy");
        DateFormat Zeitf = new SimpleDateFormat("HH:mm");
        UnterhaltungsUhrzeit.setText(Zeitf.format(Datum));
        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        long todayInMillis = c.getTimeInMillis();

        if(todayInMillis - (Integer.parseInt(Unterhaltungslist.get(i*4+3))*1000L)<= 0){
            UnterhaltungsDatum.setText("Heute");

        }
        else if ((Integer.parseInt(Unterhaltungslist.get(i*4+3))*1000L) - (todayInMillis- 86400000) >= 0){
            UnterhaltungsDatum.setText("Gestern");
        }
        else{
            UnterhaltungsDatum.setText(Datumf.format(Datum));
        }
        Unterhalttable.addView(row,i);
    }
}
这里是xml:

<?xml version="1.0" encoding="utf-8"?>
<TableRow
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:drawable/list_selector_background"
    android:id="@+id/one">
    <RelativeLayout
        android:focusable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Unterhaltungrelativlayout">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="12:00"
            android:id="@+id/Uhrzeit"
            android:layout_alignTop="@+id/horizontalScrollView3"
            android:layout_alignParentRight="true"
            android:layout_marginRight="8dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="heute"
            android:id="@+id/Datum"
            android:layout_below="@+id/Uhrzeit"
            android:layout_alignParentRight="true"
            android:layout_marginRight="8dp"
            android:layout_marginTop="5dp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#ff363132"
            android:id="@+id/borderline"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="8dp"
            android:layout_below="@+id/horizontalScrollView3" />

        <HorizontalScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/horizontalScrollView3"
            android:layout_marginTop="8dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toEndOf="@+id/Uhrzeit"
            android:scrollbars="none"
            android:layout_marginLeft="8dp"
            android:layout_toLeftOf="@+id/Uhrzeit"
            android:layout_marginRight="8dp">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:id="@+id/relativlayout1">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Unterhaltung-Beteiligte"
                    android:id="@+id/Unterhaltungvon"
                    android:layout_gravity="left|bottom"
                    android:layout_below="@+id/Unterhaltungsbetreff"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginTop="5dp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Unterhaltungs-Betref blabasdkasnfdsadhfhsadfsadgsadögh"
                    android:id="@+id/Unterhaltungsbetreff"
                    android:layout_gravity="left|top"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

            </RelativeLayout>
        </HorizontalScrollView>

        <View
            android:layout_width="15dp"
            android:layout_height="200dp"
            android:background="@drawable/verlaufpn"
            android:id="@+id/borderline2"
            android:layout_toStartOf="@+id/Uhrzeit"
            android:paddingTop="-2dp"
            android:paddingBottom="-3dp"
            android:layout_above="@+id/borderline"
            android:layout_alignRight="@+id/horizontalScrollView3"/>

          </RelativeLayout>

</TableRow>
图片:绿色可点击/红色不可点击 试试看

<TableRow
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:drawable/list_selector_background"
    android:clickable="true"
    android:onClick="onTableRowClick"
    android:id="@+id/one">

然后在活动中,创建函数onTableRowClick,单击布局时将调用该函数。

对不起,结果是一样的