I have a fragment which inflates a custom View.
Currently the view updates when the user clicks on it using the View's
public boolean onTouchEvent(MotionEvent event) {... }
However, I want to also update which Fragments are displayed when there is a MotionEvent. It is not recommended to access the FragmentManager
from within a custom view and doing something like:
try{
Activity a = (Activity) context;
FragmentManager fm = a.getFragmentManager();
// Use the fragment manager
} catch (ClassCastException e) {
Log.d(TAG, "Can't get the fragment manager with this");
}
Seems messy and full of pit-falls. I can see that for a Fragment that extends the ListFragment there is an public void onListItemClick(ListView l, View v, int position, long id)
which would allow one to create a callback , is there anyway of getting this to work for a standard Fragment ?
UPDATE:
For those interested, the solution is to put an onClickListener in the fragment onCreateView
something like:
View canvasView = inflater.inflate(R.layout.canvas_view , container, false);
ComposeView myView = (ComposeView) canvasView.findViewById(R.id.myDrawView2);
myView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view) {
Log.d("FRAG", "OnClick Called");
showNumericInput();
}
});
return canvasView;
And also to generate a clickEvent in the onTouchEvent
of the View this.performClick();
. Thus the views
onTouchEvent` will be called and this will fire a subsequent clickEvent which will trigger the handler defined in the Fragment.