Skip to content Skip to sidebar Skip to footer

Set Individual Onclick Events In Custom Viewgroup

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 metho

Solution 1:

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

privateOnClickListenermListener=newOnClickListener() {

     @OverridepublicvoidonClick(View v) {
          CellContainerGroupccg= (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;

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

Don't forget to set the mListener:

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

Post a Comment for "Set Individual Onclick Events In Custom Viewgroup"