Android:scrollview與listview共存

來源:互聯網
上載者:User

      前幾天在開發Android開發中,遇到一個問題:當scrollview與listview共存,也就是互相之間有包含的情況下,並ListView只顯示一個半Item。

      由於開始並不知道這兩者不能共存,所以修改了很長時間。在網上搜尋類似的問題,大部分的網頁是教你怎樣建立listview,不過還有找到瞭解決方案:android中scrollview與listview共存。

  ScrollView與ListView共存會存在滾動的問題,並且ListView只顯示一個半Item。 當ListView的高度設定一定的值時,ListView同樣地會顯示對應的高度的Item。
因此我們可以計算出這個ListView的總高度,再將它設定到ListView中,那麼之前的滾動,高度問題也就不存在了。

   擷取並設定ListView的方法:

public void setListViewHeightBasedOnChildren(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));      ((MarginLayoutParams)params).setMargins(10, 10, 10, 10);    listView.setLayoutParams(params);  } 

   

   參考文章: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.