0

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;
          }
        }
       } 
Sarath Kn
  • 2,680
  • 19
  • 24
pb007
  • 153
  • 1
  • 5
  • 13

1 Answers1

0
list.setOnScrollListener(new OnScrollListener() {

        public void onScrollStateChanged(AbsListView view, int scrollState) {    

        }

        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
            {
                if(flag_loading == false)
                {
                    flag_loading = true;
                    additems();
                }
            }
        }
    });
Goku
  • 9,102
  • 8
  • 50
  • 81
Sreejesh K Nair
  • 563
  • 1
  • 6
  • 16
  • what is flag_loading – pb007 Nov 28 '17 at 05:32
  • in the additem() add the next 10 items to array list and set the flag_loading as false. and call notifydatasetchanged. it should work – Sreejesh K Nair Nov 28 '17 at 05:34
  • check this link – Sreejesh K Nair Nov 28 '17 at 05:35
  • https://stackoverflow.com/questions/16398921/dynamic-listview-adding-load-more-items-at-the-end-of-scroll – Sreejesh K Nair Nov 28 '17 at 05:35
  • I think you will get an idea from the following example http://www.mysamplecode.com/2012/07/android-listview-load-more-data.html – Sreejesh K Nair Nov 28 '17 at 06:02
  • You can also use RecyclerView widget.The RecyclerView widget is a more advanced and flexible version of ListView . This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views – Sreejesh K Nair Nov 28 '17 at 06:22
  • I have done this, but it's not working on below 23 version please this new question thank you once again https://stackoverflow.com/questions/47546039/endless-scrolling-in-not-working-in-below-marshmallow-version – pb007 Nov 29 '17 at 06:04