SwipeRefreshLayout内的listview设置emptyview无效处理
在SwipeRefreshLayout子view 添加一个FrameLayout ;然后把listview和emptyview放在framelayout就可以了
--->修改后的代码 多添加了 一层FrameLayout
<android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:scrollbars="none" /> <LinearLayout android:id="@+id/ll_default" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/rgb_efefef" android:orientation="vertical" android:paddingTop="110dp" android:visibility="gone"> <ImageView android:id="@+id/iv_default" android:layout_width="160dp" android:layout_height="120dp" android:layout_gravity="center_horizontal" android:background="@mipmap/love_default_search" /> <TextView android:id="@+id/tv_default" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="12dp" android:gravity="center_horizontal" android:lineSpacingExtra="12dp" android:text="没有找到相关用户\n换个搜索条件试试吧" android:textColor="@color/rgb_888888" android:textSize="14sp" /> </LinearLayout> </FrameLayout> </android.support.v4.widget.SwipeRefreshLayout>
代码中就可以 直接给lv设置emptyView
*******************************************************************************************************************************************************************************************
但是这样设置 会出现滑动冲突;
官方文档有提到swiperefeshlayout只有一个子view,通常情况是也具有滑动属性的listview grideview scrollview recycler view 等,但如果一定包含其他控件,会出现滑动冲突
解决办法:
添加帮助类:
public class ListViewHelper { /** * 设置空布局 * * @param context * @param dataSize 数据的大小 * @param msg 错误信息 * @param id 错误图片的id * @param parent 这个必须是swipeRefreshLayout的直接父布局 */ public static void checkEmptyView(Context context, int dataSize, String msg, int id, ViewGroup parent,View.OnClickListener listener) { if (dataSize == 0) { boolean emptyExist = false; for (int i = 0; i < parent.getChildCount(); i++) { View childView = parent.getChildAt(i); if (childView instanceof LinearLayout) { emptyExist = true; } } if (!emptyExist) { //空布局是个LinearLayout View emptyView = View.inflate(context, R.layout.listview_deafult, null); TextView tv_default = (TextView) emptyView.findViewById(R.id.tv_default); tv_default.setText(msg); ImageView iv_default = (ImageView) emptyView.findViewById(R.id.iv_default); iv_default.setBackgroundDrawable(context.getResources().getDrawable(id)); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER_HORIZONTAL); parent.addView(emptyView, lp); if (listener != null) { emptyView.setOnClickListener(listener); } } } else { boolean emptyExist = false; View childView = null; for (int i = 0; i < parent.getChildCount(); i++) { childView = parent.getChildAt(i); if (childView instanceof LinearLayout) { emptyExist = true; break; } } if (emptyExist) { parent.removeView(childView); } } } }
空布局:
<?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="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/ll_default" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/rgb_efefef" android:orientation="vertical" android:paddingTop="110dp"> <ImageView android:id="@+id/iv_default" android:layout_width="160dp" android:layout_height="120dp" android:layout_gravity="center_horizontal" android:background="@mipmap/love_default_search" /> <TextView android:id="@+id/tv_default" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="12dp" android:gravity="center_horizontal" android:lineSpacingExtra="12dp" android:text="加载中,请稍后……" android:textColor="@color/rgb_888888" android:textSize="14sp" /> </LinearLayout> </LinearLayout>
listview的代码布局:
<RelativeLayout android:id="@+id/rl_parent" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipeRefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:scrollbars="none" /> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>
代码:
拿到数据之后:根据数据的size
ListViewHelper.checkEmptyView(context, list.size(), "列表为空", R.mipmap.love_default_search, RlParent, new View.OnClickListener() { @Override public void onClick(View v) { onRefresh(); } });
ok