问题描述
我的活动中有两个片段. Fragment2与Fragment1重叠. Fragment1占据了整个屏幕.当用户点击Fragment1时,我希望Fragment2消失.
I have two fragments in my activity. Fragment2 overlaps Fragment1. And Fragment1 takes up the entire screen. When the user taps Fragment1 I would like Fragment2 to disappear.
我的问题是如何确定Fragment1被窃听了?
My question is how can I determine that Fragment1 was tapped?
Fragment1主要由一个Webview组成,我以为我可以使用它的setOnTouchListener,但似乎从未调用过它.
Fragment1 is mostly made up of a webview which I was thinking I could use its setOnTouchListener but it doesn't seem to ever be called.
任何建议将不胜感激.
Any suggestions would be appreciated.
Bradley4
这是我实现onTouchListener的方式:
This is how I implemented the onTouchListener:
1)首先,我实现了"OnTouchListener"
1) first I implemented "OnTouchListener"
public class Frag_ItemDetail extends Fragment implements OnTouchListener {
2)然后我覆盖了onTouch
2)then I overrode onTouch
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("myclass", "onTouch");
return false;
}
3)然后将setOnTouchListener设置为我的webview
3) then I set the setOnTouchListener to my webview
WebView itemFullDescription = (WebView) v.findViewById(R.id.itemFullDescription);
WebView.setOnTouchListener(this);
我在按钮上设置了onTouchListenerner,它工作正常.它只是不为webview工作.
I set the onTouchListenerner on a button and it worked fine. It just isnt' working for the webview.
推荐答案
我扩展了LinearLayout
类并覆盖了onInterceptTouchEvent
,因此,如果触摸Fragment1
,Fragment2
将会消失,这是可行的.
I extended the LinearLayout
class and overrode onInterceptTouchEvent
so if Fragment1
is touched Fragment2
would disappear, which is working.
这篇关于Android检测内部片段的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!