如何使用swipeRefreshLayout仅更新片段中的新json数据

时间:2015-12-22 11:07:43

标签: java android json android-fragments

在这个片段中,我向服务器发送一个带有齐射的jsonRequest。我正在将swipeRefreshLayout设置为此片段,我只想加载新的json数据,但是当我在sendJsonRequest()上使用onRefresh()方法时,所有json数据都被启用到{ {1}}:

这是我的片段代码:

recyclerView

现在我的问题在于package ghandak.ghandshekan.com.ghandak.fragments; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonArrayRequest; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.List; import ghandak.ghandshekan.com.ghandak.R; import ghandak.ghandshekan.com.ghandak.adapters.PostRecyclerAdapter; import ghandak.ghandshekan.com.ghandak.app.AppController; import ghandak.ghandshekan.com.ghandak.models.PostData; /** * Created by imajid on 12/19/2015. */ public class TabFragment3 extends Fragment implements OnRefreshListener{ private RecyclerView allContentRecyclerView; private String url = "http://kakdo.herokuapp.com/api/news/?format=json"; private List<PostData> postDataList = new ArrayList<PostData>(); private SwipeRefreshLayout swipeRefreshLayout; //====================================================================================== onCreateView @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.tab_fragment_3 , container , false); allContentRecyclerView = (RecyclerView)view.findViewById(R.id.xmlRecyclerViewtabFragment3); allContentRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); swipeRefreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.xml_swipe_refresh_layout_tab_fragment_3); swipeRefreshLayout.setOnRefreshListener(this); swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); //swipeRefreshLayout.setColorSchemeColors(); return view; } //====================================================================================== onCreate @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); sendJsonRequest(); } //====================================================================================== sendjsonRequest private void sendJsonRequest() { JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, (String) null, new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { parseJsonResponse(response); //==========setting adapter to the recyclerview <== allContentRecyclerView.setAdapter(new PostRecyclerAdapter(getActivity() ,postDataList)); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }); AppController.getInstance().addToRequestQueue(request); } //====================================================================================== parsjsonResponse() private void parseJsonResponse(JSONArray response) { if(response == null){ Toast.makeText(getActivity(), "ریسپانس خالی هستش", Toast.LENGTH_SHORT).show(); return; }else { Log.d("parsejsonresponse", "response khali nist"); for (int i = 0 ; i < response.length() ; i++ ){ try { //Toast.makeText(getActivity(), "ریسپانس میگیرم ", Toast.LENGTH_SHORT).show(); JSONObject currentPost = response.getJSONObject(i); //Log.d("currentPost", "currentPost ro gereftam"); PostData postData = new PostData(); postData.setTitle(currentPost.getString("title")); //Toast.makeText(getActivity() , currentPost.getString("title") , Toast.LENGTH_SHORT).show(); postData.setCreate(currentPost.getString("create")); postDataList.add(postData); } catch (JSONException e) { e.printStackTrace(); } } } } @Override public void onRefresh() { sendJsonRequest(); swipeRefreshLayout.setRefreshing(false); } } 方法,我可以编写什么来加载新的json数据,而不是所有存在的json。

3 个答案:

答案 0 :(得分:4)

在代码中添加此行

  postDataList.clear(); in your code given in bellow.

<强>更新

           @Override
    public void onResponse(JSONArray response) {
        if(response == null )
            return;
        postDataList.clear();
        parseJsonResponse(response);
        //==========setting adapter to the recyclerview  <==
        allContentRecyclerView.setAdapter(new PostRecyclerAdapter(getActivity() ,postDataList));
    }

注意:在代码中创建任何类型的列表max create adapter instance时,在更改列表数据之后,您只需要调用&#39; adapterInstance.notifyDataSetChange();&# 39; 这是最佳做法,而不是每次刷新列表项时更好地创建列表,进一步信息检查here

答案 1 :(得分:1)

添加此代码,

private SwipeRefreshLayout mSwipeRefreshLayout;


mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        mSwipeRefreshLayout.setRefreshing(true);
                    }
                }, SPLASH_DISPLAY_LENGTH);
            }
        });

当你想要刷新它时,

mSwipeRefreshLayout.setRefreshing(true);

答案 2 :(得分:1)

    swipeContainer = (SwipeRefreshLayout) rootView.findViewById(R.id.swipeContainer);
            swipeContainer.setOnRefreshListener(new OnRefreshListener()
            {

                @Override
                public void onRefresh()
                {
                    //clear your old data
                    callForData();
                    //swipeContainer.setRefreshing(false); use this while get data and set in you ui
                }
            });

            // Configure the refreshing colors
            swipeContainer.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);