を使用して単純なテーブルを作成し、テーブルの行と行の個々のセルの両方にリスナーをTableLayout
追加しました。OnClick
ただし、任意のセルに触れると、セル リスナーのみが実行され、TableRow
リスナーは呼び出されません。
テーブルに 1 回タッチするだけで、これらの両方のリスナーを実行するにはどうすればよいでしょうか?
または、そのようなシナリオに関するより良い提案はありますか?
コード:
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();
}
}