Android ScrollView 和ListView 一起使用的問題匯總

來源:互聯網
上載者:User

標籤:

1.ScrollView 嵌套 ListView  ,touch事件的截獲問題。

參考 http://www.cnblogs.com/lqminn/archive/2013/03/02/2940194.html
http://blog.csdn.net/chaihuasong/article/details/17499799

_scrollView.requestDisallowInterceptTouchEvent(true);

這句話的意思是告訴scrollView,滾動的事件交給我處理。用完以後記得還回去

_scrollView.requestDisallowInterceptTouchEvent(false);

如果不設定回去,ScrollView將無法滾動了。

 

2.ScrollView 滾動時,ListView的第一個條目是否處於顯示狀態?

參考 http://stackoverflow.com/questions/4628800/android-how-to-check-if-a-view-inside-of-scrollview-is-visible

boolean checkNeedRefresh() {        Rect scrollBounds = new Rect();        View firstChild = listView.getChildAt(0);        _scrollView.getHitRect(scrollBounds);        if (firstChild.getLocalVisibleRect(scrollBounds)) {            // Any portion of the firstChild, even a single pixel, is within the            // visible window            return true;        } else {            // NONE of the firstChild is within the visible window            return false;        }    }

 

3. listView不能顯示完整

參考 http://blog.csdn.net/hahashui123/article/details/39177057
http://blog.csdn.net/solomonxiang/article/details/26507145

public static void setListViewHeight(ListView listView) {        try {            int totalHeight = 0;            ListAdapter adapter = listView.getAdapter();            for (int i = 0, len = adapter.getCount(); i < len; i++) { // listAdapter.getCount()                View listItem = adapter.getView(i, null, listView);                listItem.setLayoutParams(new LayoutParams(                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));                listItem.measure(0, 0);                 totalHeight += listItem.getMeasuredHeight();             }            ViewGroup.LayoutParams params = listView.getLayoutParams();            params.height = totalHeight                    + (listView.getDividerHeight() * (listView.getCount() - 1));            listView.setLayoutParams(params);        } catch (Exception ex) {            ex.printStackTrace();        }    }

順帶GridView

public static void setGridViewHeight(GridView gridView, int numColumns) {        try {            ListAdapter adapter = gridView.getAdapter();            int row = 3;            View listItem = adapter.getView(0, null, gridView);            if (listItem == null)                return;            listItem.setLayoutParams(new LayoutParams(                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));            listItem.measure(0, 0);             int totalHeight = listItem.getMeasuredHeight() * row                    + (gridView.getVerticalSpacing() * (row - 1));            ViewGroup.LayoutParams params = gridView.getLayoutParams();            params.height = totalHeight;            gridView.setLayoutParams(params);        } catch (Exception ex) {            ex.printStackTrace();        }    }

 如果ListView 帶有BottomView

public static void setListViewHeight(ListView listView) {        try {            int totalHeight = 0;            int bottomHeight = 0;            ListAdapter dataAdapter = null;            int totalItems = 0;            ListAdapter adapter = listView.getAdapter();            if (adapter instanceof HeaderViewListAdapter) {                HeaderViewListAdapter headerViewListAdapter = ((HeaderViewListAdapter) adapter);                dataAdapter = headerViewListAdapter.getWrappedAdapter();                totalItems = dataAdapter.getCount();                int allItems = headerViewListAdapter.getCount();                View bottomItem = headerViewListAdapter.getView(allItems - 1,                        null, listView);                bottomItem.measure(0, 0);                bottomHeight = bottomItem.getMeasuredHeight();            } else {                dataAdapter = adapter;            }            for (int i = 0, len = totalItems; i < len; i++) {                View listItem = dataAdapter.getView(i, null, listView);                listItem.setLayoutParams(new LayoutParams(                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));                listItem.measure(0, 0);                totalHeight += listItem.getMeasuredHeight();            }            int listviewCount = listView.getCount();            int height = totalHeight                    + (listView.getDividerHeight() * listviewCount + 1)                    + bottomHeight;            ViewGroup.LayoutParams params = listView.getLayoutParams();            params.height = height;            listView.setLayoutParams(params);            listView.requestLayout();                     } catch (Exception ex) {            ex.printStackTrace();        }    }

 

4. 其他,自訂控制項實現ListView

http://www.cnblogs.com/lesliefang/p/3587154.html 

 

Android ScrollView 和ListView 一起使用的問題匯總

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.