Android_UI开发总结(三):ConstraintLayout中recycleview空白处点击不生效问题

  • 先附上constraintLayout的布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.utils.PullExtendLayout
        android:id="@+id/contact_pull_extend_layout_parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.ui.ContactExtendListHeader
            android:id="@+id/search_header"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp"/>

        <com.utils.CustomRecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </com.utils.PullExtendLayout>

    <TextView
        android:id="@+id/empty_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sorry, no data to show"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>


</android.support.constraint.ConstraintLayout>
  • 附上recyclerview 相应的adapter中的的部分代码
@Override
    public void onBindViewHolder(ListItemViewHolder holder, int position) {
        // TODO: 2018/5/16 适配渲染的数据到view中,提供的是一个holder
        ListItemViewHolder viewHolder = holder;
        final Object mObject = mMemberData.get(position);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DetailActivity.class);
                context.startActivity(intent);
            }
        });

此时发现在每一个item空白处点击时,有时不生效,需要下拉出所有数据item之后才点击有效。后来通过打开手机设置>开发者选项>显示布局边界查看发现有的item绘制出来的宽度并没有match_parent。这就是问题关键。
原因是在constraintLayout中又套上子布局时,子布局的android:layout_width不一定生效,具体怎么修改好比较复杂,这样选择替换成效率更高的FrameLayout布局来实现这个效果。
注意,在各种布局中,虽然ConstraintLayout布局比较容易实现开发,但是就效率而言,FrameLayout>RelateLayout>LinearLayout>ConstraintLayout。所以较简单布局要用效率高的布局。
- 附上修改后的布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.utils.PullExtendLayout
        android:id="@+id/pull_extend_layout_parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.ui.ContactExtendListHeader
            android:id="@+id/search_header"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginEnd="10dp"
            android:layout_marginStart="10dp"/>

        <com.utils.CustomRecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </com.utils.PullExtendLayout>

    <TextView
        android:id="@+id/empty_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sorry, no data to show"
        android:textSize="20sp"
       android:layout_gravity="center"/>
</FrameLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值