-1

通过使用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();         
    } 
}
4

1 回答 1

0

一种方法是TableRow OnClickListener()直接在单元格末尾触发onClick()

以您的单元格onClick()为例:

@Override
public void onClick(View v) {
    Log.d("cxc", "CLICKED the cell [ " +getRowNum()+ ", " + getColNum()+ "]"); 

    ((TableRow)v.getParent()).performClick();
} 

如果一个单元格被点击,这两种onClickListner()方法都会被调用。此外,如果直接单击某行,例如因为缺少单元格,则只会onClickListener()调用该行。

如果您只针对 API 15 及以后的设备,还有一种替代方法performClick(),称为callOnClick().

于 2012-08-15T01:29:12.337 回答