RecylcerView注意事项

解决keyboard弹出时,recyclerView无法自动顶上去,被虚拟键盘遮盖。


        recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (bottom < oldBottom) {
                    recyclerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            recyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
                        }
                    }, 100);
                }
            }
        });

RecyclerView滑动时取消虚拟键盘

recyclerView.setOnTouchListener(new View.OnTouchListener() {
            private float startY;
            private float endY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        endY = event.getRawY();
                        float data = Math.abs(endY - startY);
                        if (data > ViewConfiguration.get(MainActivity.this).getScaledTouchSlop()) {
                            if (editText != null) {
                                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                                imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
                            }
                        }
                        break;
                    default:
                        break;

                }
                return false;
            }
        });

你可能感兴趣的:(RecylcerView注意事项)