ListView底部的removeFooterView

时间:2014-04-11 07:31:40

标签: android android-listview

我有一个从Web服务填充的ListView。该列表是从JSON中提取的,并通过BaseAdapter格式化。它是分页的。

因此,如果我请求http://example.com/item?page=2,它会将第2页附加到列表视图中。我目前正在使用OnScrollListener来请求这些页面。在每个页面的底部是我放置一个显示 loading ... 消息的页脚视图。

但是,在请求的ListView的最后一页,仍在显示正在加载的消息。我希望它能够自动删除。

这是Java代码:

public class LatestFragment extends ListFragment implements OnScrollListener {

    private int currentFirstVisibleItem;
    private int currentVisibleItemCount;
    private int currentScrollState;
    private boolean isLoading = false;

    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ...

        mFooter = inflater.inflate(R.layout.footer_loading, null);
        footerViewLayout = (RelativeLayout) mFooter.findViewById(R.id.footer_layout);

        return rootView;

    }


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

        ...

        getListView().addFooterView(footerViewLayout, null, false);

        loadItemList(1);
    }

    private void loadItemList(int page) {
        // request URL
    }


    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        this.currentFirstVisibleItem = firstVisibleItem;
        this.currentVisibleItemCount = visibleItemCount;
    }

    @Override
    public void onScrollStateChanged(AbsListView listView, int scrollState) {
        this.currentScrollState = scrollState;
        this.isScrollCompleted();
    }

    private void isScrollCompleted() {
        if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
                currentPage++;
                loadItemList(currentPage);
        }
    }
}

在List中没有要显示的新项目后,我在哪里放置.removeFooterView()来隐藏页脚视图?


更新1:

我对代码所做的改进。但是,当它到达最后一页时,我必须向下滚动一次以删除FooterView,因为它无法自动删除FooterView。到目前为止,这是我的问题的更好的解决方案。但我无法将其打勾解决:(

public class LatestFragment extends ListFragment implements OnScrollListener {

    private int currentFirstVisibleItem;
    private int currentVisibleItemCount;
    private int currentScrollState;
    private boolean isLoading = false;

    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ...

        mFooter = inflater.inflate(R.layout.footer_loading, null);
        footerViewLayout = (RelativeLayout) mFooter.findViewById(R.id.footer_layout);

        return rootView;

    }


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

        ...

        getListView().addFooterView(footerViewLayout, null, false);

        loadItemList(1);
    }

    private void loadItemList(int page) {

        if (page > 1) {
            if (lastKnownItemCount == currentItemCount){
                getListView().removeFooterView(footerViewLayout);
            }
        }

        lastKnownItemCount = currentItemCount;

        // request URL
    }


    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {
        this.currentFirstVisibleItem = firstVisibleItem;
        this.currentVisibleItemCount = visibleItemCount;
    }

    @Override
    public void onScrollStateChanged(AbsListView listView, int scrollState) {
        this.currentScrollState = scrollState;
        this.currentItemCount = listView.getCount();
        this.isScrollCompleted();
    }

    private void isScrollCompleted() {
        if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
            currentPage++;

            loadItemList(currentPage);
        }
    }
}

感谢@Wizard的想法。我希望有更好的解决方案。

1 个答案:

答案 0 :(得分:-1)

private void loadItemList(int page) {
        // request URL
        if(page is the last page){
               removeFooterView
        }
    }