LinearLayout嵌套RecycleView后无法点击,onClick失效的问题

今天在做项目时,写到了一个LinearLayout里面嵌套了RecycleView后,LinearLayout.setOnCilckListener没反应的问题,我认为是RecycleView截获了click事件。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout    
  2.                    android:id="@+id/linCompanyDepartment"    
  3.                    android:layout_width="match_parent"    
  4.                    android:layout_height="wrap_content"    
  5.                    android:background="@drawable/selector"    
  6.                    android:descendantFocusability="blocksDescendants"    
  7.                    android:visibility="visible">    
  8.     
  9.                    <TextView    
  10.                        android:layout_width="wrap_content"    
  11.                        android:layout_height="wrap_content"    
  12.                        android:layout_marginLeft="@dimen/myMargin"    
  13.                        android:layout_marginRight="@dimen/myMarginLeft"    
  14.                        android:gravity="right"    
  15.                        android:paddingBottom="@dimen/myMarginLeft"    
  16.                        android:paddingTop="@dimen/myMarginLeft"    
  17.                        android:text="部门"    
  18.                        android:textColor="@color/text_666"    
  19.                        android:textSize="@dimen/text_14sp" />    
  20.     
  21.     
  22.                    <android.support.v7.widget.RecyclerView    
  23.                        android:id="@+id/rvDepartment"    
  24.                        android:layout_width="0dp"    
  25.                        android:layout_height="wrap_content"    
  26.                        android:layout_weight="1"    
  27.                        android:padding="@dimen/myMarginLeft" />    
  28.     
  29.                    <TextView    
  30.                        android:layout_width="wrap_content"    
  31.                        android:layout_height="wrap_content"    
  32.                        android:layout_gravity="center_vertical"    
  33.                        android:layout_marginRight="@dimen/myMarginLeft"    
  34.                        android:background="@drawable/icon_arrow"    
  35.                        android:gravity="right"    
  36.                        android:textColor="@color/text_like" />    
  37.                </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才执行跳转。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. rvDepartment.setOnTouchListener(new View.OnTouchListener() {    
  2.            @Override    
  3.            public boolean onTouch(View v, MotionEvent event) {    
  4.                if (event.getAction() == MotionEvent.ACTION_DOWN) {//此处是点击按下时才执行跳转动作    
  5.                    DebugUtils.printLogE("rvDepartment:touch");    
  6.                    startActivity(new Intent(ProfileEdit.this, EditDepartment.class));    
  7.                }    
  8.                return false;    
  9.            }    
  10.        });    

转载请标明出处:http://blog.csdn.net/lovekam

在adapter里对recyclerView的item设置点击事件后,再对recyclerView本身设置touch事件时,捕捉不到Down这个action。

item的点击事件将recyclerView的点击事件覆盖了。
如果要同时存在,需要在
item的跟布局加上 android:descendantFocusability="blocksDescendants" 
在点击控件加上 android:focusable="false"

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
一般情况下,登录界面的布局可以使用LinearLayout嵌套来实现。例如,我们可以使用一个垂直的LinearLayout容器作为登录界面的根布局,然后在其中再添加两个水平的LinearLayout容器,一个用于输入用户名,一个用于输入密码。具体的实现方式可以参考以下代码: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 输入用户名的布局 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" /> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="请输入用户名" /> </LinearLayout> <!-- 输入密码的布局 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" /> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="请输入密码" /> </LinearLayout> <!-- 登录按钮 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" /> </LinearLayout> ``` 以上代码实现了一个简单的登录界面,其中包含了一个垂直的LinearLayout容器作为根布局,然后在其中添加了两个水平的LinearLayout容器,分别用于输入用户名和密码。最后再添加一个登录按钮,用于提交用户的登录信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值