如何处理片段上的触摸事件? [英] How to handle Touch Events on a Fragment?

查看:105
本文介绍了如何处理片段上的触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个界面,其中我需要处理触摸事件.特别是,我将只能将它们启用到片段中的受限区域. 为了更好地理解问题,尊重标准和实现我的应用程序的目标,我计划了导航抽屉,它假定存在许多碎片(而不是活动).带有触摸事件的活动很容易实现,另一方面,我在互联网上看到带有碎片的问题可能会成为问题.

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.

我的应用程序在体系结构级别上如下: -MainActivity.java -NavigationDrawer.java -TestFragment.java(现在只有一个片段,正在等待解决问题)

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)

我还没有找到解决方法或教程来说明如何做得好(或解决该问题的方法).然后,我问您,将问题简化为"在片段中启用触摸事件(在这种情况下为getPressure())".以下是一些可能有助于解决该问题的代码:

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

推荐答案

我不确定是否理解您的问题,但是我会尽力回答.因此,要获取有关片段的触摸事件,我将执行以下操作:

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:

-在片段onCreateView中:

-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;

,您可以在onTouch中检查不同的MotionEvent,例如:

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

MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP,...

这篇关于如何处理片段上的触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆