红外遥控中,焦点移动到viewpager,没有被fragmet的textview控件得到焦点, 打印日志查看焦点 View rootView = getWindow().getDecorView(); View focusView = rootView.findFocus(); Logcat.i("focusView=" + focusView); 发现焦点一直在viewpager内的RecyclerView上, focusView=androidx.viewpager2.widget.ViewPager2$RecyclerViewImpl{2f5d9ca VFED..... .F...... 0,0-1920,676 #1} 解决方法:清除viewpager的recyclerview的焦点 mBinding.viewpager.setAdapter(adapter); for(int i=0;i<mBinding.viewpager.getChildCount();i++){ RecyclerView recyclerView = (RecyclerView) mBinding.viewpager.getChildAt(i); recyclerView.clearFocus(); recyclerView.setFocusable(false); }
再运行就发现控件可以获取到焦点了
focusView=androidx.appcompat.widget.AppCompatTextView{962c3dd VFED..... .F...... 260,444-460,504 #7f0a029a app:id/tv_install}