2014-09-25 55 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