Recyclerview 中包含 webView 和 EditT

2018-12-20  本文已影响0人  androidfan

不知道怎么就遇到了一些奇葩的需求,掉进了一写奇葩的坑,现在的话就是在记录一下脱坑历程,最近做的一个页面,分为上中下三个部分,上面是原生的布局,中间全是webView 下面又是一个原生布局。一开始是打算是用NestedScrollView 来写的,结果做出来发现,webView的高度无法自适应,而且连滑动也是冲突了。没有办法,只有使用RecyClerview ,把上面的部分和webView 写在一个布局里,作为Rv的第一个Viewholder,最底部的原生页面作为第二个ViewHolder.
写出来之后发现webView 抢占了输入框的焦点事件,导致编辑框无法调起输入法输入。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   android:descendantFocusability="blocksDescendants"
    tools:context=".activity.ProDetalsH5Activity">

    <include
        android:id="@+id/include"
        layout="@layout/layout_title" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/webRecy"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

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

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">


        <TextView
            android:id="@+id/proNames"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_33"
            android:layout_marginTop="@dimen/dp_29"
            android:text="日常保洁"
            android:textColor="#ff333333"
            android:textSize="@dimen/sp_32"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/view"
            android:layout_width="@dimen/dp_58"
            android:layout_height="@dimen/dp_5"
            android:layout_marginTop="@dimen/dp_19"
            android:background="#E4922A"
            app:layout_constraintLeft_toLeftOf="@id/proNames"
            app:layout_constraintTop_toBottomOf="@id/proNames" />

        <TextView
            android:id="@+id/proInfo"
            android:layout_width="@dimen/dp_303"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_27"
            android:lineSpacingExtra="@dimen/dp_5"
            android:text="提供家庭室内表面清洁、物品归纳整理和高温
消费服务。"
            android:textColor="#ff999999"
            android:textSize="15sp"
            app:layout_constraintLeft_toLeftOf="@id/view"
            app:layout_constraintTop_toBottomOf="@id/view" />

        <View
            android:id="@+id/view1"
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_1"
            android:layout_marginTop="@dimen/dp_29"
            android:background="#EEEEEE"
            app:layout_constraintLeft_toLeftOf="@id/proInfo"
            app:layout_constraintTop_toBottomOf="@id/proInfo" />

        <ImageView
            android:id="@+id/view2"
            android:layout_width="@dimen/dp_19"
            android:layout_height="@dimen/dp_19"
            android:layout_marginLeft="@dimen/dp_74"
            android:layout_marginTop="@dimen/dp_24"
            android:src="@drawable/people_iocn"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@id/view1" />

        <TextView
            android:id="@+id/canPeople"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_10"
            android:text="在线可预约服务人员  89 人"
            android:textColor="#ffd74e3f"
            android:textSize="@dimen/sp_15"
            app:layout_constraintBottom_toBottomOf="@id/view2"
            app:layout_constraintLeft_toRightOf="@id/view2"
            app:layout_constraintTop_toTopOf="@id/view2" />


        <LinearLayout
            android:id="@+id/layoutTime"
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_43"
            android:layout_marginTop="@dimen/dp_27"
            android:background="@drawable/shape_rounded_rectangle_color_gray_stork"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/canPeople">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/sp_15"
                android:text="预约时间:"
                android:textColor="#ff626262"
                android:textSize="@dimen/sp_14" />


            <TextView
                android:id="@+id/inputTime"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center|left"
                android:hint="年/月/日 --:--"
                android:paddingLeft="@dimen/dp_15"
                android:textColor="@color/rv_item_tv" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutAddress"
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_43"
            android:layout_marginTop="@dimen/dp_12"
            android:background="@drawable/shape_rounded_rectangle_color_gray_stork"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layoutTime">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/sp_15"
                android:text="服务地址:"
                android:textColor="#ff626262"
                android:textSize="@dimen/sp_14" />


            <TextView
                android:id="@+id/inputAddress"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:ellipsize="end"
                android:gravity="center|left"
                android:hint="请填写您的楼号-门牌号"
                android:maxLength="20"
                android:paddingLeft="@dimen/dp_15"
                android:singleLine="true"
                android:textColor="@color/rv_item_tv" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutPhone"
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_43"
            android:layout_marginTop="@dimen/dp_12"
            android:background="@drawable/shape_rounded_rectangle_color_gray_stork"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layoutAddress">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/sp_15"
                android:text="联系电话:"
                android:textColor="#ff626262"
                android:textSize="@dimen/sp_14" />


            <EditText
                android:id="@+id/inputPhone"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@null"
                android:cursorVisible="true"
                android:maxLength="11"
                android:gravity="center_vertical"
                android:hint="请填写您的联系方式"
                android:inputType="number"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:paddingLeft="@dimen/dp_15"
                android:textColor="@color/rv_item_tv"
                android:textCursorDrawable="@drawable/color_cursor"
                android:textSize="@dimen/sp_14" />


        </LinearLayout>

        <TextView
            android:id="@+id/callPeople"
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_43"
            android:layout_marginTop="@dimen/dp_19"
            android:background="@drawable/confirm_roundbtn_click_selector"
            android:clickable="true"
            android:gravity="center"
            android:text="呼叫服务"
            android:textColor="@color/white"
            android:textSize="@dimen/sp_15"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layoutPhone" />

        <View
            android:layout_width="@dimen/dp_303"
            android:layout_height="@dimen/dp_1"
            android:layout_marginTop="@dimen/dp_29"
            android:background="#EEEEEE"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/callPeople" />

    </android.support.constraint.ConstraintLayout>


    <!--<include-->
    <!--layout="@layout/activity_pro_details_h5_in_put_content"-->
    <!--android:visibility="gone" />-->


    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
       android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_height="wrap_content"
        tools:ignore="WebViewLayout" />


</LinearLayout>

其实我是认为这样写是没问题的,我给EditText 设置了获取焦点的属性,给webView 设置了禁止获取焦点的属性,但是运行结果是狠狠打脸了,编辑框是无法输入的,后来才发现,罪魁祸首是这一个属性

   android:descendantFocusability="blocksDescendants"

我居然不知道什么时候给我的Activity布局上加入了这个属性,对于这个属性,有的一说

  beforeDescendants:viewgroup会优先其子类控件而获取到焦点
  afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
  blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

所以当我把这个属性去掉后,editText 就能正常输入了。

另外,关于Koltin 中给editText 赋初值的方法

习惯了java的setText 方法后,使用Kotlin 已经没有这种写法了,ed.text="123456799"结果这种写法会报一个类型不匹配的错误,现在也说一下解决的办法。那么正确的写法就是:

  mHolder.input_phone?.text=
                        if (TextUtils.isEmpty(mPhone)) Editable.Factory.getInstance().newEditable("请输入手机号")
                        else Editable.Factory.getInstance().newEditable(mPhone)

对的,就是要把文字转成EditAble 类型的才可以正常设置。

上一篇 下一篇

猜你喜欢

热点阅读