在我的布局中,我有产品列表的回收视图。。下面我在linearlayout中显示总价。。。但在滚动时,linearlayout不显示
我的代码场景:
1->当我在recyclerview上有一个项目时-->linearlayout显示精细
2->当我在recyclerview上有更多项目时-->向下滚动时看不到线性布局(线性布局未显示(
在第二个场景中需要帮助。。。提前感谢
下面是代码xml:--
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relative">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapseIcon="@drawable/ic_arrow_back_black_24dp"
android:id="@+id/toolbartable"
android:background="@color/colorPrimaryDark">
</androidx.appcompat.widget.Toolbar>
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/relative"
android:id="@+id/nest"
android:scrollbars="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_below="@id/relative"
android:layout_height="wrap_content"
android:id="@+id/recyleview"/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/linearlayoutorder"
android:layout_below="@id/nest"
android:orientation="horizontal"
android:weightSum="2"
android:background="@drawable/border"
android:backgroundTintMode="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOTAL"
android:layout_gravity="center"
android:layout_weight="1"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/totalidcost"
android:layout_gravity="right|center"
android:textAlignment="textEnd"
tools:ignore="RtlCompat"></TextView>
</LinearLayout>
</RelativeLayout>
更新代码:--
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relative">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapseIcon="@drawable/ic_arrow_back_black_24dp"
android:id="@+id/toolbartable"
android:background="@color/colorPrimaryDark">
</androidx.appcompat.widget.Toolbar>
</RelativeLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nest"
android:scrollbars="vertical"
android:layout_below="@id/relative">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
android:id="@+id/recyleview"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/linearlayoutorder"
android:orientation="horizontal"
android:weightSum="2"
android:background="@drawable/border"
android:backgroundTintMode="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOTAL"
android:layout_gravity="center"
android:layout_weight="1"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/totalidcost"
android:layout_gravity="right|center"
android:textAlignment="textEnd"
tools:ignore="RtlCompat"></TextView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
获取--:Caused by: android.view.InflateException: Binary XML file line #34: ScrollView can host only one direct child
时出错
`Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child`
需要帮助
以下解决方案对我有效:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relative">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:collapseIcon="@drawable/ic_arrow_back_black_24dp"
android:id="@+id/toolbartable"
android:background="@color/colorPrimaryDark">
</androidx.appcompat.widget.Toolbar>
</RelativeLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nest"
android:scrollbars="vertical"
android:layout_below="@id/relative">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
android:id="@+id/recyleview"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/linearlayoutorder"
android:layout_below="@id/recyleview"
android:orientation="horizontal"
android:weightSum="2"
android:background="@drawable/border"
android:backgroundTintMode="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TOTAL"
android:layout_gravity="center"
android:layout_weight="1"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/totalidcost"
android:layout_gravity="right|center"
android:textAlignment="textEnd"
tools:ignore="RtlCompat"></TextView>
</LinearLayout>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
您尚未添加weightSum和orientation值用于LinearLayout。
源
您可以使用NestedScrollView而不是ScrollView。还可以将RecyclerView和LinearLayout放在NestedScrollView中。并启用android:nestedScrollingEnabled=true
。应该对你有用。
<NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled=true>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyleview"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/linearlayoutorder"
app:layout_constraintTop_toBottomOf="@id/relative" />
<LinearLayout
android:id="@+id/linearlayoutorder"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000"
android:backgroundTintMode="@color/colorPrimary"
android:orientation="horizontal"
android:weightSum="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="TOTAL" />
<TextView
android:id="@+id/totalidcost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_weight="1"
android:textAlignment="textEnd"
tools:ignore="RtlCompat" />
</LinearLayout>
</LinearLayout>
在你的java类中,你有你的recyclerview集recyclerView.setNestedScrollingEnabled(true or false);
像这样使用ConstraintLayout,下面是您需要的完整xml代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbartable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
app:collapseIcon="@drawable/back" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyleview"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/linearlayoutorder"
app:layout_constraintTop_toBottomOf="@id/relative" />
<LinearLayout
android:id="@+id/linearlayoutorder"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000"
android:backgroundTintMode="@color/colorPrimary"
android:orientation="horizontal"
android:weightSum="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="TOTAL" />
<TextView
android:id="@+id/totalidcost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_weight="1"
android:textAlignment="textEnd"
tools:ignore="RtlCompat" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>