0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android nestされたfragmentから、fragmentへ遷移する際にはまった

Last updated at Posted at 2019-09-13

spinnerなどで項目を切り替えた際に、タブの数を動的に変える処理をViewPagerを使ってやってみたがハマったのでメモ

タブでViewPagerを切り替えるレイアウト

view_pager.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#eff1f4"
        android:orientation="horizontal">
        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

ViewPagerにセットするレイアウト(RecyclerViewを渡すだけ)

fragment_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"/>
</LinearLayout>

タブとviewpagerのレイアウト

main_fragment.kt
class tabFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.view_pager, container, false)
    }

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

        //spinnerに設置する処理
        spinner.adapter = spinnerAdapter
        //spinnerの値に応じたタブリストを用意する
        val tabList = ....
        // viewpager用のFragmentPagerAdapter *childFragmentManagerを渡すのが肝
        val tabAdapter = tabsPagerAdapter(childFragmentManager, context, tabList.size)
        view_pager.adapter = tabAdapter
}
TabsPagerAdapter.kt
class TabsPagerAdapter(fm: FragmentManager, context: Context, page:Int) : FragmentPagerAdapter(fm) {
    //タブの数
    val size = page
    private var selectedSpinnerId:Int = 0
    override fun getItem(position: Int): Fragment {
        val fragment = RecyclerViewFragment.newInstance(position)

        return fragment
    }

    // 表示されるタブの数
    override fun getCount(): Int {
        return page
    }

     fun addAll(tabListMap:MutableList<MutableMap<Int,String>>,spinnerId:Int) {
         tabList.addAll(tabListMap)
         selectedSpinnerId = spinnerId
    }
}
RecyclerViewFragment.kt
class AdSearchResultFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        return inflater.inflate(R.layout.fragment_list, container, false)
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    
        // APIなどから取ってきたデータをRecyclerViewに設置する処理(省略)
    }
    companion object {
        fun newInstance(position: Int): RecyclerViewFragment {
            val recyclerViewFragment = RecyclerViewFragment()

            return recyclerViewFragment
        }
    }
}
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?