我有一个Listview,我需要获取所有项目的总高度总和,以便ListView高度等于所有项目的高度。我试图实现以下类来实现它。
public class UIUtils {
/**
* Sets ListView height dynamically based on the height of the items.
*
* @param listView to be resized
* @return true if the listView is successfully resized, false otherwise
*/
public static boolean setListViewHeightBasedOnItems(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if(listAdapter != null)
{
int numberOfItems = listAdapter.getCount();
WindowManager wm = (WindowManager) listView.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int deviceWidth;
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size = new Point();
display.getSize(size);
deviceWidth = size.x;
} else {
deviceWidth = display.getWidth();
}
int desiredWidth = View.MeasureSpec.makeMeasureSpec(deviceWidth, View.MeasureSpec.AT_MOST);
int desiredHeight = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
// 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(desiredWidth, desiredHeight);
totalItemsHeight += item.getMeasuredHeight();
}
// Get total height of all item dividers.
int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1);
int totalPadding = listView.getPaddingBottom() + listView.getPaddingTop();
// Set list height.
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalItemsHeight + totalDividersHeight + totalPadding;
listView.setLayoutParams(params);
listView.requestLayout();
return true;
}
else
{
return false;
}
}
}
以下是我最终如何使用前面描述的类:
ListView list = (ListView) findViewById(R.id.bills);
BillsListViewAdapter listViewAdapter = new BillsListViewAdapter(context, R.layout.item_bill, bills);
list.setAdapter(listViewAdapter);
UIUtils.setListViewHeightBasedOnItems(list);
所提及的类只会返回所有项的静态值,即使在某些情况下高度可能会有所不同,因为项目中TextView的文本可能有两行或更多行。
item_bil.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:padding="10dp"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/number"
android:layout_marginEnd="2dp"
android:textSize="12sp"/>
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="{{number}}"
android:textSize="12sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hour"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginEnd="2dp"
android:textSize="12sp"/>
<TextView
android:id="@+id/hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="{{hour}}"
android:textSize="12sp"/>
</LinearLayout>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="{{name}}"
android:textSize="16sp"
android:textColor="@color/colorPrimary"
android:layout_marginBottom="5dp"
android:gravity="center_vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="5dp">
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="{{address}}"
android:textStyle="bold"
android:textSize="12sp"
android:gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.1">
<TextView
android:id="@+id/amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="{{amount}}"
android:textSize="18sp"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
的ListView:
<ListView
android:id="@+id/charges"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none"/>
答案 0 :(得分:0)
试试这段代码。它在我的应用程序中正常工作。
PropertyInfo prop;
// init prop, etc...
var value = prop.GetValue(null);
switch (value)
{
case string s:
// ...
break;
case int i:
// ...
break;
case DateTime d:
// ...
break;
default:
// ...
break;
}
还有另一件事可能导致问题,请看一下这个问题。在滚动之前,请确保您的屏幕至少完全显示列表视图的一个项目。否则,您的最后一个列表项的某些部分将被隐藏。如果是这种情况,则必须在&#34; ListViewHelper&#34;中为总高度添加静态值。类。让我们说:
switch(shape)
{
case Circle c:
WriteLine($"circle with radius {c.Radius}");
break;
case Rectangle s when (s.Length == s.Height):
WriteLine($"{s.Length} x {s.Height} square");
break;
case Rectangle r:
WriteLine($"{r.Length} x {r.Height} rectangle");
break;
default:
WriteLine("<unknown shape>");
break;
case null:
throw new ArgumentNullException(nameof(shape));
}
希望这会有所帮助!