いろいろやる課、書く係

いろいろなことを、たんたんと記録するブログ

KotlinでRecyclerViewをスクロールさせたときにtoolbarが隠れるようにしておくと、Fragment遷移後でも消えたままになるので、強制表示させる

まぁ、スクロールしても隠れないようにしておいてもいいんだけれど、スクロールするくらい量があるんだから、ツールバーかくしてデータの表示領域広くしてあげたほうがよいかなって思いまして。

なお参考にしたのはこちらの記事。

medium.com

 

以下、手順。

1.Basic Activityテンプレートで新プロジェクト作成

※ビルドでエラーが出たらこちら参照

sakimika.hateblo.jp

 

2.Floating Buttonは要らないので関係個所削除

わかんなければ先に進んで、赤線出た箇所を削除

 

3.こちらの記事を参考に、FirstFragmentにRecyclerView追加

hirauchi-genta.com

ただし、MainActivity.ktはFirstFragment.ktに読み替え。(#OnCreateも#OnViewCreatedに読み替え。)

activity_main.xmlも、fragment_first.xmlに読み替え。

 

4.RecyclerViewの中身を追加(FirstFragment.kt)

画像は適当にします(res > drawble > 右クリック> New > Vector Asset で適当なのを追加して、全部それにするとか)

スクロールが必要なほどの行数が欲しいので、美しくないけどささっとこんな感じにした

mAnimalList = arrayListOf(dog, cat, elephant, horse, lion)

mAnimalList = arrayListOf(dog, cat, elephant, horse, lion, dog, cat, elephant, horse, lion)

 

5.スクロールでtoolbarが隠れるようにする(activity_main.xml

赤字部分を追加

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/Theme.MyApplication.PopupOverlay" />

 

6.行クリックでSecondFragmentに遷移するようにする

CustomAdapter.ktに以下を追加

    private inner class ItemClickListener : View.OnClickListener {
        override fun onClick(view: View) {
            
            Navigation.findNavController(view)
                .navigate(R.id.action_FirstFragment_to_SecondFragment)
        }
    }

#onCreateViewHolderに赤字部分追加

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context).inflate(R.layout.list_item, viewGroup, false)
        view.setOnClickListener(ItemClickListener())
        return ViewHolder(view)
    }

 

7.FirstFragmentでtoolbarが消えるまでスクロールし、行クリックで画面遷移すると、toolbarが消えたままであることを確認

 

8.AppBarLayoutにid追加(activity_main.xml

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/appBar"
        android:theme="@style/Theme.MyApplication.AppBarOverlay">

 

9.MainActivity.kt#onCreateに以下を追加

        val navController = findNavController(R.id.nav_host_fragment)
        navController.addOnDestinationChangedListener { _, destination, _ ->
            findViewById<AppBarLayout>(R.id.appBar)?.apply {
                setExpanded(true, false)
            }
        }

 

10.FirstFragmentでtoolbarが消えるまでスクロールし、行クリックで画面遷移すると、toolbarが強制的に表示されることを確認

9をすると、Navigationを使ったすべての遷移についてそうなるけど、Fragment個別にしたい場合は

        navController.addOnDestinationChangedListener { _, destination, _ ->
            when (destination.id)
            {
                R.id.SecondFragment ->
                    findViewById<AppBarLayout>(R.id.appBar)?.apply {
                        setExpanded(true, false)
                    }
            }
        }

とするか

そのFragmentのonViewCreatedで

        (activity as AppCompatActivity).findViewById<AppBarLayout>(R.id.appBar)?.apply {
            setExpanded(true, false)
        }

とする。

 

なお、Fragment使ってるバージョンでお届けしましたが、Fragment使ってないぜ!って場合は、何かしらのactivity#onCreateで

        val appBarLayout : AppBarLayout = findViewById(R.id.appBar)
        appBarLayout.setExpanded(true, false)

こうかな……