Android detecting on touch inside fragment

2020-07-06 09:09发布

enter image description here

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.

My question is how can I determine that Fragment1 was tapped?

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

This is how I implemented the onTouchListener:

1) first I implemented "OnTouchListener"

public class Frag_ItemDetail extends Fragment implements OnTouchListener {

2)then I overrode onTouch

@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("myclass", "onTouch"); 
return false;
}

3) then I set the setOnTouchListener to my webview

WebView itemFullDescription = (WebView) v.findViewById(R.id.itemFullDescription);
WebView.setOnTouchListener(this); 

I set the onTouchListenerner on a button and it worked fine. It just isnt' working for the webview.

2条回答
爷的心禁止访问
2楼-- · 2020-07-06 09:21

I extended the LinearLayout class and overrode onInterceptTouchEvent so if Fragment1 is touched Fragment2 would disappear, which is working.

查看更多
等我变得足够好
3楼-- · 2020-07-06 09:23

Shouldnt your return false be return true? If activity 2 is touched(or the webview) it should be returning as true. or vice versa

查看更多
登录 后发表回答