說一說ListView(一)

ListView作爲一個爛大街的Android控件(一句調侃,哈哈),我們今天簡單來總結下。當然了,它的被使用程度以及重要性自然也是不言而喻的。

 

一、首先來介紹下一個很常見的問題:ScrollView和ListView的衝突。

解決這個衝突,我們有兩種方法:自定義ListView、動態設置ListView的高度。

方法一:自定義ListView,實現起來很簡單,僅需一處改動。

這裏我們主要來講一下方法二:動態設置ListVie的高度。直接上源碼:

    /**
     * 動態設置ListView的高度
     * @param listView
     */
    public static void setListViewHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

注:使用方法二,ListView的item佈局的根元素必須爲LinearLayout。

 

二、ListView的上拉加載。

實現AbsListView.OnScrollListener接口即可,緊接着重寫onScrollStateChanged方法和onScroll方法。示例如下:

    @Override
    public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        switch (absListView.getId()) {
            case R.id.lv_other_report:
                otherReportLastIndex = firstVisibleItem + visibleItemCount;
                otherReportTotalIndex = totalItemCount;
                break;
            case R.id.lv_real_time_report:
                realTimeReportLastIndex = firstVisibleItem + visibleItemCount;
                realTimeReportTotalIndex = totalItemCount;
                break;
        }
    }
    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {
        switch (absListView.getId()) {
            case R.id.lv_other_report:
                if (otherReportLastIndex == otherReportTotalIndex && (scrollState == SCROLL_STATE_IDLE)) {
                    if (!isOtherReportEnd) {
                        if (isReportLoading) {
                            new HintUtils().showTopToast(getActivity(), "正在加載...");
                        } else {
                            relative_footer_other_report.setVisibility(View.VISIBLE);
                            isReportLoading = true;
                            isDefaultLoad = false;
                            isOtherReportLoadMore = true;
                            loadingPatrolReport = true;
                            otherReportLimitBegin += otherReportLimit;
                            new GetPatrolReportThread(handler, null, null, null, null, null, otherReportLimitBegin + "", otherReportLimit + "", false).start();
                        }
                    }
                }
                break;
            case R.id.lv_real_time_report:
                if (realTimeReportLastIndex == realTimeReportTotalIndex && (scrollState == SCROLL_STATE_IDLE)) {
                    if (!isRealTimeReportEnd) {
                        if (isReportLoading) {
                            new HintUtils().showTopToast(getActivity(), "正在加載...");
                        } else {
                            relative_footer_real_time_report.setVisibility(View.VISIBLE);
                            isReportLoading = true;
                            isDefaultLoad = false;
                            isRealTimeReportLoadMore = true;
                            realTimeReportLimitBegin += realTimeReportLimit;
                            new GetRealTimeReportThread(getRealTimeReportHandler, null, null, null, null, null, realTimeReportLimitBegin + "", realTimeReportLimit + "", false).start();
                        }
                    }
                }
                break;
        }
    }

 

發佈了75 篇原創文章 · 獲贊 54 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章