getLastVisiblePosition在片段中返回-1

时间:2013-12-03 09:08:04

标签: android

我有一个listfragment,其listview是使用联系人提供程序中的cursorloader加载的。

要求:首次加载listview时,我需要使用view.getLastVisiblePosition()一次,并且每次滚动时都需要。

问题:view.getLastVisiblePosition在onScrollListener中有效,但在onActivityCreated()或onCreateView()或onViewCreated()中始终返回-1。

代码

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Set up ListView, assign adapter and set some listeners. The adapter was previously
        // created in onCreate().
        setListAdapter(mAdapter);

        final ListView v = getListView();

        v.setOnItemClickListener(this);
        v.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                // Pause image loader to ensure smoother scrolling when flinging
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                    mImageLoader.setPauseWork(true);
                } else {
                    mImageLoader.setPauseWork(false);
                }

                if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
                    evaluateInviteView();
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {}
        });



        if (mIsTwoPaneLayout) {
            // In a two-pane layout, set choice mode to single as there will be two panes
            // when an item in the ListView is selected it should remain highlighted while
            // the content shows in the second pane.
            v.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }

        // If there's a previously selected search item from a saved state then don't bother
        // initializing the loader as it will be restarted later when the query is populated into
        // the action bar search view (see onQueryTextChange() in onCreateOptionsMenu()).
        if (mPreviouslySelectedSearchItem == 0) {
            // Initialize the loader, and create a loader identified by ContactsQuery.QUERY_ID
            getLoaderManager().initLoader(ContactsQuery.QUERY_ID, null, this);
        }

evaluateInviteView();
    }

 private void evaluateInviteView(){
        final ListView v = getListView();
        int LastVisiblePosition = v.getLastVisiblePosition();
        Log.d("faizal","inside : " + LastVisiblePosition );
    }

另一方面,类似问题在getLastVisiblePosition returning -1被问到。一个解决方案建议在runnable中执行view.getLastVisiblePosition()。但是对我来说这也是-1。

2 个答案:

答案 0 :(得分:0)

我通常用它来获得最后的可见位置

list.setOnScrollListener(new OnScrollListener() {

     @Override
     public void onScrollStateChanged(AbsListView view,
             int scrollState) { // TODO Auto-generated method stub
         int threshold = 1;
         int count = list.getCount();

         if (scrollState == SCROLL_STATE_IDLE) {
             if (list.getLastVisiblePosition() >= count
                     - threshold) {
                //do your stuff

             }
         }
     }

答案 1 :(得分:0)

我也遇到了这个问题,我正在拔头发。解决方案包含在此处:getLastVisiblePosition returning -1

实际填充ListView项需要一些时间,因此对getLastVisiblePosition()的调用将返回-1,直到发生这种情况。解决方案是将新Runnable发布到Listview,其中所有代码都依赖于最后一个可见位置,并执行Runnable内的所有内容。

例如,我正在编写的一些代码动态修改每个列表项中的TextView:

list.post(new Runnable() {
    @Override
    public void run() {
        int start = list.getFirstVisiblePosition();
        int last = list.getLastVisiblePosition();
        for (int i = start; i <= last; i++) {
            View vv = list.getChildAt(i-start);
            View v = list.getAdapter().getView(i, vv, list);
            TextView tv = (TextView) v.findViewById(R.id.list_text);
            // other stuff...
        }
    }
});