How to do auto smooth scroll like end credit of movie?

19 Views Asked by At

Problem
I write these scripts to do that. But Auto Smooth Scroll is not working even if I tap the fragment.

Scripts

Detail1Fragment.kt

package com.example.guitercodeapp3.ui.dashboard

import android.content.Intent
import android.graphics.Typeface
import android.os.Bundle
import android.os.Handler
import android.text.Spannable
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.method.ScrollingMovementMethod
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ScrollView
import android.widget.TextView
import androidx.activity.addCallback
import androidx.fragment.app.Fragment
import com.example.guitercodeapp3.R

class Detail1Fragment : Fragment() {

    private lateinit var scrollView: ScrollView
    private var isAutoScrolling = false
    private val scrollRunnable: Runnable = object : Runnable {
        override fun run() {
            if (isAutoScrolling) {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN)
                scrollView.postDelayed(this, SCROLL_DELAY_MS)
            }
        }
    }

    companion object {
        const val ARG_SELECTED_ITEM = "selected_item"
        const val ARG_LYRICS = "lyrics"

        @JvmStatic
        fun newInstance(selectedItem: String, lyrics: String): Detail1Fragment {
            val fragment = Detail1Fragment()
            val args = Bundle()
            args.putString(ARG_SELECTED_ITEM, selectedItem)
            args.putString(ARG_LYRICS, lyrics)
            fragment.arguments = args
            return fragment
        }

        fun String.convertXmlNewlines(): String {
            return this.replace("\n", "\\n")
        }

        private const val SCROLL_DELAY_MS = 1000L
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val rootView = inflater.inflate(R.layout.fragment_detail1, container, false)
        val textViewDetail = rootView.findViewById<TextView>(R.id.textViewDetail)

        arguments?.let {
            val selectedItem = it.getString(ARG_SELECTED_ITEM)
            var lyrics = it.getString(ARG_LYRICS)

            val spannable = SpannableStringBuilder("$selectedItem\n\n$lyrics\n\n\n\n")

            // make title Bold
            val boldSpan = StyleSpan(Typeface.BOLD)
            spannable.setSpan(boldSpan, 0, selectedItem?.length ?: 0, SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)

            // make title lager than default
            val largeTextSpan = RelativeSizeSpan(1.5f) // make title 1.5 times lager than default

            spannable.setSpan(largeTextSpan, 0, selectedItem?.length ?: 0, SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE)

            textViewDetail.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT

            textViewDetail.text = spannable
        }
        return rootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // get ScrollView in the fragment
        scrollView = view.findViewById(R.id.scrollViewDetail1)

        //Set a click listener for a scroll view to toggle automatic scrolling
        scrollView.setOnClickListener{
            toggleAutoScroll()
        }

        //Handling when the smartphone's back button is pressed
        requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner){
            //return to DashboardFragment
            requireFragmentManager().beginTransaction().remove(this@Detail1Fragment).commit()
        }
    }

    private fun toggleAutoScroll(){
        isAutoScrolling = !isAutoScrolling
        if(isAutoScrolling){
            scrollView.post(scrollRunnable)
        }else{
            scrollView.removeCallbacks(scrollRunnable)
        }
    }

}

fragment_detail1.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
    android:background="#000"
    android:clickable="true"
    tools:context=".ui.dashboard.Detail1Fragment"
    android:focusable="true">

    <ScrollView
        android:id="@+id/scrollViewDetail1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textViewDetail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello_blank_fragment" />

    </ScrollView>
</androidx.appcompat.widget.LinearLayoutCompat>

string.xml

<resources>
    <string name="app_name">GuiterCodeApp3</string>
    <string name="title_home">Home</string>
    <string name="title_dashboard">Dashboard</string>
    <string name="title_notifications">Notifications</string>
    <string name="list_animal" />

    <string-array name="list_song">
        <item>歌うたいのバラッド</item> //Title of song
        <item>踊り子</item> //Title of song
        <item>Whatever</item> //Title of song
    </string-array>

    <string-array name="list_lyrics">//these are lyrics of three songs
        <item>嗚呼 唄うことは\n難しいことじゃない\nただ声に身をまかせ\n頭の中をからっぽにするだけ\n\n嗚呼 目を閉じれば\n胸の中に映る\n懐かしい思い出やあなたとの毎日\n\n本当のことは歌の中にある\nいつもなら照れくさくて\n言えないことも\n\n今日だってあなたを思いながら\n歌うたいは唄うよ\nずっと言えなかった言葉がある\n短いから聞いておくれ\n「愛してる」ah…\n\n嗚呼 唄うことは\n難しいことじゃない\nその胸の目隠しをそっと外せばいい\n\n空に浮かんでる言葉をつかんで\nメロディを乗せた雲で\n旅に出かける\n\n情熱の彼方に何がある?\n気になるから行こうよ\n窓の外には北風が\n腕組みするビルの影に吹くけれど\n\nぼくらを乗せて メロディは続く…\nwow wow…\n\n今日だってあなたを思いながら\n歌うたいは唄うよ\nどうやってあなたに伝えよう\n雨の夜も 冬の朝もそばにいて\n\nハッピーエンドの ah\n映画を今イメージして唄うよ\nこんなに素敵な言葉がある\n短いけど聞いておくれよ\nah「愛してる」</item>
        <item>ねぇ、どっかに置いてきたような\n事が一つ二つ浮いているけど\nねぇ、ちゃんと拾っておこう\nはじけて忘れてしまう前に\n\n回り出した あの子と僕の未来が\n止まりどっかで またやり直せたら\n回り出した あの子と僕が被害者\nづらでどっかを また練り歩けたらな\n\nとぅるるる とぅるるる とぅるる\nとぅるるる とぅるるる とぅるる\n\nとぅるるる とぅるるる とぅるる\nとぅるるる とぅるるる とぅるる\n\nあのね、私あなたに会ったの\n夢の中に置いてきたけどね\nねぇ、どうして私が好きなの\n一度しか会ったことがないのにね\n\n思いを蹴って\n二人でしてんだ\n壊(わす)れない愛を歌う\n言葉を二人に課して\n誓いをたてんだ\n忘れない愛を歌うようにね\n\n回り出した あの子と僕の未来が\n止まりどっかで またやり直せたら\n回り出した あの子と僕が被害者\nづらでどっかを また練り歩けたらな\n\nとぅるるる とぅるるる とぅるる\nとぅるるる とぅるるる とぅるる\n\nとぅるるる とぅるるる とぅるる\nとぅるるる とぅるるる とぅるる\n\nとぅるるる とぅるるる とぅるる\nとぅるるる とぅるるる とぅるる\n\nとぅるるる とぅるるる とぅるる\nとぅるるる とぅるるる とぅるる\n\n回り出した あの子と僕の未来が\n止まりどっかで またやり直せたら\n回り出した あの子と僕が被害者\nづらでどっかを また練り歩けたらな\n\n時代に乗って僕たちは\n変わらず愛に生きるだろう\n僕らが散って残るのは\n変わらぬ愛の歌なんだろうな</item>
        <item>Lyrics of Whatever</item>
    </string-array>

    <!-- TODO: Remove or change this placeholder text -->
    <string name="hello_blank_fragment">Hello blank fragment</string>

</resources>

What I want to do

I would like to work the app like following explain.

First, Auto Smooth Scroll start if the fragment is tapped.

Next, Auto Smooth Scroll stop if the fragment is tapped next time.

0

There are 0 best solutions below