如何在数据更新后更改ListView时检查

时间:2014-01-28 21:07:05

标签: java android listview android-listview onscroll

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

有什么方法可以做到吗?

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

提前致谢!

我的方法,可以添加更多数据:

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();
            }
        }
    } 
});

1 个答案:

答案 0 :(得分: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.
        }
    }
}