我的应用程序有4个带有滑动标签布局的标签 我在MainActivity中使用了一个视图寻呼机,每个标签都有一个4片段...
我使用volley从mysql接收数据。 cardview。 Recycler视图和JSON
我想使用SwipeRefreshLayout刷新列表,但列表剂量刷新,我应该关闭应用程序并重启
在我的SwipeRefreshLayout OnRefreshListener中我调用了我的getdata()方法,但我认为更好的方法是刷新片段(重新加载)。
任何想法? 谢谢
我的所有片段代码:
public class AllOfAds extends Fragment {
SwipeRefreshLayout mSwipeRefreshLayout;
//Creating a List of superheroes
private List<Getter_Setter> listCars;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
//Volley Request Queue
private RequestQueue requestQueue;
//The request counter to send ?page=1, ?page=2 requests
private int requestCount = 1;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.allofads, container, false);
return v;
}
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
super.onCreate(savedInstanceState);
final ProgressBar progressBar = (ProgressBar) getActivity().findViewById(R.id.progressBar1);
mSwipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipeRefreshLayout);
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
//Initializing Views
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
//Initializing our superheroes list
listCars = new ArrayList<>();
requestQueue = Volley.newRequestQueue(getActivity());
//Calling method to get data to fetch data
getData();
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
getData();
adapter.notifyDataSetChanged();
}
});
//Adding an scroll change listener to recyclerview
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (isLastItemDisplaying(recyclerView)) {
//Calling the method getdata again
getData();
progressBar.setVisibility(View.GONE);
}
}
});
//initializing our adapter
adapter = new CardAdapter(listCars, getActivity());
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
}
//Request to get json from server we are passing an integer here
//This integer will used to specify the page number for the request ?page = requestcount
//This method would return a JsonArrayRequest that will be added to the request queue
private JsonArrayRequest getDataFromServer(int requestCount) {
//Initializing ProgressBar
final ProgressBar progressBar = (ProgressBar) getActivity().findViewById(R.id.progressBar1);
//Displaying Progressbar
//JsonArrayRequest of volley
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ConfigJson.DATA_URL + String.valueOf(requestCount),
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Calling method parseData to parse the json response
parseData(response);
//Hiding the progressbar
mSwipeRefreshLayout.setRefreshing(false);
progressBar.setVisibility(View.GONE);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
//If an error occurs that means end of the list has reached
Toast.makeText(getActivity(), "no internet access!", Toast.LENGTH_SHORT).show();
}
});
//Returning the request
return jsonArrayRequest;
}
//This method will get data from the web api
private void getData() {
//Adding the method to the queue by calling the method getDataFromServer
requestQueue.add(getDataFromServer(requestCount));
//Incrementing the request counter
requestCount++;
}
//This method will parse json data
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
//Creating the superhero object
Getter_Setter cars = new Getter_Setter();
JSONObject json = null;
try {
//Getting json
json = array.getJSONObject(i);
//Adding data to the superhero object
cars.setImageUrl(json.getString(ConfigJson.TAG_IMAGE_URL));
cars.setName(json.getString(ConfigJson.TAG_PUBLISHER));
cars.setBrand(json.getString(ConfigJson.TAG_BRAND));
} catch (JSONException e) {
e.printStackTrace();
}
//Adding the superhero object to the list
listCars.add(cars);
}
//Notifying the adapter that data has been added or changed
adapter.notifyDataSetChanged();
}
//This method would check that the recyclerview scroll has reached the bottom or not
private boolean isLastItemDisplaying(RecyclerView recyclerView) {
if (recyclerView.getAdapter().getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
Toast.makeText(getActivity(), "end of ads...!", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
编辑:我的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar1"
android:layout_gravity="center"/>
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>