2014-09-25 57 views
0

一个片段有两个视图一个FrameLayout包含一些子视图,在FrameLayout下面有一个viewPager。 我想更换片段时有一个像ACTION_MOVE上的FrameLayout的动作,让我上的FrameLayout添加onTouchListener但它从来没有工作,viewPager效果很好,也是FrameLayout里的孩子们的观点具有onClick事件FrameLayout上面的viewPager片段中的onTouch事件拦截了bean

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    view = inflater.inflate(R.layout.hall_gift, container, 
      false); 
    frameLayout = (FrameLayout)view.findViewById(R.id.fans_body_title); 
    frameLayout.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if(event.getAction()==MotionEvent.ACTION_DOWN){ 
       Log.i(TAG,"ACTION DOWN"); 
      } 

      return false; 
     } 
    }); 
    return view; 
} 
+0

由“framlayout” ,你的意思是'FrameLayout',对吧? – APerson 2014-09-25 02:18:08

+0

是的!抱歉拼写错误! – Sunday 2014-09-25 02:27:36

+0

张贴您的代码请 – mmlooloo 2014-09-25 02:33:08

回答

0

你可以扩展FrameLayout并覆盖它的onInterceptTouchEvent()来拦截来自子视图的触摸事件。然后,使用您的自定义FrameLayout而不是以前的FrameLayout。

PLZ参照该文件: http://developer.android.com/training/gestures/viewgroup.html#intercept

下面的代码片段:

public class MyFrameLayout extends FrameLayout { 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 

    /* 
    * This method determines whether we want to intercept the motion. 
    * If we return true, onTouchEvent will be called. 
    */ 

      final int action = MotionEventCompat.getActionMasked(ev);  
      switch (action) { 
       case MotionEvent.ACTION_MOVE: { 
        if(meet your condition){ 
         return true; 
        } 
       } 

      } 
     return false; 
    } 

} 
+0

我知道你在说什么.3Q – Sunday 2014-09-25 03:46:16

+0

@Sunday它能解决你的问题吗? – 2014-09-25 04:19:45

+0

yes!frameLayout甚至不会拦截默认设置,当我们需要它的ontouch工作时,我们需要拦截自己 – Sunday 2014-09-25 05:18:03

1

如果你给

android:clickable="true" 

您的FrameLayout 它不允许触摸到你viewpager