gpt4 book ai didi

java - 如何使用 swipeRefreshLayout 更新 fragment 中的新 json 数据

转载 作者:太空狗 更新时间:2023-10-29 16:14:17 26 4
gpt4 key购买 nike

在这个 fragment 中,我向服务器发送了一个带 volley 的 jsonRequest。并且我将 swipeRefreshLayout 设置为此 fragment ,我只想加载新的 json 数据,但是当我在 onRefresh()< 上使用 sendJsonRequest() 方法时recyclerView 启用所有 json 数据:

这是我的 fragment 代码:

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);

}
}

现在我的问题是在 OnRefresh 方法中,我可以写什么来只加载新的 json 数据,而不是所有现有的 json。

最佳答案

在你的代码中添加这一行

  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));
}

注意:当您在代码中创建任何类型的列表最多创建一次适配器实例时,更改列表数据后您只需调用“adapterInstance.notifyDataSetChange();”这是最好的做法,而不是每次刷新列表项时都创建列表,更多信息检查 here

关于java - 如何使用 swipeRefreshLayout 更新 fragment 中的新 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34414466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com