簡體   English   中英

Android使整個表格可點擊

[英]Android make whole tablerow clickable

我有一個小問題。
我想用可點擊的TableRows創建一個表。
多數方法工作正常,但OnClickListener不適用於整個Tablerow。
在垂直Scrollview元素所在的位置,TableRow是不可單擊的。
有沒有辦法為整個行設置點擊監聽器?

這里是活動的代碼片段:

 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>

圖片:(綠色可點擊/紅色不可點擊) http://abload.de/img/screenshot_2014-09-17lpu6s.jpg

嘗試

<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”,並在單擊布局時將調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM