Android:无法解析方法'findFirstVisibleItemPosition()'?

时间:2015-03-29 08:14:27

标签: android android-recyclerview

我正在尝试在回收站视图上编写无限滚动的代码。这是给我编译错误的片段:

@Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

                if ( (visibleItemCount+pastVisiblesItems) >= totalItemCount) {
                    Log.v("...", "Last Item Wow !");
                }

我之前写的声明是:

mLayoutManager = new LinearLayoutManager(this);

mLayoutManager是RecyclerView.LayoutManager类的对象

1 个答案:

答案 0 :(得分:23)

mLayoutManager is an object of class RecyclerView.LayoutManager错误,您应该android.support.v7.widget.LinearLayoutManager使用mLayoutManager,所以:

mLayoutManager = new LinearLayoutManager(this); 
//above 'LinearLayoutManager' is from
//'android.support.v7.widget.LinearLayoutManager'

mRecyclerView.setLayoutManager(mLayoutManager);

然后mLayoutManager.findFirstVisibleItemPosition();调用onScrolled(...);

希望这有帮助!