0

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 viewsonTouchEvent` will be called and this will fire a subsequent clickEvent which will trigger the handler defined in the Fragment.

avrono
  • 1,648
  • 3
  • 19
  • 40
  • Invisible Button as the fragment's background? You would need to make the base layout a RelativeLayout and place the other widgets on top of the invisible button. – Rick Falck Jan 06 '14 at 20:50
  • Implement a custom interface in your activity and pass a reference to the interface to the activity that needs it. – dm78 Jan 06 '14 at 20:52

1 Answers1

0

Try handling this logic in the parent activity or fragment. As a general rule, fragments shouldn't know about their parents. You could, for instance, do the following:

  1. Have a parent activity with a layout, with a fragment in the layout.
  2. Set an onClickListener on the layout that attaches and detaches the fragments as desired.

This way, the parent activity can handle whatever it needs to at the top level, and the fragments never need to know. If you need to do this fragment switching, but want it to be inside another fragment, you can do the same thing. Just note that you must add the nested fragments programmatically, because xml nested fragments aren't supported. See this for a fragment switching code example.

Community
  • 1
  • 1
GLee
  • 5,003
  • 5
  • 35
  • 39
  • I tried to create a Listener Interface in the fragment and make the activity implement this Interface. However there is no way for the Listener to be triggered. I will play around with the suggestions here ... – avrono Jan 06 '14 at 22:49
  • I tried to get the view in the `onCreate` of the Activity and setOnClickListener to a private anonymous onclicklistener, but this does not seem to get called .... – avrono Jan 06 '14 at 23:16
  • Ok, I solved I needed to call the `performClick` from `onTouchEvent` to trigger a click event (obviously captured by `onTouchEvent`) – avrono Jan 06 '14 at 23:51
  • Ok, is it working? Alternatively, unless you are doing something else in onTouchEvent(), you can stop overriding onTouchEvent() and just use the onClickListeners. Depending on what you return, onTouchEvent() will consume the event so that it never reaches the listeners. – GLee Jan 07 '14 at 02:53