简体   繁体   English

如何使用SwipeRefreshLayout更新RecyclerView中的json数据

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

I am new in android development. 我是android开发的新手。 I am learning it. 我正在学习。 I am able to fetch json data in listview using Recyclerview. 我能够使用Recyclerview在listview中获取json数据。 Now I want to include swiperRefreshLayout, so any update in server also displayed while refreshing. 现在,我想包含swiperRefreshLayout,因此刷新时也会显示服务器中的任何更新。 I search tutorial about swiperRefreshLayout in json, but this doesn't work. 我在json中搜索有关swiperRefreshLayout的教程,但这不起作用。 I tried using DefaultHttpClient, but I can't catch this idea. 我尝试使用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);
    }

On refresh call your parseJSON() make sure before this you need to clear your array. 在刷新时,调用parseJSON()确保在此之前需要清除数组。

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

SwipeLayout has nothing to do with the server(Network calls). SwipeLayout与服务器无关(网络调用)。 When the user pulls to refresh the onRefresh() of your SwipeRefreshLayout.OnRefreshListener is called. 当用户拉动刷新SwipeRefreshLayout的onRefresh()时,将调用OnRefreshListener。 That's it, that is all the SwipeRefreshLayout does. 就是这样,这就是SwipeRefreshLayout所做的全部。

This might help you- 这可能对您有帮助-

    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