我的线性布局:main_linear_layout包含四个线性布局:ll1 ll2 ll3 ll4。每个LinearLayout都包含Buttons。我正在尝试在onFling上实现main_linear_layout方法。我想要能够滑动任何地方的布局的Buttons和调用一个动作。
目前,我可以在屏幕上的任何地方滑动,除了按钮的布局。我尝试使用以下代码来解决问题:
swipeLayout = (LinearLayout) findViewById(R.id.main_linear_layout);
//swipeLayout is the main Layout that contains 4 other linear layouts
swipeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
gestureScanner.onTouchEvent(event);
return true;
}
});但我仍然不能在Buttons的布局上滑动。有人知道这是为什么吗?还有什么我需要实现的吗?我应该对主线性布局中的每一个线性布局实现一个OnTouchListener吗?还是每个布局中的每个Button?
在xml中,我在主线性布局中添加了一堆代码:
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:longClickable="true" 但这也不起作用。有人能帮忙吗?
发布于 2014-12-07 04:34:50
如果您有一个LinearLayout,然后在其中放置更多的布局,然后按您说的那样在这些布局上设置按钮,那么这是按预期运行的。
您只需要将侦听器添加到外部layout...so,当然,只有当您在其上滑动时它才会触发。通过滑动其他布局,甚至按钮,幻灯片事件甚至无法到达该侦听器,因为它是被消耗的。
您需要将侦听器添加到每个要检查的元素中。
要做到这一点,一种方法是创建一个按钮数组,并一次性执行:
private Button button1;
private Button button2;
....
private Button button10;
...
protected void onCreate(Bundle b) {
super.onCreate(b);
button1 = findViewById(R.id.button1);
...
button10 = findViewById(R.id.button10);
OnClickListener onCL = new OnClickListener . . . //do all of your creation here
Button[] buttons = {button1, button2, . . . , button10};
for(Button b : buttons) {
b.setOnClickListener(onCL);
}
}发布于 2014-12-07 04:57:37
ll1,ll2,ll3,ll4之所以不接收触摸事件,是因为父线性布局接收到了运动事件,并且没有进一步传播。
为什么在主线性布局上需要一个触摸事件侦听器?你想把潜艇的布局都放在一起吗?
如果希望在触摸事件中选择其他布局,则在触摸侦听器上返回false
swipeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureScanner.onTouchEvent(event);
return false;
}
});或者您可以创建4个GestureDetectors,并根据按下的视图将事件传递给右边的事件。
swipeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId()){
case R.id.ll1:
gesture1.ontouchEvent(e);
break;
case R.id.ll2:
gesture1.ontouchEvent(e);
break;
}
//etc
return false;
}
});https://stackoverflow.com/questions/27339576
复制相似问题