滚动到 RecycleView 中的下一篇文章时如何获取 ItemPosition

问题描述

我使用 json api Retrofit 和 wordpress 作为后端制作应用程序,前 10 个帖子运行顺畅,当我点击这些帖子时,我可以看到帖子详细信息,但是当我滚动以获取更多帖子时,我可以看到这些帖子,但是当我点击它我看到这个错误

 Caused by: java.lang.indexoutofboundsexception: Index: 10,Size: 10
    at java.util.ArrayList.get(ArrayList.java:437)
    at com.punjabidharti.myapplication.PostDetails.onCreate(PostDetails.java:30)

和我的 PostDetails.Class

 Intent i = getIntent();
    int position = i.getExtras().getInt("itemPosition");
    Log.e("PostDetails ","title is " + MainActivity.mListPost.get(position).getTitle().getRendered());
    this.title = (TextView) findViewById(R.id.title);
    title.setText(Html.fromHtml(MainActivity.mListPost.get(position).getTitle().getRendered()));
    String data = String.valueOf((Html.fromHtml(MainActivity.mListPost.get(position).getContent().getRendered())));

    WebView webview = (WebView)this.findViewById(R.id.postwebview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadData(data,"text/html; charset=utf-8","UTF-8");

这就是我在滚动上获得更多帖子的方式:

 recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView,int dx,int dy) {
            if (dy > 0) { //check for scroll down
                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                if (loading) {
                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                        loading = false;
                        Log.v("...","Last Item Wow !");
                        // Do pagination.. i.e. fetch new data

                        yourURL = "https://punjabidharti.com/wp-json/wp/v2/posts/?categories=4514&page=2";

                        getRetrofit();


                        loading = true;
                    }
                }
            }
        }
    });

这就是我从 Retrofit 获取数据的方式:

public void getRetrofit(){
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseURL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
    String yourURl = yourURL.replace(baseURL,"");
    Call<List<WPPost>>  call = service.getPostInfo( yourURl);
    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);
            if (response.body() != null) {
                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().getWpAttachment().get(0).getHref()));
                }

                progressBar.setVisibility(View.GONE);
            } else {
                progressBar.setVisibility(View.GONE);
            }


            adapter.notifyDataSetChanged();
        }
        @Override
        public void onFailure(Call<List<WPPost>> call,Throwable t) {
        }
    });

我已经搜索一个多星期,但仍然无法找到答案。请帮忙

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...