[英]Should I use ScrollView or RecyclerView for scrolling in fragment?
I have a question about my Android app.我对我的 Android 应用程序有疑问。 I have a fragment in which I want to scroll.
我有一个片段,我想在其中滚动。 Should I use the ScrollView in the fragment or in the main activity (the fragment is used for navigation in toolbar)?
我应该在片段中还是在主要活动中使用 ScrollView(片段用于在工具栏中导航)?
Also, should I use ScrollView or RecyclerView?另外,我应该使用 ScrollView 还是 RecyclerView? Here is what I tried so far, but it didn't work.
这是我到目前为止尝试过的,但没有奏效。 This is the main activity xml, where I added the ScrollView.
这是主要活动 xml,我在其中添加了 ScrollView。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
android:fillViewport="true"
android:isScrollContainer="false">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottomAppBar" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:backgroundTint="@color/green_700"
app:fabAlignmentMode="center"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="30dp"
app:fabCradleVerticalOffset="10dp"
app:contentInsetStart="0dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:itemIconTint="@color/light_grey"
app:itemTextColor="@color/light_grey"
app:itemTextAppearanceActive="@font/josefin_sans_semibold"
android:background="@drawable/background_transparent"
app:menu="@menu/bottom_navigation_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onPressFab"
android:src="@drawable/ic_add"
app:layout_anchor="@id/bottomAppBar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</ScrollView>
And this is the fragment where I need scrolling.这是我需要滚动的片段。 It is the home fragment from toolbar.
它是工具栏的主页片段。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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"
android:fillViewport="true"
android:isScrollContainer="false">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/myCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
app:layout_constraintTop_toTopOf="parent">
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.card.MaterialCardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="100dp"
app:cardBackgroundColor="@color/green_700"
app:cardElevation="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.428"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MaterialCardView.Cut">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:background="@drawable/search_outline"
android:fontFamily="@font/josefin_sans_semibold"
android:hint="Search Barcode"
android:inputType="text"
android:maxLines="1"
android:paddingLeft="30dp"
android:paddingTop="10dp"
android:paddingRight="30dp"
android:paddingBottom="10dp"
android:textColor="@color/dark_grey"
android:textColorHint="@color/grey_200"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.183"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/sbutton"
android:layout_width="90dp"
android:layout_height="60dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="16dp"
android:background="@drawable/background_transparent"
android:drawableLeft="@drawable/search_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<ImageView
android:id="@+id/imageView4"
android:layout_width="450dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="64dp"
android:layout_marginRight="64dp"
android:src="@drawable/home_image"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="208dp"
android:layout_height="138dp"
android:layout_marginTop="120dp"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
android:gravity="center"
android:text="Welcome back!"
android:textColor="@color/darker_grey"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/myCoordinatorLayout" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_marginTop="340dp"
android:clipToPadding="false"
android:foregroundGravity="center"
android:overScrollMode="never"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/myCoordinatorLayout">
</androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
I would be grateful if you have any suggestions.如果您有任何建议,我将不胜感激。 Thank you!
谢谢!
RecyclerView is used to make lists. RecyclerView 用于制作列表。
Try put yout fragment code inside a ScrollView and remove it from your activity.尝试将您的片段代码放入 ScrollView 并将其从您的活动中删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.