Android 如何处理片段上的触摸事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21882251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle Touch Events on a Fragment?
提问by user3319400
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)
我的应用,在架构层面,如下: - MainActivity.java - NavigationDrawer.java - TestFragment.java(现在为单个片段,等待解决问题)
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:
我还没有找到解释如何做好(或如何解决问题)的解决方案或教程。然后我问你,将问题简化为“在片段中启用触摸事件(在这种情况下为 getPressure())”。下面是一些可能有助于解决问题的代码:
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! :)
非常感谢您的建议!:)
回答by intips
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:
- 在你的片段 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:
并且您可以在 onTouch 中检查不同的 MotionEvents,例如:
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP,...