根据其中的多行textview动态设置ListView Height [英] Set ListView Height dynamically based on multiline textview inside it
问题描述
我正在使用基本适配器来动态设置列表视图中的数据.我试图动态设置listview的高度.如果其中的textview是单行,则效果很好.但是,如果textview是多行,则高度设置不正确.高度仅考虑单行textview设置.如何正确设置包含多行textview的listview项的高度.这是代码:
I am using a base adapter to set data in a listview dynamically. I tried to set listview height dynamically. It works perfectly if the textview inside it is single line. However if the textview is multiline, the height is not set properly. The height is set considering single line textview only. How can I set correctly set the height of listview items containing multiline textview. Here's the code:
片段代码:
ListView mlistNews=(ListView)rootView.findViewById(R.id.news_listView);
mlistNews.setAdapter(new NewsAdapter(getActivity(),params_news));
Utils.setListViewHeightBasedOnItems(mlistNews);
实用功能:
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter != null) {
int numberOfItems = listAdapter.getCount();
// Get total height of all items.
int totalItemsHeight = 0;
for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
View item = listAdapter.getView(itemPos, null, listView);
item.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() *
(numberOfItems - 1);
// Get padding
int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight + totalPadding;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
} else {
return false;
}
}
dashboard_list_news.xml
dashboard_list_news.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<TextView
android:layout_width="0dp"
android:id="@+id/news_item_text"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/chevron"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp" />
</LinearLayout>
listView:
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rounded_shape_list"
android:id="@+id/news_listView"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:divider="#6DCB99"
android:dividerHeight="1dp" />
推荐答案
Teemu提到,您需要限制宽度.下面的示例代码.
As Teemu mentions you need to constrain the width. Sample code below.
float px = 300 * (listView.getResources().getDisplayMetrics().density);
item.measure(View.MeasureSpec.makeMeasureSpec((int)px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
这篇关于根据其中的多行textview动态设置ListView Height的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!