溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android開發(fā)如何實現(xiàn)ScrollView中嵌套兩個ListView

發(fā)布時間:2021-07-10 12:44:39 來源:億速云 閱讀:212 作者:小新 欄目:移動開發(fā)

這篇文章主要介紹Android開發(fā)如何實現(xiàn)ScrollView中嵌套兩個ListView,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

具體如下:

做的項目中要使用兩個ListView在同一個頁面上下顯示,因為數(shù)據(jù)源不同,不能通過在Adapter中設(shè)置標(biāo)志位去區(qū)分顯示,最后只能硬著頭皮做一個ScrollView嵌套兩個ListView,但按正常情況是不能同時顯示的,會出現(xiàn)上面的ListView完全顯示,下面的只顯示一個Item,查了一些資料終于成功了

主要有一個ListViewUtility ,代碼如下:

import java.io.File;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ListViewUtility {  // 是一個Scrollview 中 添加兩個Listview的顯示布局
  public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
      // pre-condition
      return;
    }
    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
//     Global.pos = 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,如下

signListview.setAdapter(mYinggaoSignListAdapter);
appListView.setAdapter(adapter);
ListViewUtility.setListViewHeightBasedOnChildren(signListview);
ListViewUtility.setListViewHeightBasedOnChildren(appListView);

最后把兩個ListView添加到 ListViewUtility中,并且兩個ListView要填充完再添加,兩個ListView中的adpter的布局最外層要使用LinearLayout,只有LinearLayout 在ListViewUtility 中才能成功測量每個Item 的高度,不然就會出錯。

以上是“Android開發(fā)如何實現(xiàn)ScrollView中嵌套兩個ListView”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI