簡體   English   中英

如何使用 okhttp 在回收站視圖中進行無限滾動

[英]how to make endless scrolling in recycler view with okhttp

我正在使用 okhttp 獲取 json 數據並將其顯示在回收站視圖上。

OkHttpClient client = new OkHttpClient();

okhttp3.Request request = new okhttp3.Request.Builder()
        .url(url)
        .get()
        .addHeader("Content-Type", "application/x-www-form-urlencoded")
        .build();

client.newCall(request).enqueue(new Callback() {

    @Override
    public void onFailure(Call call, IOException e) {
        Log.e(TAG, "onFailure: ", e);
    }

    @Override
    public void onResponse(Call call, final okhttp3.Response response) throws IOException {
        final String data = response.body().string();

        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                try {
                    JSONArray array = new JSONArray(data);

                    for (int i = 0; i < array.length(); i++) {

                        JSONObject Object = array.getJSONObject(i);

                        String name = Object.getString("displayname");

                        User user = new User(name);
                        userlist.add(user);

                        adapter = new dataadapter(mContext, userlist);
                        recycler.setAdapter(adapter);
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });


    }
});

我希望如果用戶滾動到回收器視圖中的最后一個數據,它應該再次運行 okhttp 函數並再次連接到服務器並獲取 json 文件並將其添加到 recyclerview ..然后用戶應該獲得更多數據並且它應該繼續滾動。

請幫助我,我很多天都面臨這個問題,但我沒有得到任何解決方案...

請使用 Retrofit 將數據拉入/拉出 API 只需三個步驟即可顯示來自服務器的數據 1) 根據 json 制作模型類 2) 使用改造調用 api 3) 成功檢索數據后將其傳遞給 recyclerview 適配器就是這樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM