繁体   English   中英

使用协调器布局时扩展 TextView 的库不起作用

[英]The library to expand TextView doesn't work while using Coordinator Layout

我正在使用“it.mike5v:viewmore-textview:1.1.3”库。 当我第一次输入时,它显示阅读更多文本,但是当我 go 返回(后退按钮)并再次输入时,阅读更多文本消失了。 我在协调器布局中有 textview。 我认为这就是问题所在。 我已经尝试了很多其他库,但这个是最好的。 这是一些代码

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:background="@color/black">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black"
            android:fitsSystemWindows="true"
            app:liftOnScroll="true"
            app:elevation="0dp">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipChildren="false"
                android:clipToPadding="false"
                app:contentScrim="@color/black"
                app:statusBarScrim="@color/black"
                app:scrimAnimationDuration="300"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:id="@+id/masterclass_cover_image"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:fitsSystemWindows="false"
                    android:clipToOutline="false"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    android:background="@color/lighter_background"
                    app:layout_collapseMode="parallax"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:src="@drawable/profile_background" />

                <com.google.android.material.appbar.MaterialToolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?android:actionBarSize"
                    android:elevation="0dp"
                    app:contentInsetLeft="0dp"
                    app:contentInsetStart="0dp"
                    app:contentInsetStartWithNavigation="0dp"
                    app:layout_collapseMode="pin"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_scrollFlags="scroll|enterAlways|snap">

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

                            <ImageButton
                                android:id="@+id/back_button"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:layout_gravity="center"
                                android:background="@null"
                                android:scaleType="centerInside"
                                android:src="@drawable/ic_backbutton_with_arrow"
                                app:layout_constraintBottom_toBottomOf="parent"
                                app:layout_constraintStart_toStartOf="parent"
                                app:layout_constraintTop_toTopOf="parent" />
                      
                    </LinearLayout>
                </com.google.android.material.appbar.MaterialToolbar>
            </com.google.android.material.appbar.CollapsingToolbarLayout>
        </com.google.android.material.appbar.AppBarLayout>

        <androidx.core.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/black"
            android:overScrollMode="never"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingHorizontal="16dp"
                android:clipChildren="false"
                android:orientation="vertical">

                <it.mike5v.viewmoretextview.ViewMoreTextView
                    android:id="@+id/masterclass_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/text_light_color"
                    android:layout_marginTop="8dp"
                    app:ellipsizeText="@string/read_more"
                    app:ellipsizeTextColor="@color/white"
                    app:duration="100"
                    app:visibleLines="10" />

            </LinearLayout>
        </androidx.core.widget.NestedScrollView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

在我的片段中


        binding.masterclassDescription.setOnClickListener {
            binding.masterclassDescription.toggle()
}

我对该库一无所知(如果您对其功能有特别的疑问,请联系作者/向其存储库的跟踪器添加问题)但听起来您想存储一些“打开的”state,即使用户退出片段?

重置 state 是正常行为,您已经离开屏幕,所以您会重新回来 - 如果您想覆盖它,您需要存储当前的 state ( 例如在SharedPreferences)并在您阅读时阅读它重新设置片段。 例如:

companion object {
    const val KEY_READ_MORE_EXPANDED = "read more expanded"
}

// keep a convenient reference to this, initialise it in one of the setup lifecycle callbacks
lateinit var prefs: SharedPreferences

// wherever you're doing your view setup
prefs = requireActivity().getPreferences(Context.MODE_PRIVATE)
// initialise the open state based on whatever state is stored
// I have no idea what the property you need to set is called, I just used 'expanded'
binding.masterclassDescription.expanded = prefs.getBoolean(KEY_READ_MORE_EXPANDED, false)

binding.masterclassDescription.setOnClickListener {
    binding.masterclassDescription.toggle()
    // store the state - again I don't know what the real property you need is called
    // You could also do this in onPause, or onStop, or onSaveInstanceState instead
    val isExpanded = binding.masterclassDescription.expanded
    with(prefs.edit()) {
        putBoolean(KEY_READ_MORE_EXPANDED, isExpanded)
        apply()
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM