简体   繁体   中英

Infinite scrolling issue, while scrolling down and api call function works and list position goes to the first item

private StringRequest VolleyPostlogin(final int requestCount){

        System.out.println("MeeeHistoryServerResponseeNrequestCount "+requestCount);




        mSwipeRefreshLayout.setRefreshing(true);
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
        String URL = Constants.APP_URL+"get_history_new";

        final String mRequestBody = "{\"jsonrpc\": \"2.0\",\"params\":{\"page\":"+requestCount+"}}";
        System.out.println("HisResponseeNmRequestBody "+mRequestBody);
        //   final String mRequestBody = "{\"jsonrpc\": \"2.0\",\"params\":{}}";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("VOLLEY", response);

                System.out.println("HisResponseeNmRequestBody response "+response);

                try{

                    JSONObject mainObject = new JSONObject(response);
                    JSONObject result = mainObject.getJSONObject("result");
                    String status = result.getString("status");
                    if(status.equals("200")){
                        JSONArray Arrresponse = result.getJSONArray("response");

                        if(Arrresponse.length()>0) {
                            for (int i = 0; i < Arrresponse.length(); i++) {
                                try{
                                    JSONObject codeObject = Arrresponse.getJSONObject(i);
                                    System.out.println("Arrresponse "+codeObject);

                                    name.add(codeObject.getString("name"));


                                    try{
                                        SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm a", Locale.ENGLISH);
                                        df.setTimeZone(TimeZone.getTimeZone("UTC"));
                                        Date date = df.parse(codeObject.getString("actual_start_date"));
                                        df.setTimeZone(TimeZone.getDefault());
                                        actual_start_date.add(df.format(date));

                                    }catch (Exception e){
                                        System.out.println("ArrresponseException "+e.toString());
                                        actual_start_date.add("");
                                    }


                                    try{
                                        SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm a", Locale.ENGLISH);
                                        df.setTimeZone(TimeZone.getTimeZone("UTC"));
                                        Date date = df.parse(codeObject.getString("actual_stop_date"));
                                        df.setTimeZone(TimeZone.getDefault());
                                        actual_stop_date.add(df.format(date));

                                    }catch (Exception e){
                                        actual_stop_date.add("");
                                        System.out.println("ArrresponseException "+e.toString());
                                    }


                                    actual_duration.add(codeObject.getString("actual_duration"));

                                    try{
                                        SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm a", Locale.ENGLISH);
                                        df.setTimeZone(TimeZone.getTimeZone("UTC"));
                                        Date date = df.parse(codeObject.getString("planned_start_date"));
                                        df.setTimeZone(TimeZone.getDefault());
                                        planned_start_date.add(df.format(date));

                                    }catch (Exception e){
                                        planned_start_date.add("");
                                        System.out.println("ArrresponseException "+e.toString());
                                    }

                                    try{
                                        SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy hh:mm a", Locale.ENGLISH);
                                        df.setTimeZone(TimeZone.getTimeZone("UTC"));
                                        Date date = df.parse(codeObject.getString("planned_stop_date"));
                                        df.setTimeZone(TimeZone.getDefault());
                                        planned_stop_date.add(df.format(date));

                                    }catch (Exception e){
                                        planned_stop_date.add("");
                                        System.out.println("ArrresponseException "+e.toString());
                                    }

                                    meeting_status.add(codeObject.getString("status"));
                                    latitude.add(codeObject.getString("planned_latitude"));
                                    longitude.add(codeObject.getString("planned_longitude"));
                                    meeting_id.add(codeObject.getString("id"));
                                    external_attentendees.add(codeObject.getString("partner_name"));
                                    internal_attentendees.add(codeObject.getString("attendees"));
                                    planned_duration.add(codeObject.getString("planned_duration"));
                                    show_as.add(codeObject.getString("show_as"));
                                    location.add(codeObject.getString("location"));
                                    Status.add(codeObject.getString("status"));
                                    tags.add(codeObject.getString("tags"));
                                    reminders.add(codeObject.getString("reminders"));
                                    description.add(codeObject.getString("description"));
                                    summary.add(codeObject.getString("summary"));
                                    mRecyclerView.setVisibility(View.VISIBLE);

                                    historyAdapter = new HistoryAdapter(name,actual_start_date,actual_stop_date,planned_start_date,planned_stop_date,latitude,longitude,meeting_status,meeting_id,external_attentendees,internal_attentendees,planned_duration,show_as,tags,reminders,description,actual_duration,Status,summary,location,mSwipeRefreshLayout,activity);
                                    mRecyclerView.setAdapter(historyAdapter);
//                                    progressBar.setVisibility(View.GONE);
                                    mSwipeRefreshLayout.setRefreshing(false);
                                    historyAdapter.notifyDataSetChanged();
                                    NoData.setVisibility(View.GONE);


                                    manager = new LinearLayoutManager(activity);
                                    mRecyclerView.setLayoutManager(manager);

                                    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                                        @Override
                                        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                                            super.onScrollStateChanged(recyclerView, newState);
                                            if(newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL)
                                            {
                                                isScrolling = true;
                                                Toast.makeText(getActivity(),"onScrollStateChanged " , Toast.LENGTH_LONG).show();

                                            }
                                        }

                                        @Override
                                        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                                            super.onScrolled(recyclerView, dx, dy);
                                            currentItems = manager.getChildCount();
                                            totalItems = manager.getItemCount();
                                            scrollOutItems = manager.findFirstVisibleItemPosition();
                                            System.out.println("NEWonScrolledmanager currentItems "+currentItems);
                                            System.out.println("NEWonScrolledmanager totalItems "+totalItems);
                                            System.out.println("NEWonScrolledmanager scrollOutItems "+scrollOutItems);


                                            if(isScrolling && (currentItems + scrollOutItems == totalItems))
                                            {
                                                Toast.makeText(getActivity(),"onScrolled " , Toast.LENGTH_LONG).show();
                                                isScrolling = false;
                                                getData();
                                            }


                                        }
                                    });

                                }catch (Exception e){

                                    Toast.makeText(getActivity(),"Something went wrong, please try again later" , Toast.LENGTH_LONG).show();
                                    System.out.println("ServerResponseExceptionCatch "+e.toString());

                                }
                            }

                        }else {
                            mSwipeRefreshLayout.setRefreshing(false);
                        }


                    }else {
                        NoData.setVisibility(View.VISIBLE);
                        mSwipeRefreshLayout.setRefreshing(false);
                    }

                }catch (Exception e){

                    Toast.makeText(getActivity(),"Something went wrong, please try again later" , Toast.LENGTH_LONG).show();
                    System.out.println("ServerResponseExceptionCatch "+e.toString());
//                    progressBar.setVisibility(View.GONE);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                try{
                    Log.e("VOLLEY", error.getLocalizedMessage());
                    mSwipeRefreshLayout.setRefreshing(false);
                    System.out.println("ServerResponse error"+error);
                    Toast.makeText(getActivity(),"Something went wrong, please try again later" , Toast.LENGTH_LONG).show();
//                   progressBar.setVisibility(View.GONE);
                }catch (Exception e){
                    System.out.println("ServerResponseException "+e.toString());
                    Toast.makeText(getActivity(),"Something went wrong, please try again later" , Toast.LENGTH_LONG).show();
                    System.out.println("ServerResponseExceptionCatch "+e.toString());
//                   progressBar.setVisibility(View.GONE);
                }


            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }
            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                            mRequestBody, "utf-8");
                    return null;
                }

            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                System.out.println("ConstantsSessionId "+Constants.SessionId);
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/json");
                params.put("X-Openerp-Session-Id", Constants.SessionId);
                return params;
            }

        };

        requestQueue.add(stringRequest);
        return stringRequest;
    }

I'm using android volley in my project for infinite scrolling when i scroll down api call and its not resuming to the list position which i have seen last,it goes to the top of the list. Here is my complete code for infinite scrolling, my issue is when I try to scroll down and API call the list will go straight to the top instead of staying in the last position where API call happens, I tried many ways to sort out this issue but it's not working. Please someone help me to fix this issue.

Thanks in advance.

What you are trying to achieve is called as 'Pagination'.

You are doing it in a wrong way.

Follow this tutorial

you will get it done.

And don't implement your network call or adapter in activity or fragment. Create new classes for that. It will make your code more readable and easy to track. Check MVP pattern in android.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM