OnKeyListener或不onkeydown事件,如果孩子的意见采取重点工作(OnKeyList

2019-09-19 08:42发布

我想听听滚动型,看它是否是滚动。 我用OnTouchListener,它工作得很好。 但是,当我想补充的兼容性,使用轨迹球或OnKeyListener重写的onkeydown方法,它不能正常工作。 好像服用焦点的子键引起该问题。

任何解决方案或替代方法来解决这个问题呢? 任何帮助表示赞赏。

下面是一些演示代码重现我的问题:

public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);
    LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);

    LinearLayout.LayoutParams wrapParams = new LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    for (int i = 0; i < 100; i++) {
        MyItem item = (MyItem) LayoutInflater.from(this).inflate(R.layout.item, null);
        item.setParent(scrollView);
        item.setBackgroundColor(Color.WHITE);
        item.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        mainLayout.addView(item, wrapParams);
    }

    scrollView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // can go into here
        }

    });

    scrollView.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                // never go in, unless no child button get focus
            }
            return false;
        }
    });
}

}

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <com.fannyxie.MyScroller 
        android:id="@+id/scroll_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="beforeDescendants"
        android:clickable="true"
        android:focusable="true">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <LinearLayout android:id="@+id/main_layout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
            </LinearLayout>
        </LinearLayout>
    </com.fannyxie.MyScroller>
</LinearLayout>

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.fannyxie.MyItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="TITLE"/>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:id="@+id/myButton" android:text="Button"></Button>
</com.fannyxie.MyItem>

MyScroller.java

public class MyScroller extends ScrollView {

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

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //not go into here...
        Log.i("MyScroller", "onKeyDown");
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onTrackballEvent(MotionEvent event) {
        //not go into here...
        Log.i("MyScroller", "onTrackballEvent");
        return super.onTrackballEvent(event);
    }

    @Override
    protected boolean onRequestFocusInDescendants(int direction,
            Rect previouslyFocusedRect) {
        //some times go into here, when no button get the focus when entering first time
        Log.i("MyScroller", "request focus in descendants");
        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
    }



}

MyItem.java

public class MyItem extends LinearLayout {

    private Button myButton;
    public MyItem(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected boolean onRequestFocusInDescendants(int direction,
            Rect previouslyFocusedRect) {
        // never go into here
        Log.i("MyItem", "request focus in descendants");
        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
    }

}

Answer 1:

谢谢@Jeffrey。 但是,我发现了一个更好的办法来做到这一点。 只是覆盖滚动型的dispatchkeyevent方法和处理轨迹球/键盘事件出现。 它工作得很好。

public class MyScroller extends ScrollView {

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

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if ((KeyEvent.KEYCODE_DPAD_UP == event.getKeyCode() || KeyEvent.KEYCODE_DPAD_DOWN == event.getKeyCode())) {
            //handle key events here
    }
    return super.dispatchKeyEvent(event);
}

}


Answer 2:

触摸事件传递从孩子父母。 如果有儿童消费甚至(返回true),然后它停止; 它不传递给家长。 因此,2个解决方案,

第一是扩展视图容器类和重写onInterceptTouchEvent() 这里有一个例子,其中我是做TabHost ,但对你来说可能是LinearLayout ,或什么的,

public class FlingableTabHost extends TabHost {
    private GestureDetector gestureDetector;

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

    public FlingableTabHost(Context context) {
        super(context);
        initGestureListener();
    }

    public boolean onInterceptTouchEvent(MotionEvent event){
        super.onInterceptTouchEvent(event);
        // handle touch event. only return true if you handle it yourself here.
    }
}

第二种方式是设置递归设置触摸监听所有子视图自定义触摸监听器,

ViewGroup myLayout = ...;
registerTouchListener(myLayout, myTouchListener);

private void registerTouchListener(View view, View.OnTouchListener listener) {
  view.setOnTouchListener(listener);
  if (view instanceof ViewGroup) {
    ViewGroup vg = (ViewGroup)view;
    for (int i = 0, n = vg.getChildCount(); i < n; i++) {
      View v = vg.getChildAt(i);
      registerTouchListener(v, listener);
    }
  }
}


文章来源: OnKeyListener or OnKeydown not work if children views take the focus