簡體   English   中英

如何使用SwipeRefreshLayout更新RecyclerView中的json數據

[英]How to use SwipeRefreshLayout to update json data in RecyclerView

我是android開發的新手。 我正在學習。 我能夠使用Recyclerview在listview中獲取json數據。 現在,我想包含swiperRefreshLayout,因此刷新時也會顯示服務器中的任何更新。 我在json中搜索有關swiperRefreshLayout的教程,但這不起作用。 我嘗試使用DefaultHttpClient,但是無法理解這個想法。

protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health);
        mRecyclerView = findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

            }
        });
        mExampleList = new ArrayList<>();

        mRequestQueue = Volley.newRequestQueue(this);
        parseJSON();
    }

    private void parseJSON() {
        String url = "http://fitandfineindustries.com/healthapi.php";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("info");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject hit = jsonArray.getJSONObject(i);

                                String creatorName = hit.getString("heading");
                                String img = hit.getString("img");
                                String imageUrl = img.length() == 0 ? "file:///android_asset/fitandfineindustries.jpg" : "http://fitandfineindustries.com/images/plan/"+img;

                                mExampleList.add(new NewsItem(imageUrl, creatorName));
                            }

                            mNewsAdapter = new NewsAdapter(HealthActivity.this, mExampleList);
                            mRecyclerView.setAdapter(mNewsAdapter);
                            mNewsAdapter.setOnItemClickListener(HealthActivity.this);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mRequestQueue.add(request);
    }

在刷新時,調用parseJSON()確保在此之前需要清除數組。

SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
         mExampleList.clear()
         parseJSON();
        }
    });
    mExampleList = new ArrayList<>();
    mRequestQueue = Volley.newRequestQueue(this);
    parseJSON();
}

SwipeLayout與服務器無關(網絡調用)。 當用戶拉動刷新SwipeRefreshLayout的onRefresh()時,將調用OnRefreshListener。 就是這樣,這就是SwipeRefreshLayout所做的全部。

這可能對您有幫助-

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            //call the parseJSON() function here like this.
            parseJSON();
            // call the setRefreshing(true) function to show
            // the circular rotating refresh icon
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });

    //Don't forget to hide the refreshing icon when your server call ends like this.
    //mSwipeRefreshLayout.setRefreshing(false);

暫無
暫無

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

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