【問題】
Tablayoutをnestした際、nestされた側のtabが表示されない
【現状】
上下にtabがあるレイアウトを実現したく、tablayoutとviewpagerを使用しております。
その際、nestされた側のtab(画面の上側)がツールバーと被っており、うまく表示されません。
ググってみたのですが、なかなかそのような現象を記載しているものはなかったので、ここで質問させていただいております、、、
【コード】
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.design.widget.TabLayout android:id="@+id/tabTab01" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:tabGravity="fill" app:tabIndicatorColor="@android:color/holo_blue_light" app:tabMaxWidth="0dp" app:tabMode="fixed" app:tabTextAppearance="@style/TextAppearance.AppCompat.Medium" /> <android.support.v4.view.ViewPager android:id="@+id/styleviewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" app:layout_constraintBottom_toTopOf="@+id/tabTab01" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" /> </android.support.constraint.ConstraintLayout>
Mainactivity.kt
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //ViewPagerに先ほど作成したAdapterのインスタンスを渡してあげる styleviewpager.adapter = FGadapter(supportFragmentManager, this) //TabLayoutにViewPagerのインスタンスを渡すと自動的に実装してくれる tabTab01.setupWithViewPager(styleviewpager) //Tabへの処理はsetupWithViewPagerをした後だったら可能 //ここではタブ名とアイコンを設定している val tab1 = tabTab01.getTabAt(0) tab1?.text = "追加" val tab2 = tabTab01.getTabAt(1) tab2?.text = "一覧" } }
basefragment1.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.TabLayout android:id="@+id/childTab" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:tabGravity="fill" app:tabIndicatorColor="@android:color/holo_blue_light" app:tabMaxWidth="0dp" app:tabMode="fixed" /> <android.support.v4.view.ViewPager android:id="@+id/childviewpager" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/childTab" /> </android.support.constraint.ConstraintLayout>
FGadapter.kt
class FGadapter(fm: FragmentManager, context: Context) : FragmentPagerAdapter(fm) { override fun getItem(position: Int): Fragment { return when (position) { 0 -> { basefragment1() } 1 -> { basefragment2() } else -> { basefragment1() } } } override fun getCount(): Int { return 2 } }
basefragment1.kt
class basefragment1: Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.basefragment1, null) } override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) childviewpager.adapter = CHFGadapter(childFragmentManager, this) childTab.setupWithViewPager(childviewpager) val childtab1 = childTab.getTabAt(0) childtab1?.text = "A" val childtab2 = childTab.getTabAt(1) childtab2?.text = "B" } }
暫定的に上側からいくらか距離を指定することで、ツールバーとtabが被るのは防ぐことはできるのですが、
非常に気持ち悪い状態で、かつなかなか解決できておりません。
よろしければ、アドバイスいただければ幸いです。
何卒よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。