今天在做项目时,写到了一个LinearLayout里面嵌套了RecycleView后,LinearLayout.setOnCilckListener没反应的问题,我认为是RecycleView截获了click事件。
- <LinearLayout
- android:id="@+id/linCompanyDepartment"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/selector"
- android:descendantFocusability="blocksDescendants"
- android:visibility="visible">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="@dimen/myMargin"
- android:layout_marginRight="@dimen/myMarginLeft"
- android:gravity="right"
- android:paddingBottom="@dimen/myMarginLeft"
- android:paddingTop="@dimen/myMarginLeft"
- android:text="部门"
- android:textColor="@color/text_666"
- android:textSize="@dimen/text_14sp" />
-
-
- <android.support.v7.widget.RecyclerView
- android:id="@+id/rvDepartment"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:padding="@dimen/myMarginLeft" />
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_marginRight="@dimen/myMarginLeft"
- android:background="@drawable/icon_arrow"
- android:gravity="right"
- android:textColor="@color/text_like" />
- </LinearLayout>
在我度搜了好久都搜不出有用的东西,都是说什么在LinearLayout中设置Android:descendantFocusability="blocksDescendants",获取焦点啥的,设置focusable="true"啊之类,通通试过都没有用。
于是用英文来谷歌,依然没发现直接能解决答案的代码,但是在google上看到一篇文章受到了启发:http://sapandiwakar.in/recycler-view-item-click-handler/ ,其实主要是看到了代码中“onInterceptTouchEvent“这个方法受到启发,可能是touch事件截获了焦点。然后尝试用RecycleView.setOnTouchListener来打印一下,果然,在这里。因此在LinearLayout设置了onClickListener后再设置RecycleView的setOnTouchListener就可以了。(我暂时找不到其他更好的办法了,只能这样暴力搞掂)
注意:setOnTouchListener会捕获到2次或3次,按下,抬起之类的手势,所以我这里需要判断是DOWN才执行跳转。
- rvDepartment.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- DebugUtils.printLogE("rvDepartment:touch");
- startActivity(new Intent(ProfileEdit.this, EditDepartment.class));
- }
- return false;
- }
- });
转载请标明出处:http://blog.youkuaiyun.com/lovekam
在adapter里对recyclerView的item设置点击事件后,再对recyclerView本身设置touch事件时,捕捉不到Down这个action。
item的点击事件将recyclerView的点击事件覆盖了。
如果要同时存在,需要在
item的跟布局加上 android:descendantFocusability="blocksDescendants"
在点击控件加上 android:focusable="false"