parameter must be a descendant of this view

解决RecyclerView中删除含有EditText的item时出现的焦点异常问题。通过清除被删除item中仍持有焦点的控件焦点,避免因父布局变化导致的bug。

最近开发的时候,recycleView里面有一个EditText,删除item的时候出现这个问题。本着能百度就不思考的原则,到网上搜索了一下。看到了各种源码分析,但是都不能解决问题。所以写一下解决思路。其实这个就是典型的谜底就在谜面上,
这句话翻译过来就是:参数必须是这个view的后代。因为我们把item删除了,但是它的焦点还在。但是父布局变了,导致触发了这个bug。所以我们只要移除焦点就行了。

一开始使用父布局元素设置下面的代码发现没有用

android:focusableInTouchMode="true"
android:background="@drawable/bg_all_half_blue_light"

后来改成点击删除后刷新item时候,从activity 中获取占用焦点的控件:

View currentFocus = activity.getCurrentFocus();
if (currentFocus != null) {
    currentFocus.clearFocus();
}

问题解决了。

Caused by: java.lang.IllegalArgumentException: parameter must be a descendant of this view at android.view.ViewGroup.offsetRectBetweenParentAndChild(ViewGroup.java:6295) at android.view.ViewGroup.offsetDescendantRectToMyCoords(ViewGroup.java:6224) at com.st.launcher.view.TVRecyclerView.findFocusView(TVRecyclerView.java:632) at com.st.launcher.view.TVRecyclerViewTest.testFindFocusView_1_withRealViews(TVRecyclerViewTest.java:824) @Test public void testFindFocusView_1_withRealViews() throws Exception { TVRecyclerView spy = spy(new TVRecyclerView(RuntimeEnvironment.application)); FrameLayout parent = new FrameLayout(RuntimeEnvironment.application); Button child = new Button(RuntimeEnvironment.application); parent.addView(child); child.setFocusable(true); child.setFocusableInTouchMode(true); Method findFocusView = TVRecyclerView.class.getDeclaredMethod("findFocusView", View.class, View.class); findFocusView.setAccessible(true); findFocusView.invoke(spy, parent, child); } private void findFocusView(View parent, View child) { if (child.isFocusable() && child.isFocusableInTouchMode()) { Rect rect = new Rect(); child.getDrawingRect(rect); offsetDescendantRectToMyCoords(child, rect); offsetRectIntoDescendantCoords(parent, rect); Log.d(TAG, "onChildAttachedToWindow: rect = " + rect); if (rect.contains(mFocusRect)) { if (nofityFocusView != null) { nofityFocusView.clear(); nofityFocusView = null; } nofityFocusView = new WeakReference<View>(child); } } else if (child instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) child; int count = viewGroup.getChildCount(); for (int i = 0; i < count; i++) { findFocusView(parent, viewGroup.getChildAt(i)); } } }
最新发布
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值