【问题标题】:Why is RecyclerView in scrollView is invisible?为什么scrollView中的RecyclerView是不可见的?
【发布时间】:2021-05-06 11:03:01
【问题描述】:

我有一个 kotlin 应用程序。我不明白为什么我的 recyclerView 是不可见的。当我将 ScrollView 更改为 ConstraintLayout 时,一切正常,但它不可滚动。我想让我的片段可滚动。我试过 NestedScrollView。我没有任何想法。这是我的滚动视图:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    tools:context=".ui.fragments.ChannelFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/channelBlock"
            android:layout_width="match_parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_margin="30dp"
            android:background="@drawable/main_background"
            android:padding="20dp"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/ivChannelImage"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:src="@drawable/ic_person_flat"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/channelName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Qewbite"
                android:textColor="@color/whiteColor"
                android:layout_marginTop="8dp"
                android:textSize="26sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/ivChannelImage" />

            <LinearLayout
                android:id="@+id/subscriptionBlock"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/channelName"
                android:gravity="center_vertical">

                <Button
                    android:id="@+id/subscribeButton"
                    android:layout_width="wrap_content"
                    android:textAllCaps="false"
                    android:layout_height="40dp"
                    android:textSize="12dp"
                    android:gravity="center"
                    android:background="@drawable/subscribed_background"
                    android:padding="0dp"
                    app:backgroundTint="@color/colorDark"
                    android:textColor="@color/whiteColor"
                    android:layout_marginLeft="5dp"
                    android:text="Unfollow" />

                <ImageButton
                    android:id="@+id/notificationButton"
                    android:layout_width="40dp"
                    android:textAllCaps="false"
                    android:layout_height="40dp"
                    android:textSize="12dp"
                    android:tint="@color/whiteColor"
                    android:src="@drawable/ic_notification"
                    app:backgroundTint="@color/colorDark"
                    android:background="@drawable/subscribe_notification_background"
                    android:padding="0dp"
                    android:textColor="@color/whiteColor" />
            </LinearLayout>

            <View
                android:id="@+id/headerHr"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/colorNavbarGray"
                app:layout_constraintTop_toBottomOf="@id/subscriptionBlock"
                android:layout_marginTop="10dp"
                />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/channelSubscribers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10M"
                android:textColor="@color/whiteColor"
                android:layout_marginTop="8dp"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="@id/channelSubscribersTitle"
                app:layout_constraintEnd_toEndOf="@id/channelSubscribersTitle"
                app:layout_constraintTop_toBottomOf="@id/headerHr" />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/channelSubscribersTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Followers"
                android:textColor="@color/colorLightGray"
                android:textSize="12sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/channelSubscribers" />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/channelListened"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="120M"
                android:textColor="@color/whiteColor"
                android:layout_marginTop="8dp"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="@id/channelListenedTitle"
                app:layout_constraintEnd_toEndOf="@id/channelListenedTitle"
                app:layout_constraintTop_toBottomOf="@id/headerHr" />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/channelListenedTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Auditions"
                android:textColor="@color/colorLightGray"
                android:textSize="12sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/channelListened" />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/channelYear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2021"
                android:textColor="@color/whiteColor"
                android:layout_marginTop="8dp"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/headerHr" />

            <com.google.android.material.textview.MaterialTextView
                android:id="@+id/channelYearTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Year"
                android:textColor="@color/colorLightGray"
                android:textSize="12sp"
                app:layout_constraintStart_toStartOf="@id/channelYear"
                app:layout_constraintEnd_toEndOf="@id/channelYear"
                app:layout_constraintTop_toBottomOf="@id/channelSubscribers" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabSelectedTextColor="@color/colorPrimary"
            app:tabRippleColor="@color/colorGray"
            android:layout_marginTop="8dp" />

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/channelViewPager"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"/>

    </LinearLayout>
</ScrollView>

这是我在 ViewPager 中的片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.fragments.ChannelAudioFragment">

    <ProgressBar
        android:id="@+id/allChannelAudiosProgressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/noAudiosTitle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:gravity="center|left"
        android:text="No audios"
        android:layout_marginTop="5dp"
        android:textSize="15dp"/>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/channelAudiosRefreshSwipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvAllChannelAudios"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</LinearLayout>

为什么会这样?

【问题讨论】:

标签: java android kotlin


【解决方案1】:

我遇到了同样的问题。 我还将 recyclerview 放入 swiperefreshlayout 中,但它没有显示任何内容。 所以我给了recyclerview height一些值,然后它就显示出来了。 不是我想要的,但它有效

【讨论】:

    猜你喜欢
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    相关资源
    最近更新 更多