Java RecyclerView项不环绕ListView的高度
我有一个活动,看起来像这样:Java RecyclerView项不环绕ListView的高度,java,android,xml,listview,android-recyclerview,Java,Android,Xml,Listview,Android Recyclerview,我有一个活动,看起来像这样: <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_p
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jonas.trainingslog1.WorkoutDataActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
...
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/layout">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<ListView
android:id="@+id/lst"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="30dp"
app:layout_constraintTop_toBottomOf="@+id/lst"/>
将此添加到listview…动态设置listview的高度
public static void setListViewHeightBasedOnChildren(final ListView listView) {
listView.post(new Runnable() {
@Override
public void run() {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int listWidth = listView.getMeasuredWidth();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(
View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += listItem.getMeasuredHeight();
Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
}
Log.d("totalHeight " + totalHeight, "********");
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
listView.setLayoutParams(params);
listView.requestLayout();
}
});
}
public静态无效setListViewHeightBasedOnChildren(最终ListView ListView){
post(新的Runnable(){
@凌驾
公开募捐{
ListAdapter ListAdapter=listView.getAdapter();
如果(listAdapter==null){
返回;
}
int totalHeight=listView.getPaddingTop()+listView.getPaddingBottom();
int listWidth=listView.getMeasuredWidth();
对于(int i=0;i
将此添加到您的recyclerview适配器一个选项是使用垂直线性布局,在其中放置页面的所有内容。然后将视图的高度(包括recyclerview)设置为0dp,并添加布局权重。并非所有英雄都穿斗篷!谢谢,这很有效。