[英]How to set the Dynamic height to list view in fragment activity..?
我需要在滾動視圖內實現列表視圖。 我知道在滾動視圖中使用listview不是一個好主意,但這是應用程序的要求,所以我需要實現它。 在應用程序即時消息中,將TabLayout與3種類型的標簽一起使用,例如類別1,類別2和類別3,以過濾數據。
在第一個選項卡類別1中,我需要添加2個列表視圖,一個是修復項目,第二個是動態的。 拳頭列表視圖是固定的,所以我可以給它固定的高度。 但是第二個列表視圖是動態的,因此我需要根據列表視圖中值的總數給出高度。
我嘗試了多種方法來提高動態高度,但沒有完成任務。
這是我可以嘗試的方法:-
-------------------第一------------------------------ -首先從問題
public static void getTotalHeightofListView(ListView listView) {
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
Log.w("HEIGHT" + i, String.valueOf(totalHeight));
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
- - - - - - - - - -第二 - - - - - - - - - - - - - - - -來自問題https://stackoverflow.com/questions/17693578
public static class ListUtils {
public static void setDynamicHeight(ListView mListView) {
ListAdapter mListAdapter = mListView.getAdapter();
if (mListAdapter == null) {
// when adapter is null
return;
}
int height = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(mListView.getWidth(), MeasureSpec.UNSPECIFIED);
for (int i = 0; i < mListAdapter.getCount(); i++) {
View listItem = mListAdapter.getView(i, null, mListView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
height += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = mListView.getLayoutParams();
params.height = height + (mListView.getDividerHeight() * (mListAdapter.getCount() - 1));
mListView.setLayoutParams(params);
mListView.requestLayout();
}
}
- - - - - - - - - -錯誤 - - - - - - - - - -
在您的活動xml文件中,可以在ScrollView中使用LinearLayout ,然后可以為其分配android:weight屬性。
您可以在單個元素和所有其他元素各自的可用空間上為其分配權重。
更好的做法是,它使用支持視圖動態變化的列表。 對於您的問題,有一個特殊列表-LinkedListView
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.