0

我有一个未解决的问题,我无法回答自己。有什么办法,如何检查ListView.getCount();更新数据后 ListView ( ) 是否更改 - 更准确地说,在加载更多过程之后。ProgressBar在我的更新方法之后 ListView 保持不变后,我想删除页脚视图 ( )。

有什么办法吗?

如有必要,请检查下面的短代码

提前致谢!

我的方法,它添加了更多数据:

public void updateData() {
    mListView = (ListView)getView().findViewById(R.id.animal_list);     
    final ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
    query.setCachePolicy(CachePolicy.NETWORK_ONLY);
    query.orderByAscending("animal");
    query.setLimit(mListView.getCount() + 5);

    Log.v("updateData", "uploading data from Parse.com");
    query.findInBackground(new FindCallback<Animal>() {

        @Override
        public void done(List<Animal> animals, ParseException error) {

            if(animals != null){
                mAdapter.clear();
                mProgressBar = (ProgressBar) getView().findViewById (R.id.loading_animals);
                mProgressBar.setVisibility(View.INVISIBLE);
                RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);  
                footie.setVisibility(View.VISIBLE);

                mAdapter.clear();

                for (int i = 0; i < animals.size(); i++) {

                    mAdapter.add(animals.get(i));

                }  

            }  
        }
    }); 
}

我的 OnScrollListener 发现必须更新数据:

mListView.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                         int totalItemCount) {
    }


    @Override
    public void onScrollStateChanged(AbsListView view,
        int scrollState) {

        int threshold = 1;
        int count = mListView.getCount();

        if (scrollState == SCROLL_STATE_IDLE) {
            if (mListView.getLastVisiblePosition() >= count - threshold){

                updateData();
                //here do I need to add - if ListView didnt change - > remove footer                        
            }

        }
        if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
            View currentFocus = getActivity().getCurrentFocus();

            if(currentFocus != null) {
                currentFocus.clearFocus();
            }
        }
    } 
});
4

1 回答 1

1

您可以扩展适配器并单独设置数据。

public void updateData() {
    ...

    query.findInBackground(new FindCallback<Animal>() {
        @Override
        public void done(List<Animal> animals, ParseException error) {

            if(animals != null){
                mProgressBar = (ProgressBar) getView().findViewById (R.id.loading_animals);
                mProgressBar.setVisibility(View.INVISIBLE);
                RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);  
                footie.setVisibility(View.VISIBLE);

                mAdapter.setItems(animals);
            }  
        }
    }); 
}

class CustomAdapter extends ArrayAdapter {
    public void setItems(List<Animal> animals) {
        int oldCount = getCount();
        int newCount = animals.size();
        clear();
        addAll(animals);
        if (oldCount != newCount) {
            // Do whatever you want here. The number of items changed.
        }
    }
}
于 2014-01-28T23:58:40.310 回答