2015-09-25 54 views
0

我正在開發我的第一場比賽。機制很簡單:在屏幕上隨機移動泡泡。我的畫布是Activity上的RelativeLayout。佈局很簡單。Force的RelativeLayout焦點onTouchEvent

<RelativeLayout 
    android:id="@+id/frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@id/header" /> 

的觸摸事件是通過從View.OnTouchListener實施onTouch方法定義。一類提取如下:

public class BubbleActivity extends AppCompatActivity implements View.OnTouchListener { 
    private RelativeLayout mFrame; 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     //... 
    }  
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //... 
     setContentView(R.layout.activity_bubble); 
     mFrame = (RelativeLayout) findViewById(R.id.frame); 
     //... 
    }  
    @Override 
    protected void onResume() { 
     super.onResume(); 
     mFrame.setOnTouchListener(this); 
    } 
} 

問題是,有時觸摸事件不會發生。我想知道是否有某種機制總是抓住焦點,確保我的RelativeLayout對象中的觸摸事件。如果你想通過你捕獲所有TouchEvent小號RelativeLayout然後實現自定義視圖擴展RelativeLayoutonInterceptTouchEvent()處理事件

歡迎任何幫助

回答

0

實施例:

public class RelativeLayoutExt extends RelativeLayout { 
    public RelativeLayoutExt(Context context) { 
     super(context); 
    } 

    public RelativeLayoutExt(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public RelativeLayoutExt(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public RelativeLayoutExt(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
     // TODO handle your touches here 
     return super.onInterceptTouchEvent(ev); 
    } 
}