LoginSignup
0
1

More than 1 year has passed since last update.

[Android] [Kotlin] TabLayoutと(Horizontal)RecyclerViewを仲良く共存させる方法

Last updated at Posted at 2023-02-15

06b26b96-6dd5-4cbd-af52-9db9c5550397.png

実現したこと

TabLayoutでの画面切り替えとHorizontalなRecyclerViewでのリストスクロールの両方を実装したいけど、
スクロールしようとすると画面が切り替わったりして、うまくスクロールできない!仲良くして!!
ってなったので、それを解決するべく方法を見つけましたので、共有します。

レイアウトも見たい方は、
https://qiita.com/shinmai333/items/8bdd5a72c0f86348bd02
こちらから見れます。
全く一緒のレイアウトで、Fragmentの中身にHorizontalなRecyclerViewを乗っけただけです。
ソースコードやレイアウトはまあまあ適当に作ってありますので、ご了承くださいませ。

ソースコード

MainActivity.kt
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val viewPager : ViewPager2 = findViewById(R.id.viewPager)
        val tabLayout : TabLayout = findViewById(R.id.tablayout)

        val adapter = ViewPagerAdapter(supportFragmentManager, lifecycle)
        viewPager.adapter = adapter
        TabLayoutMediator(tabLayout, viewPager) { tab, position ->
            val relativeLayout = LayoutInflater.from(this)
                .inflate(R.layout.tab_item, tabLayout, false) as RelativeLayout
            val tabTextView : TextView = relativeLayout.findViewById(R.id.tab_title)
            tabTextView.text = adapter.getTabTitle(position)
            tab.customView = relativeLayout
        }.attach()
    }
}
SampleFragment.kt
class SampleFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_sample, container, false)
        setView(view)
        return view
    }

    private fun setView(view: View) {
        val parent : MainActivity = activity as MainActivity
        val rv : RecyclerView = view.findViewById(R.id.rv)
        val adapter = RecyclerAdapterList()
        rv.layoutManager = GridLayoutManager(parent, 2, RecyclerView.HORIZONTAL,false)
        rv.adapter = adapter

        // これを書くことでTabLayoutとRecyclerViewが仲良しになります
        rv.addOnItemTouchListener(object : OnItemTouchListener {
            override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
                val action = e.action
                when (action) {
                    MotionEvent.ACTION_MOVE -> rv.parent.requestDisallowInterceptTouchEvent(true)
                }
            return false
            }
            override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
            override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
        })
    }
}

最近よくRecyclerViewを扱っているのですが、たくさん問題点が出てきて苦戦してます。
もし役に立てそうな実装を見つけたら、また投稿したいと思います。
ありがとうございました。

0
1
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
1