Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

android - How to handle Touch Events on a Fragment?

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! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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,...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...