通过使用TableLayout
,我创建了一个简单的表,并向OnClick
表行和行的单个单元格添加了侦听器。但是,触摸任何单元格只会执行单元格侦听TableRow
器,并且不会调用侦听器。
我怎样才能让这两个监听器在桌面上单击一下就可以执行?
或者对这种情况有什么更好的建议?
代码:
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class PostSampleTable extends TableLayout {
public PostSampleTable(Context context) {
super(context);
super.setLayoutParams( new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT) );
super.setClickable( true );
init();
}
private void init() {
for (int i = 0; i < 39; i++) {
TableRow lineRow = new TableRow( getContext() );
super.addView( lineRow );
addRowListener( lineRow, i);
for (int j = 0; j < 9; j++) {
View cell = createTextView("DATACELLAT_" +i+ "_" +j+ "", 100);
lineRow.addView( cell );
addCellListener( cell, i, j);
}
}
}
private void addRowListener(TableRow lineRow, int i) {
MyRowL mrl = new MyRowL() {
int rowNum = 0;
@Override
public void onClick(View v) {
Log.d("cxc", "CLICKED Line Row [ " +getRowNum()+ "]");
}
@Override
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
@Override
public int getRowNum() {
return this.rowNum;
}
};
mrl.setRowNum( i );
lineRow.setOnClickListener( mrl );
}
private void addCellListener(View cell, int i, int j) {
MyCellL mcl = new MyCellL() {
int rowNum = 0;
int colNum = 0;
@Override
public void onClick(View v) {
Log.d("cxc", "CLICKED the cell [ " +getRowNum()+ ", " + getColNum()+ "]");
}
@Override
public void setRowNum(int rowNum) {
this.rowNum = rowNum;
}
@Override
public int getRowNum() {
return this.rowNum;
}
@Override
public void setColNum(int colNum) {
this.colNum = colNum;
}
@Override
public int getColNum() {
return this.colNum;
}
};
mcl.setRowNum( i );
mcl.setColNum( j );
cell.setOnClickListener( mcl );
}
private TextView createTextView(String text, int width) {
TextView reply = new TextView( getContext() );
reply.setText(text);
reply.setWidth( width );
return reply ;
}
interface MyRowL extends OnClickListener {
public void setRowNum(int rowNum);
public int getRowNum();
}
interface MyCellL extends MyRowL {
public void setColNum(int colNum);
public int getColNum();
}
}