我有一个小问题
我想创建一个具有可单击TableRows的表
大多数操作都很好,但OnClickListener不适用于整个Tablerow
垂直滚动视图元素所在的TableRow不可单击。
有没有办法为整个Row设置一个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>
图片:(绿色可点击/红色不可点击)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",当单击布局时会调用它。