1. 程式人生 > >Android下拉重新整理和上拉載入

Android下拉重新整理和上拉載入

先看看XML佈局檔案,下拉重新整理和上拉載入哪個在外層並沒有什麼影響。最裡面嵌套了一個RecycleView。

<android.support.v4.widget.SwipeRefreshLayout

    android:id="@+id/gridswipreRefresh"

    android:layout_below="@+id/grid_view"

    style="?android:attr/progressBarStyle"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout

        android:id="@+id/gridrefreshLayout"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        app:srlAccentColor="#000000"

        app

:srlEnableHeaderTranslationContent="false"

        app:srlEnablePreviewInEditMode="true"

        app:srlEnableRefresh="false"

        app:srlPrimaryColor="#000000"

        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.RecyclerView

            android

:id="@+id/gs_recyclerview"

            android:layout_width="match_parent"

            android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>

        <com.scwang.smartrefresh.layout.footer.ClassicsFooter

            android:layout_width="match_parent"

            android:layout_height="wrap_content" />

     </com.scwang.smartrefresh.layout.SmartRefreshLayout>

</android.support.v4.widget.SwipeRefreshLayout>

    我們把swipeRefresh.setRefreshing(true)放在swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()這個函式中,而不是和mRefreshLayout.setNoMoreData(false)一樣放在search_sendRequestWithOkHttp()裡邊,是為了避免上拉載入的時候又同時重新整理,最後記得重新整理完之後要把重新整理功能關掉,即記得呼叫swipeRefresh.setRefreshing(false)函式。廢話不多說,上程式碼。

private SwipeRefreshLayout swipeRefresh;

private RefreshLayout mRefreshLayout;

private RecyclerView recyclerView;

recyclerView = (RecyclerView) findViewById(R.id.gs_recyclerview);

swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.gridswipreRefresh);

mRefreshLayout = findViewById(R.id.gridrefreshLayout);

swipeRefresh.setColorSchemeResources(R.color.colorPrimary);

swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

    @Override

    public void onRefresh() {

        swipeRefresh.setRefreshing(true);

        page = 1;

        if (svText.trim()!=""){

            if(((pvText_grid!="選擇網格")) && ((pvText_form!="主體業態"))){

                search_sendRequestWithOkHttp(

                        "&enterpriseName=%25"+svText+"%25&",

                        "&supervisionorg=%25"+pvText_grid+"%25",

                        "&mainbusinessform=%25"+pvText_form+"%25");

            }else if (((pvText_grid!="選擇網格")) && ((pvText_form=="主體業態"))){

                search_sendRequestWithOkHttp(

                        "&enterpriseName=%25"+svText+"%25",

                        "&supervisionorg=%25"+pvText_grid+"%25",

                        "");

            }else if (((pvText_grid=="選擇網格")) && ((pvText_form!="主體業態"))) {

                search_sendRequestWithOkHttp(

                        "&enterpriseName=%25"+svText+"%25",

                        "",

                        "&mainbusinessform=%25"+pvText_form+"%25");

            }else if (((pvText_grid=="選擇網格")) && ((pvText_form=="主體業態"))) {

                search_sendRequestWithOkHttp(

                        "&enterpriseName=%25"+svText+"%25",

                        "",

                        "");

            }

        }else {

            svText = "";

            if(((pvText_grid!="選擇網格")) && ((pvText_form!="主體業態"))){

                search_sendRequestWithOkHttp(

                        "",

                        "&supervisionorg=%25"+pvText_grid+"%25",

                        "&mainbusinessform=%25"+pvText_form+"%25");

            }else if (((pvText_grid!="選擇網格")) && ((pvText_form=="主體業態"))){

                search_sendRequestWithOkHttp(

                        "",

                        "&supervisionorg=%25"+pvText_grid+"%25",

                        "");

            }else if (((pvText_grid=="選擇網格")) && ((pvText_form!="主體業態"))) {

                search_sendRequestWithOkHttp(

                        "",

                        "",

                        "&mainbusinessform=%25"+pvText_form+"%25");

            }else if (((pvText_grid=="選擇網格")) && ((pvText_form=="主體業態"))) {

                search_sendRequestWithOkHttp(

                        "",

                        "",

                        "");

            }

        }

    }

});

mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

    @Override

    public void onLoadMore(RefreshLayout refreshlayout) {

        ++page;

        Log.i("page",String.valueOf(page));

        if (pvText_grid == "選擇網格" && pvOption1 == 0) {

            getFormCountRequestWithOkHttp("");

        }else {

            getFormCountRequestWithOkHttp(pvText_grid);

        }

                if (svText.trim()!=""){

                    if (((pvOption1!=0 || pvText_grid!="選擇網格")) && ((pvOption2==0 || pvText_form=="主體業態"))){

                        if (page < (main/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "&enterpriseName=%25"+svText+"%25",

                                    "&supervisionorg=%25"+pvText_grid+"%25",

                                    "");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvOption1!=0 || pvText_grid!="選擇網格")) && ((pvText_form=="食品銷售"))){

                        if (page < (food/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "&enterpriseName=%25"+svText+"%25",

                                    "&supervisionorg=%25"+pvText_grid+"%25",

                                    "");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvOption1!=0 || pvText_grid!="選擇網格")) && ((pvText_form=="餐飲服務"))){

                        if (page < (serve/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "&enterpriseName=%25"+svText+"%25",

                                    "&supervisionorg=%25"+pvText_grid+"%25",

                                    "");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvOption1!=0 || pvText_grid!="選擇網格")) && ((pvText_form=="單位食堂"))){

                        if (page < (canteen/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "&enterpriseName=%25"+svText+"%25",

                                    "&supervisionorg=%25"+pvText_grid+"%25",

                                    "");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvText_grid=="選擇網格")) && ((pvText_form=="單位食堂"))) {

                        if (page < (canteen/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "&enterpriseName=%25"+svText+"%25",

                                    "",

                                    "&mainbusinessform=%25"+pvText_form+"%25");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvText_grid=="選擇網格")) && ((pvText_form=="餐飲服務"))) {

                        if (page < (serve/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "&enterpriseName=%25"+svText+"%25",

                                    "",

                                    "&mainbusinessform=%25"+pvText_form+"%25");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvText_grid=="選擇網格")) && ((pvText_form=="食品銷售"))) {

                        if (page < (food/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "&enterpriseName=%25"+svText+"%25",

                                    "",

                                    "&mainbusinessform=%25"+pvText_form+"%25");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvText_grid=="選擇網格")) && ((pvOption2==0 || pvText_form=="主體業態"))) {

                        if (page < (main/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "&enterpriseName=%25"+svText+"%25",

                                    "",

                                    "");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }

                }else {

                    svText = "";

                    if (((pvOption1!=0 || pvText_grid!="選擇網格")) && ((pvOption2==0 || pvText_form=="主體業態"))){

                        if (page < (main/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "",

                                    "&supervisionorg=%25"+pvText_grid+"%25",

                                    "");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvOption1!=0 || pvText_grid!="選擇網格")) && ((pvText_form=="食品銷售"))){

                        if (page < (food/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "",

                                    "&supervisionorg=%25"+pvText_grid+"%25",

                                    "&mainbusinessform=%25"+pvText_form+"%25");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvOption1!=0 || pvText_grid!="選擇網格")) && ((pvText_form=="餐飲服務"))){

                        if (page < (serve/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "",

                                    "&supervisionorg=%25"+pvText_grid+"%25",

                                    "&mainbusinessform=%25"+pvText_form+"%25");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvOption1!=0 || pvText_grid!="選擇網格")) && ((pvText_form=="單位食堂"))){

                        if (page < (canteen/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "",

                                    "&supervisionorg=%25"+pvText_grid+"%25",

                                    "&mainbusinessform=%25"+pvText_form+"%25");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvText_grid=="選擇網格")) && ((pvText_form=="單位食堂"))) {

                        if (page < (canteen/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "",

                                    "",

                                    "&mainbusinessform=%25"+pvText_form+"%25");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvText_grid=="選擇網格")) && ((pvText_form=="餐飲服務"))) {

                        if (page < (serve/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "",

                                    "",

                                    "&mainbusinessform=%25"+pvText_form+"%25");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvText_grid=="選擇網格")) && ((pvText_form=="食品銷售"))) {

                        if (page < (food/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "",

                                    "",

                                    "&mainbusinessform=%25"+pvText_form+"%25");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }else if (((pvText_grid=="選擇網格")) && ((pvOption2==0 || pvText_form=="主體業態"))) {

                        if (page < (main/20+1)) {

                            search_sendRequestWithOkHttp(

                                    "",

                                    "",

                                    "");

                        }else {

                            mRefreshLayout.finishLoadMoreWithNoMoreData();

                        }

                    }

                }

            }

});

public void search_sendRequestWithOkHttp(String enterpriseName, String supervisionorg, String mainbusinessform) {

    mRefreshLayout.setNoMoreData(false);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    recyclerView.addItemDecoration(new DividerItemDecoration(GridSupervisionActivity.this, DividerItemDecoration.VERTICAL_LIST));

    url= "grid/findByExample?page=" + page + "&pageSize=" + pageSize + enterpriseName + supervisionorg + mainbusinessform;

    Log.i("urll",url);

    HttpUtil.sendOkHttpRequest(url,new okhttp3.Callback(){

        @Override

        public void onResponse(Call call, Response response) throws IOException {

            responseData = response.body().string();

            Log.i("search_responseData",responseData);

            parseJSONWithGSON(responseData);

            //handler.post(gridrunnableUi);

        }

        @Override

        public void onFailure(Call call,IOException e){

            String responseData = "網路錯誤";

            showResponse(responseData);

        }

    });

    gridSupervisionAdapter = new GridAdapter(gridlistDataItemArrayList);

    recyclerView.setAdapter(gridSupervisionAdapter);

    Log.i("recyclerView-----------",".......................................");

}

public void parseJSONWithGSON(String jsonData) {

    Gson gson = new GsonBuilder().create();

    Gridlist gridlist= gson.fromJson(jsonData,Gridlist.class);

    ArrayList<Gridlist> gridlistData = gridlist.getData();

    //Log.i("search_responseData",responseData);

    if (gridlistData != null) {

        if (page == 1) {

            gridlistDataItemArrayList.clear();

        }

        gridlistDataItemArrayList.addAll(gridlistData);

        showResponse("資料載入成功");

        Log.i("search_responseData",".......................................");

    }else {

        showResponse("資料載入失敗");

    }

}

private void showResponse(final String response) {

    runOnUiThread(new Runnable() {

        @Override

        public void run() {

            if (response.equals("資料載入成功")||response.equals("無結果")){

                gridSupervisionAdapter.notifyDataSetChanged();

            }else {

                ToastUtil.showToast(GridSupervisionActivity.this,response);

            }

            swipeRefresh.setRefreshing(false);

            mRefreshLayout.finishLoadMore();

            // 在這裡進行UI操作,將結果顯示到介面上

        }

    });

}