1

i am very new as Android Developers, and i am making app with json and retrofit to fetch posts from wordpress, i can see latest 10 posts but i want to load next 10 while scroll down, i use this code but it load same 10 posts again and again , this is my code in MainActivity

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
    {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {
            //  Log.e("...", " onscroll dx " + dx +" dy "+ dy);
            try{
                if(dy > 0) //check for scroll down
                {
                    visibleItemCount = mLayoutManager.getChildCount();
                    totalItemCount = mLayoutManager.getItemCount();
                    pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
                    if (loading)
                    {   //  Log.e(TAG, " page count lock "+PageCountLock);
                        progressBar.setVisibility(View.VISIBLE);
                        if ( pastVisiblesItems >= (totalItemCount/3) ){
                            Log.e(TAG," on scroll visible - "+ visibleItemCount+"  total "+ totalItemCount+"  pastvisible "+pastVisiblesItems );
                            getRetrofit( );
                        }
                        if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
                        {
                            // loading = false;
                            Log.e("...", "Last Item readed in list **** ");
                        }
                    }
                }
            }catch (Exception e){
                Log.e(TAG,"scrooll action "+ e.toString());
            }

        }
    });

please can you tell me how to fetch next posts ?

this is to get posts from Retrofit

public void getRetrofit(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
        Call<List<WPPost>>  call = service.getPostInfo();
        
        call.enqueue(new Callback<List<WPPost>>() {
            @Override
            public void onResponse(Call<List<WPPost>> call, Response<List<WPPost>> response) {
                Log.e("mainactivyt", " response "+ response.body());
                mListPost = response.body();
                progressBar.setVisibility(View.GONE);
                for (int i=0; i<response.body().size();i++){
                    Log.e("main ", " title "+ response.body().get(i).getTitle().getRendered() + " "+
                            response.body().get(i).getId());
                    String tempdetails =  response.body().get(i).getExcerpt().getRendered().toString();
                    tempdetails = tempdetails.replace("<p>","");
                    tempdetails = tempdetails.replace("</p>","");
                    tempdetails = tempdetails.replace("[&hellip;]","");
                    list.add( new Model( Model.IMAGE_TYPE,  response.body().get(i).getTitle().getRendered(),
                            tempdetails,
                            response.body().get(i).getLinks().getWpFeaturedmedia().get(0).getHref())  );
                }
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onFailure(Call<List<WPPost>> call, Throwable t) {
            }
        });
    }
    public static List<WPPost> getList(){
        return  mListPost;
    }
  • Does this answer your question? [How to implement endless list with RecyclerView?](https://stackoverflow.com/questions/26543131/how-to-implement-endless-list-with-recyclerview) – ADM Mar 12 '21 at 04:59
  • "it load same 10 posts again and again" you didn't include the code that requests the posts – Ryan M Mar 12 '21 at 05:13
  • @RyanM i think its getRetrofit – Punjabi Dharti Mar 12 '21 at 05:18
  • I assume so. You didn't include it in your post. – Ryan M Mar 12 '21 at 05:19
  • please check my updated question now @RyanM – Punjabi Dharti Mar 12 '21 at 05:25
  • You're making the exact same request every time: `service.getPostInfo();` - why do you expect that to return different results? – Ryan M Mar 12 '21 at 05:27
  • i am very new , please can you tell how to get next posts ? @RyanM – Punjabi Dharti Mar 12 '21 at 05:31
  • I have no idea. There's probably a parameter for page or offset or something. You need to specify that parameter in your request. – Ryan M Mar 12 '21 at 05:32
  • your api should look something like this http://yoursite.com/wp-json/wp/v2/tutorial?per_page=10&page=1 and your page should increase when user reaches bottom of recycler view then that should get you next page data – unownsp Mar 12 '21 at 05:37

0 Answers0