说明:kotlin + tablayout + viewpager + adapter实现滑动界面
效果图
step1:
package com.example.flushfragmentdemoimport androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)val fragments = ArrayList<Fragment>()fragments.add(FooFragment())fragments.add(PlayerFragment(""))mainViewPager.apply {adapter = object :FragmentStateAdapter(this@MainActivity){override fun getItemCount(): Int {return 2}override fun createFragment(position: Int): Fragment {return fragments[position]}}setCurrentItem(1,false)}TabLayoutMediator(tabLayout,mainViewPager){ tab: TabLayout.Tab, i: Int ->when (i) {0 -> tab.text = "fpp"1 -> tab.text = "player"}}.attach()}
}
step2:
package com.example.flushfragmentdemoimport android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroupprivate const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"class FooFragment : Fragment() {private var param1: String? = nullprivate var param2: String? = nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)arguments?.let {param1 = it.getString(ARG_PARAM1)param2 = it.getString(ARG_PARAM2)}}override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {return inflater.inflate(R.layout.fragment_foo, container, false)}companion object {@JvmStaticfun newInstance(param1: String, param2: String) =FooFragment().apply {arguments = Bundle().apply {putString(ARG_PARAM1, param1)putString(ARG_PARAM2, param2)}}}
}
step3:
package com.example.flushfragmentdemoimport android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflaterimport android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_player.*class PlayerFragment(private val url:String) : Fragment() {override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {// Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_player, container, false)}override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState)}override fun onResume() {super.onResume()}override fun onPause() {super.onPause()}
}
step4:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><com.google.android.material.appbar.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"><com.google.android.material.tabs.TabLayoutandroid:id="@+id/tabLayout"android:layout_width="match_parent"android:layout_height="wrap_content"app:tabIndicatorColor="@android:color/white" /></com.google.android.material.appbar.AppBarLayout><androidx.viewpager2.widget.ViewPager2android:id="@+id/mainViewPager"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_anchorGravity="bottom" /></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>
step5:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="@android:color/holo_green_dark"tools:context=".FooFragment"><!-- TODO: Update blank fragment layout --><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:background="@android:color/holo_red_dark"android:text="fpp"android:textSize="30sp" /></FrameLayout>
step6:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="@android:color/holo_orange_dark"tools:context=".FooFragment"><!-- TODO: Update blank fragment layout --><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="player"android:textSize="30sp" /></FrameLayout>
end