下拉刷新是一个布局,需要在布局文件中添加,我的灯泡控制项目的该部分布局如下
1. 布局
<?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" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment_connect_scan" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.wifi.main.MainActivity"> <LinearLayout android:id="@+id/connect_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout style="@style/button_wrapper_style" android:paddingTop="8dp" android:paddingBottom="8dp"> <TextView android:id="@+id/down_refresh" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:gravity="center" android:text="@string/down_refresh" android:textColor="@color/qmui_config_color_gray_5" android:textSize="18dp" tools:layout_editor_absoluteX="135dp" tools:layout_editor_absoluteY="353dp" /> </RelativeLayout> <RelativeLayout style="@style/button_wrapper_style" android:paddingTop="0dp" android:paddingBottom="0dp"> <com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout android:id="@+id/pull_to_refresh" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/qmui_config_color_background"> <ListView android:id="@+id/mdns_list" android:paddingLeft="8dp" android:paddingRight="8dp" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout> </RelativeLayout> </LinearLayout> </FrameLayout>2.使用
private QMUIPullRefreshLayout mPullRefreshLayout;
mPullRefreshLayout = (QMUIPullRefreshLayout)view.findViewById(R.id.pull_to_refresh);
mPullRefreshLayout.setOnPullListener(new QMUIPullRefreshLayout.OnPullListener() { @Override public void onMoveTarget(int offset) { } @Override public void onMoveRefreshView(int offset) { } @Override public void onRefresh() { mPullRefreshLayout.postDelayed(new Runnable() { @Override public void run() { //扫描时间 long scanTime = 0; final QMUITipDialog tipDialog; openBroadcast(getContext()); //加载扫描到的服务 ConfigWifi configWifi = ConfigWifi.init(); configWifi.scanMDNS(); mdnsList = configWifi.getMdnsList(); flag = true; while(flag) { if (mdnsList.size() > 0) { /*ListAdapter mAdapter = new MyAdapter(getContext(), mdnsList); mlistView.setAdapter(mAdapter); new WifiUtils.Utility().setListViewHeightBasedOnChildren(mlistView); flag=false;*/ //显示服务信息 initListView(mdnsList); flag = false; } if ((System.currentTimeMillis() - scanTime) > 10000) { scanTime = System.currentTimeMillis(); } else { //超时退出 flag = false; } } if (!flag){ tipDialog = new QMUITipDialog.Builder(getContext()) .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL) .setTipWord("扫描超时,请稍候重试") .create(); tipDialog.show(); //显示两秒关闭 mListView.postDelayed(new Runnable() { @Override public void run() { tipDialog.dismiss(); } },2000); } mPullRefreshLayout.finishRefresh(); } }, 12000); } });效果如下所示: