设置个体的onclick事件自定义的ViewGroup [英] Set individual onClick events in custom ViewGroup

查看:301
本文介绍了设置个体的onclick事件自定义的ViewGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了我创建几个自定义子视图自定义的ViewGroup(在Java code,而不是XML)。
这些儿童的都需要具有相同的onClick事件

来处理这些点击事件的方法在于:使用布局中活动类。如何设置这个方法为onclick处理所有子视图?

下面是我的code简化。

自定义的ViewGroup:

 公共类CellContainerGroup扩展的ViewGroup
{
    CellView [] [] CellGrid =新CellView [9] [9];    公共CellContainerGroup(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);        TypedArray A = context.obtainStyledAttributes(R.styleable.CellContainerGroup);        尝试{
                //在这里初始化属性。
            } {最后
               a.recycle();
            }        的for(int i = 0; I< 9;我++)
            对于(INT J = 0; J< 9; J ++)
            {
                //添加CellViews到CellGrid。
                CellGrid [I] [J] =新CellView(背景下,NULL); //属性集中为空传递
                //手动设置属性,高度,宽度等。
                // ...                //添加视图组
                this.addView(CellGrid [I] [J],我* 9 + J);
            }
    }
}

这包含了我想要的click处理程序使用方法的活动:

 公共类SudokuGameActivity延伸活动{    //左明一切从其他活动。    公共无效cellViewClicked(查看视图){
    {
        //东西在这里做...
    }
}


解决方案

设置一个 OnClickListener (在 CellContainerGroup ),这将调用该方法:

 私人OnClickListener mListener =新OnClickListener(){     @覆盖
     公共无效的onClick(视图v){
          CellContainerGroup CCG =(CellContainerGroup)v.getParent();
          ccg.propagateEvent(五);
     }
}

其中, propagateEvent CellContainerGroup 这样的方法:

 公共无效propagateEvent(查看电池){
     ((SudokuGameActivity)mContext).cellViewClicked(手机);
}

和其中 mContext 上下文引用这样的:

 私人语境mContext;公共CellContainerGroup(上下文的背景下,ATTRS的AttributeSet){
   超(背景下,ATTRS);
   mContext =背景;
// ...

不要忘记设置 mListener

  CellGrid [I] [J] .setOnClickListener(mListener);

I have created a custom ViewGroup in which I create a couple of custom child Views (in the java code, not xml). Each of these childs needs to have the same onClick event.

The method to handle these click events resides in the activity class that uses the layout. How do I set this method as the onClick handler for all child views?

Here is my code simplified.

The custom ViewGroup:

public class CellContainerGroup extends ViewGroup
{
    CellView[][] CellGrid = new CellView[9][9];

    public CellContainerGroup(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(R.styleable.CellContainerGroup);

        try {
                //initialize attributes here.       
            } finally {
               a.recycle();
            }

        for(int i = 0; i < 9 ; i++)
            for(int j = 0 ; j < 9 ; j++)
            {
                //Add CellViews to CellGrid.
                CellGrid[i][j] = new CellView(context, null); //Attributeset passed as null
                //Manually set attributes, height, width etc.               
                //...

                //Add view to group
                this.addView(CellGrid[i][j], i*9 + j);
            }
    }
}

The activity that contains the method I want to use as the click handler:

public class SudokuGameActivity extends Activity {

    //Left out everything else from the activity.

    public void cellViewClicked(View view) {
    {
        //stuff to do here...
    }
}

解决方案

Setup a single OnClickListener(in CellContainerGroup) which will call that method:

private OnClickListener mListener = new OnClickListener() {

     @Override
     public void onClick(View v) {
          CellContainerGroup ccg = (CellContainerGroup) v.getParent();
          ccg.propagateEvent(v);
     }
}

where propagateEvent is a method in CellContainerGroup like this:

public void propagateEvent(View cell) {
     ((SudokuGameActivity)mContext).cellViewClicked(cell);
}

and where mContext is a Context reference like this:

private Context mContext;

public CellContainerGroup(Context context, AttributeSet attrs) {
   super(context, attrs);
   mContext = context;
//...

Don't forget to set the mListener:

CellGrid[i][j].setOnClickListener(mListener);

这篇关于设置个体的onclick事件自定义的ViewGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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