如何开枪的tablelayout两个监听器一按:通过tablerow及其细胞 [英] How to fire two listeners on the tablelayout by one click: tablerow and its cell

查看:104
本文介绍了如何开枪的tablelayout两个监听器一按:通过tablerow及其细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用 TableLayout 我创建了一个简单的表,并添加的OnClick 听众既表中的行与行单个细胞。但是,接触任何单元格仅执行单元监听器和的TableRow 监听器不叫。

我怎样才能得到这两个侦听器与上表中的单个触摸来执行?

或者对这样的场景任何更好的建议?

code:

进口android.content.Context; 进口android.util.Log; 进口android.view.View; 进口android.widget.TableLayout; 进口android.widget.TableRow; 进口android.widget.TextView; 公共类PostSampleTable扩展TableLayout {     公共PostSampleTable(上下文的背景下){         超(上下文);         super.setLayoutParams(新TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,                                 TableLayout.LayoutParams.WRAP_CONTENT));         super.setClickable(真正的);         在里面();     }     私人无效的init(){         的for(int i = 0; I< 39;我++){             的TableRow lineRow =新的TableRow(的getContext());             super.addView(lineRow);             addRowListener(lineRow,我);             对于(INT J = 0; J< 9; J ++){                 视图细胞= createTextView(DATACELLAT_+ I +_+ J +,100);                 lineRow.addView(细胞);                 addCellListener(单元,I,J);             }         }     }     私人无效addRowListener(的TableRow lineRow,int i)以{         MyRowL MRL =新MyRowL(){             INT的rowNum = 0;             @覆盖             公共无效的onClick(视图v){                 Log.d(CXC,单击的行行[+ getRowNum()+]);             }             @覆盖             公共无效setRowNum(INT的rowNum){                 this.rowNum =的rowNum;             }             @覆盖             公众诠释getRowNum(){                 返回this.rowNum;             }         };         mrl.setRowNum(ⅰ);         lineRow.setOnClickListener(MRL);     }     私人无效addCellListener(查看细胞,诠释我,诠释J){         MyCellL MCL =新MyCellL(){             INT的rowNum = 0;             INT colNum = 0;             @覆盖             公共无效的onClick(视图v){                 Log.d(CXC,点击细胞[+ getRowNum()+,+ getColNum()+]);             }             @覆盖             公共无效setRowNum(INT的rowNum){                 this.rowNum =的rowNum;             }             @覆盖             公众诠释getRowNum(){                 返回this.rowNum;             }             @覆盖             公共无效setColNum(INT colNum){                 this.colNum = colNum;             }             @覆盖             公众诠释getColNum(){                 返回this.colNum;             }         };         mcl.setRowNum(ⅰ);         mcl.setColNum(J);         cell.setOnClickListener(MCL);     }     私人TextView的createTextView(字符串文本,诠释宽度){         TextView的回复=新的TextView(的getContext());         reply.setText(文本);         reply.setWidth(宽);         返回答复;     }     接口MyRowL扩展OnClickListener {         公共无效setRowNum(INT的rowNum);         公众诠释getRowNum();     }     接口MyCellL扩展MyRowL {         公共无效setColNum(INT colNum);         公众诠释getColNum();     } }

解决方案

这样做将是火的一种方式的TableRow OnClickListener() 直接在单元格的结束的onClick()

用你的的onClick()为例:

  @覆盖
公共无效的onClick(视图v){
    Log.d(CXC,点击细胞[+ getRowNum()+,+ getColNum()+]);

    ((的TableRow)v.getParent())performClick()。
}
 

如果一个细胞被点击,既 onClickListner()方法被调用。此外,如果行直接点击,说是因为它缺少的细胞,只有该行 onClickListener()将被调用。

如果你只针对API 15的设备以后也有和替代方法 performClick(),被称为的 callOnClick()

By using TableLayout I created a simple table and added OnClick listeners to both the table rows and the rows' individual cells. However, touching any cell executes only the cell listener and TableRow listener isn't called.

How can I get both of these listeners to execute with a single touch on the table?

Or any better suggestion on such scenario?

Code:

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();         
    } 
}

解决方案

One way to do it would be to fire the TableRow OnClickListener() directly at the end of the cell onClick():

Using your cell onClick() as an example:

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

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

If a cell gets clicked, both onClickListner() methods are called. Also, if a row is clicked directly, say because it is missing cells, only the row onClickListener() will be called.

If you are only targeting devices of API 15 onwards there is also and alternative method to performClick(), called callOnClick().

这篇关于如何开枪的tablelayout两个监听器一按:通过tablerow及其细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆