天天看点

RecycleView添加HeaderView宽度不能撑满,第一次加载HeaderView的子控件获取不到焦点

步骤:

1、RecycleView.addHeaderView(view);

2、RecycleView.setAdapter(mAdapter)

3、请求数据

4、mAdapter.notifyDataSetChanged();

出问题了:

1、HeaderView的宽度不能撑满

2、HeaderView的控件不可点击

1、不能撑满解决方案:

在view的下方加一条线(有时候不行)或者把根布局换成RelativeLayout

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

    <LinearLayout
        android:id="@+id/ll_org_search"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginBottom="6dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="6dp"
        android:background="@drawable/shape_round_white_edit"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:drawableLeft="@mipmap/icon_search"
            android:drawablePadding="8dp"
            android:gravity="center"
            android:text="搜索"
            android:textColor="@color/gray_a"
            android:textSize="13sp" />
    </LinearLayout>
    <include layout="@layout/layout_line"/>  <!--关键代码-->
</LinearLayout>
           

HeaderView的子控件获取不到焦点

解决方案1:

在请求成功的onSuccess方法中判断是不是第一次加载的,如果是第一次就使用setAdapter,不是第一次就notifyDataSetChanged,完美解决了,但是我不喜欢这种解决方案
if (page == ) {
                    mPtrRvLayout.onRefreshComplete();
                    if(isFirst) {
                        isFirst = false;
                        mRvList.setAdapter(mCommonAdapter);
                    } else {
                        mCommonAdapter.notifyDataSetChanged();
                    }

                } else {
                    mRvList.onLoadMoreComplete();
                    mCommonAdapter.notifyDataSetChanged();
                }
           

解决方案2:

把HeaderView的子控件的onClick事件换成onTouch事件,也完美解决了
llOrgSearch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP) {
                    startActivity(new Intent(getContext(), VipListSearchActivity.class));
                }
                return true;
            }
        });
           

猜想原因:

所以我想原因可能是在进行事件分发的时候,在onTouch的时候,事件被拦截了,所以就没有分发到onClik中去,求更权威的解答。

继续阅读