How to handle Touch Events on a Fragment?
Asked Answered
V

2

61

I'm building an interface where I need to process touch events. In particular I would to be able to enable them only to a confined area in a fragment. To better understand the problem, to respect the standards and for the goal of my application, I planned the navigation drawer, which assumes the presence of many fragment (instead of activities). An activity with touch events is implemented quite easily, on the other hand I have read on the internet that with the fragments can become a problem.

My application, at the architectural level, is as follows: - MainActivity.java - NavigationDrawer.java - TestFragment.java (for a single fragment now, waiting to solve the problem)

I've not found a solution or a tutorial that explains how to do well (or how to workaround the problem). Then I ask you, simplifying the problem to just "enable a touch event in a fragment (getPressure() in this case)". Below are some pieces of code that may help to solve the problem:

TestFragment

public class TestFragment extends Fragment {
    private static final String ARG_SECTION_NUMBER = "section_number";

    public static TestFragment newInstance(int sectionNumber) {
        TestFragment fragment = new TestFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public TestFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_test, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Here I want to return the press value (0 to 1)
    }
}

How to associate the listener to the whole fragment? And in the case of a particular area? Finally, how can I return the value of the pressure on the screen?

Thank you so much in advice! :)

Vocalise answered 19/2, 2014 at 13:41 Comment(2)
BTW, in my company we have a class MainActivity extends Activity, and many Fragments. The one major problem we had was: We could not get the navigation stack (aka back stack) for fragments to work the way we wanted (I think iOS did this better), so we ended up managing fragments and the back button in our own code. That is, we kept track of fragments, and when back button pressed, made our own decision about where to go back to. (If a fragment A links to a child fragment B1, which links to a sibling/peer fragment B2, we wanted Back to return to A, not B1, in places where that made sense)Billiebilling
I Think one answer for this question is here: #11001782Ium
H
126

I'm not sure if I understood your problem, but I will try to answer this. So to get touch events on fragment I would do this:

-in your fragment onCreateView:

View view = inflater.inflate(R.layout.fragment_test, container, false);

    view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                if(event.getAction() == MotionEvent.ACTION_MOVE){
                    //do something
                }
                return true;
            }
    });

//here the rest of your code

return view;

and you can check for different MotionEvents in onTouch, for example:

MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP,...
Hime answered 19/2, 2014 at 15:39 Comment(5)
Thank you! :) I've done that you wrote, but Android Studio says that the method invocation 'view.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, [...] may produce a null pointer exception. Have you an idea to solve this problem?Vocalise
I think you should not worry about that warning, I have used this method in eclipse and it didn't show any warning, and I never had a problem using it this way.Hime
@Vocalise you can also implement onTouch method in you fragment and use view.setOnTouchListener(this)Montpellier
To clarify Ivan Black's comment: At start of your fragment: public class YourFragmentName implements View.OnTouchListener. And somewhere within your fragment is a method public boolean onTouch(View v, MotionEvent event) { ... }. Then you hook that to your view with view.setOnTouchListener(this).Billiebilling
still no luck for me rootView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getActionMasked() == MotionEvent.ACTION_DOWN){ Toast.makeText(getActivity(), "you just touch the screen :-)", Toast.LENGTH_SHORT).show(); } return true; } });Judiejudith
C
4

In Kotlin:

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

   val view =  inflater.inflate(R.layout.fragment_img_view, container, false)

    view.setOnTouchListener(object : View.OnTouchListener {
        override fun onTouch(v: View?, event: MotionEvent): Boolean {
            if (event.action == MotionEvent.ACTION_MOVE) {
                scaleGestureDetector.onTouchEvent(event)
            }
            return true
        }
    })
    return view
}
Cabriole answered 7/1, 2021 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.