#android #carousel #android-motionlayout
Вопрос:
Я пытаюсь реализовать Карусель с TextView
помощью, но у меня странный сбой. При прокрутке влево TextView
последнее слово текста исчезает, неважно, как долго оно длится, последнее слово текста появится/исчезнет при прокрутке и исчезнет, когда прокрутка будет завершена. Если в текстовом представлении есть только одно слово, последний символ исчезает.
Вот пример:
main_fragment.xml
lt;?xml version="1.0" encoding="utf-8"?gt; lt;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:id="@ id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ui.main.MainFragment"gt; lt;androidx.constraintlayout.motion.widget.MotionLayout android:id="@ id/motion_carousel" android:layout_width="0dp" android:layout_height="100dp" app:layoutDescription="@xml/carousel_scene" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:visibility="visible"gt; lt;TextView android:id="@ id/text_title_0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/carousel_names_horizontal_margin" android:background="#000" android:padding="18dp" android:textColor="#fff" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@ id/text_title_1" app:layout_constraintTop_toTopOf="parent" tools:text="Text 0 disappear" /gt; lt;TextView android:id="@ id/text_title_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/carousel_names_horizontal_margin" android:background="#000" android:padding="18dp" android:textColor="#fff" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@ id/text_title_2" app:layout_constraintTop_toTopOf="parent" tools:text="Text 1 disappear" /gt; lt;TextView android:id="@ id/text_title_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#000" android:padding="18dp" android:textColor="#fff" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:text="Text 2 disappear" /gt; lt;TextView android:id="@ id/text_title_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/carousel_names_horizontal_margin" android:background="#000" android:padding="18dp" android:textColor="#fff" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@ id/text_title_2" app:layout_constraintTop_toTopOf="parent" tools:text="Text 3 disappear" /gt; lt;TextView android:id="@ id/text_title_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/carousel_names_horizontal_margin" android:background="#000" android:padding="18dp" android:textColor="#fff" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toEndOf="@ id/text_title_3" app:layout_constraintTop_toTopOf="parent" tools:text="Text 4 disappear" /gt; lt;androidx.constraintlayout.helper.widget.Carousel android:id="@ id/carousel_scene_title" android:layout_width="wrap_content" android:layout_height="wrap_content" app:carousel_backwardTransition="@ id/backward" app:carousel_firstView="@ id/text_title_2" app:carousel_forwardTransition="@ id/forward" app:carousel_nextState="@ id/next" app:carousel_previousState="@ id/previous" app:constraint_referenced_ids="text_title_0,text_title_1,text_title_2,text_title_3,text_title_4" /gt; lt;/androidx.constraintlayout.motion.widget.MotionLayoutgt; lt;/androidx.constraintlayout.widget.ConstraintLayoutgt;
carousel_scene.xml
lt;?xml version="1.0" encoding="utf-8"?gt; lt;MotionScene xmlns:android="http://schemas.android.com/apk/res/android" xmlns:motion="http://schemas.android.com/apk/res-auto" motion:defaultDuration="5000"gt; lt;Transition android:id="@ id/forward" motion:constraintSetEnd="@ id/next" motion:constraintSetStart="@id/start" motion:duration="100"gt; lt;OnSwipe motion:dragDirection="dragLeft" motion:maxAcceleration="20" motion:onTouchUp="decelerateAndComplete" motion:touchAnchorSide="left" /gt; lt;/Transitiongt; lt;Transition android:id="@ id/backward" motion:constraintSetEnd="@ id/previous" motion:constraintSetStart="@ id/start" motion:duration="100"gt; lt;OnSwipe motion:dragDirection="dragRight" motion:maxAcceleration="20" motion:onTouchUp="decelerateAndComplete" motion:touchAnchorSide="right" /gt; lt;/Transitiongt; lt;ConstraintSet android:id="@ id/previous"gt; lt;Constraint android:id="@ id/text_title_0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/carousel_names_horizontal_margin" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintEnd_toStartOf="@id/text_title_1" motion:layout_constraintTop_toTopOf="parent" /gt; lt;Constraint android:id="@ id/text_title_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/carousel_names_horizontal_margin" android:layout_marginEnd="@dimen/carousel_names_horizontal_margin" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintEnd_toEndOf="parent" motion:layout_constraintStart_toStartOf="parent" motion:layout_constraintTop_toTopOf="parent" /gt; lt;Constraint android:id="@ id/text_title_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/carousel_names_horizontal_margin" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintStart_toEndOf="@id/text_title_1" motion:layout_constraintTop_toTopOf="parent" /gt; lt;/ConstraintSetgt; lt;ConstraintSet android:id="@ id/start"gt; lt;Constraint android:id="@ id/text_title_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/carousel_names_horizontal_margin" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintEnd_toStartOf="@id/text_title_2" motion:layout_constraintTop_toTopOf="parent" /gt; lt;Constraint android:id="@ id/text_title_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/carousel_names_horizontal_margin" android:layout_marginEnd="@dimen/carousel_names_horizontal_margin" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintHorizontal_bias="0.5" motion:layout_constraintEnd_toEndOf="parent" motion:layout_constraintStart_toStartOf="parent" motion:layout_constraintTop_toTopOf="parent" /gt; lt;Constraint android:id="@ id/text_title_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/carousel_names_horizontal_margin" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintStart_toEndOf="@id/text_title_2" motion:layout_constraintTop_toTopOf="parent" /gt; lt;/ConstraintSetgt; lt;ConstraintSet android:id="@ id/next"gt; lt;Constraint android:id="@ id/text_title_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/carousel_names_horizontal_margin" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintEnd_toStartOf="@id/text_title_3" motion:layout_constraintTop_toTopOf="parent" /gt; lt;Constraint android:id="@ id/text_title_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/carousel_names_horizontal_margin" android:layout_marginEnd="@dimen/carousel_names_horizontal_margin" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintEnd_toEndOf="parent" motion:layout_constraintStart_toStartOf="parent" motion:layout_constraintTop_toTopOf="parent" /gt; lt;Constraint android:id="@ id/text_title_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/carousel_names_horizontal_margin" motion:layout_constraintBottom_toBottomOf="parent" motion:layout_constraintStart_toEndOf="@id/text_title_3" motion:layout_constraintTop_toTopOf="parent" /gt; lt;/ConstraintSetgt; lt;/MotionScenegt;
Основная часть.кт
class MainFragment : Fragment() { companion object { fun newInstance() = MainFragment() } private lateinit var viewModel: MainViewModel private lateinit var binding: MainFragmentBinding private val nameList = mutableListOflt;Stringgt;() init { for (i in 0..7){ nameList.add("Text ${i} disappear") } } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { binding = MainFragmentBinding.inflate(inflater, container, false) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) binding.carouselSceneTitle.setAdapter(object : Carousel.Adapter { override fun count(): Int { return nameList.size } override fun populate(view: View, index: Int) { (view as TextView).apply { text = nameList[index] } } override fun onNewItem(index: Int) { } }) } }
Ответ №1:
Просто понял, что добавление пустого места после текста устраняет проблему, но это скорее обходной путь, чем правильное решение.
override fun populate(view: View, index: Int) { (view as TextView).apply { text = " " nameList[index] " " } }