我想增加listview的高度,我正在使用这种方法
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
这是我的列表视图
<ListView
android:id="@+id/lvItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#3A9ED4"
android:dividerHeight="2px" />
但是在这里我的问题是高度没有增加基于它占据的项目显示一些空的空间 任何人都可以建议我......
答案 0 :(得分:1)
使用此方法:
public void setListViewHeightBasedOnChildren(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(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
我希望它有所帮助!
答案 1 :(得分:0)
试试这个,基于listview使用Child。 setListViewHeightBasedOnChildren()这将设置基于listview子项的高度。
public class Utils {
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++) {
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.requestLayout();
}
}
答案 2 :(得分:0)
您可以根据列表项设置身高。
int itemcount=listAdapter.getCount();
ViewGroup.LayoutParams params = listview.getLayoutParams();
params.height =(itemcount*60);
listview.setLayoutParams(params);
listview.requestLayout();
并在layout.xml中将listview height设置为0dp
android:layout_height="0dp"
答案 3 :(得分:0)
@Rajesh答案很好,只需确保在之后调用方法,你的listview已经加载了如下数据。
setListViewHeightBasedOnChildren(yourlistView);
答案 4 :(得分:0)
尝试此代码我确信这将有效:
1)创建一个名为 LVHelper 的新类,并插入以下代码:
public class LVHelper {
// Created By Rahul Ravindran 20/07/17
// This class increases listview's height as and when more item is added
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
//do nothing return null
return;
}
//set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
//setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
myListView.setLayoutParams(params);
}
2)设置列表视图的适配器后,添加以下代码:
myListView.setAdapter(#Your Adapter Here#);
LVHelper.getListViewSize(myListView);