I want to load limited data on ListView at scrolling time. I am using volley response to fetch the data from server. I have a code which is used to load data at scrolling time but it's not working.
Below is a Similar_Matchs_Tab fragment class where we are fetching the data. And also using onStart and setUserVisibleHint method
public class Similar_Matchs_Tab extends Fragment {
SessionManager session;
String email;
public String JSON_URL;
private ListView listView;
private Boolean isStarted = false;
private Boolean isVisible = false;
public Similar_Matchs_Tab() {}
@Override
public void onStart() {
super.onStart();
isStarted = true;
if (isVisible && isStarted){
sendRequest();
}
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
isVisible = isVisibleToUser;
if (isStarted && isVisible) {
sendRequest();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
session = new SessionManager(getActivity());
HashMap<String, String> user = session.getUserDetails();
email = user.get(SessionManager.KEY_EMAIL);
JSON_URL = "https://www.maangal.com/maangal_mobile/similar_matches.php?matri_id="+email;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.matches_tab, container, false);
listView = (ListView) view.findViewById(R.id.listView);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
}
});
return view;
}
private void sendRequest(){
final ProgressDialog loading = ProgressDialog.show(getActivity(),"Loading Data", "Please wait...",false,false);
StringRequest stringRequest = new StringRequest(Request.Method.POST,JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
Log.e("Similar MAtches******",response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.getMessage(), Toast.LENGTH_LONG).show();
}
});
int MY_SOCKET_TIMEOUT_MS = 30000;
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
protected void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
Profile_Match_custom_List cl = new Profile_Match_custom_List(getActivity(), ParseJSON.ids,ParseJSON.ages, ParseJSON.heights, ParseJSON.communities,ParseJSON.castes,ParseJSON.educations,ParseJSON.occupations,ParseJSON.incomes,ParseJSON.pics,ParseJSON.locations,ParseJSON.shortlist,ParseJSON.expressinterest);
listView.setAdapter(cl);
}
}
And below is the OnScrollFinishListener abstract class where I implements AbsListView.OnScrollListener and I need 10 items at a time so I used bufferItemCount = 10
Please check my both code and tell me where I my going to wrong and help me to solve this issue.
public abstract class OnScrollFinishListener implements AbsListView.OnScrollListener {
private int bufferItemCount = 10;
private int currentPage = 0;
private int itemCount = 0;
private boolean isLoading = true;
public OnScrollFinishListener(int bufferItemCount) {
this.bufferItemCount = bufferItemCount;
}
public OnScrollFinishListener() {}
public abstract void loadMore(int page, int totalItemsCount);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
if (totalItemCount < itemCount) {
this.itemCount = totalItemCount;
if (totalItemCount == 0) {
this.isLoading = true; }
}
if (isLoading && (totalItemCount > itemCount)) {
isLoading = false;
itemCount = totalItemCount;
currentPage++;
}
if (!isLoading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + bufferItemCount)) {
loadMore(currentPage + 1, totalItemCount);
isLoading = true;
}
}
}