步驟:
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中去,求更權威的解答。