[英]How to make listview expandable inside a scrollview
由于我是 android 编程的新手,因此我面临一个问题,如下所述:
我在ScrollView
有一个ListView
,但它没有扩展(例如:如果我有 3 个数据,则 ListView 只显示 1 个数据)我需要使ListView
扩展。
这是我的 xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/svprofile"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:id="@+id/linLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageToUpload"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_menu_camera"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Profile Detail"
android:id="@+id/tvProfileDetail"
android:layout_marginTop="30dp" />
<EditText
android:id="@+id/etFullname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/Fullname"
android:editable="false"/>
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/Email"/>
<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:hint="@string/Phone"/>
<EditText
android:id="@+id/etAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="1"
android:maxLines="3"
android:hint="@string/Address"/>
<EditText
android:id="@+id/etDirectSuperiorName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/DirectSuperiorName"
android:editable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Leave Balance"
android:id="@+id/tvSisaCuti"
android:layout_marginTop="20dp" />
<EditText
android:id="@+id/etSisaCuti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Sisa Cuti"
android:layout_marginTop="1dp"
android:editable="false"
android:paddingTop="10dp" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lvProfile"
android:choiceMode="singleChoice"
android:headerDividersEnabled="false"
android:divider="@null"
android:dividerHeight="0dp"/>
</LinearLayout>
</ScrollView>
设计
任何帮助将不胜感激。 谢谢伙计们
您可以使用 ExpandableListView 而不是使用列表视图。
<ExpandableListView
android:id="@+id/activity_expandable_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"/>
有关更多详细信息,请参阅此内容。
您正在设置android:layout_height="wrap_content"
这就是为什么它只显示一行的原因。 wrap_content
将 listView 调整为所有项目不起作用。
<ListView
android:layout_width="match_parent"
android:layout_height="500dp"
android:id="@+id/lvProfile"
android:choiceMode="singleChoice"
android:headerDividersEnabled="false"
android:divider="@null"
android:dividerHeight="0dp"/>
在另一个Scrolling
UI 组件中使用一个Scrolling
UI 组件不是一个好习惯。
你应该改变你的实现逻辑。
如果您在 ScrollView 中使用列表视图,请遵循以下代码它将帮助您解决问题:
对于exe
listview.setAdapter(youradapter);
setListViewHeightBasedOnChildren(youradapter)
public void setCustomListViewHeightBasedOnChildren(ListView listView, YourAdapter yourAdapter) {
if (yourAdapter == null) {
return;
}
int totalHeight;
int listWidth = listView.getMeasuredWidth();
int items = yourAdapter.getCount();
if (items == 0) {
totalHeight = 0;
} else {
View childView = yourAdapter.getView(0, null, listView);
childView.measure(MeasureSpec.makeMeasureSpec(listWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
totalHeight = childView.getMeasuredHeight() * items;
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight;
listView.setLayoutParams(params);
listView.requestLayout();
}
你可以这样做:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_expandable_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<!-- your code --->
<ExpandableListView
android:id="@+id/activity_expandable_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"/>
</LinearLayout>
</ScrollView>
那么你的活动或片段中需要一个函数:
private fun setListViewHeight(
listView: ExpandableListView,
group: Int
) {
val listAdapter: ExpandableListAdapter =
listView.expandableListAdapter as ExpandableListAdapter
var totalHeight = 0
val desiredWidth: Int = View.MeasureSpec.makeMeasureSpec(
listView.getWidth(),
View.MeasureSpec.EXACTLY
)
for (i in 0 until listAdapter.groupCount) {
val groupItem: View = listAdapter.getGroupView(i, false, null, listView)
groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED)
totalHeight += groupItem.measuredHeight
if (listView.isGroupExpanded(i) && i != group
|| !listView.isGroupExpanded(i) && i == group
) {
for (j in 0 until listAdapter.getChildrenCount(i)) {
val listItem: View = listAdapter.getChildView(
i, j, false, null,
listView
)
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED)
totalHeight += listItem.measuredHeight
}
}
}
val params: ViewGroup.LayoutParams = listView.layoutParams
var height: Int = (totalHeight
+ listView.dividerHeight * (listAdapter.groupCount - 1))
if (height < 10) height = 200
params.height = height
listView.layoutParams = params
listView.requestLayout()
}
你可以查看这篇文章和你github中的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.