Fragment的使用一般可以分为两种场景:一、Fragment与ViewPager连用,二、Fragment单独使用。
一、Fragment与ViewPager连用
1、在activity的xml里添加ViewPager控件
2、新建继承自Fragment(v4包)的类,可起名为Fragment1(可在包上右键new -- fragment -- blank fragment),类似的可新建多个(与viewPager连用)
class Fragment1 : Fragment() { private var param1: String? = null private var param2: String? = null private var listener: OnFragmentInteractionListener? = null override 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_fragment1, container, false) } fun onButtonPressed(uri: Uri) { listener?.onFragmentInteraction(uri) } override fun onAttach(context: Context) { super.onAttach(context) if (context is OnFragmentInteractionListener) { listener = context } else { throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener") } } override fun onDetach() { super.onDetach() listener = null } //需要注意的是这个接口,activity必须实现,这个可以实现fragment向activity发消息 interface OnFragmentInteractionListener { fun onFragmentInteraction(uri: Uri)//参数可以是其他类型 } companion object { private const val ARG_PARAM1 = "param1" private const val ARG_PARAM2 = "param2" @JvmStatic fun newInstance(param1: String, param2: String) = Fragment1().apply { arguments = Bundle().apply { putString(ARG_PARAM1, param1) putString(ARG_PARAM2, param2) } } } }
3、新建继承自FragmentAdapter的类,可,命名为MyFragmentAdapter
class MyFragmentAdapter(fm: FragmentManager, fragments: MutableList) : FragmentPagerAdapter(fm) { private var fragmentList: MutableList = fragments override fun getItem(position: Int): Fragment { return fragmentList[position] } override fun getCount(): Int { return fragmentList.size } }
4、准备工作准备好以后,开始添加Activity里的代码。注意要继承FragmentActivity类。
class MyFragmentActivity : FragmentActivity() , Fragment1.OnFragmentInteractionListener , Fragment2.OnFragmentInteractionListener { override fun onFragmentInteraction222(string: String) { Log.d("baibai", "onFragmentInteraction222") Log.d("baibai", string) } override fun onFragmentInteraction(uri: Uri) { } private lateinit var mFragments: MutableListprivate lateinit var mFragment1: Fragment1 private lateinit var mFragment2: Fragment2 private lateinit var mFragmentAdapter: MyFragmentAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_my_fragment) initFragment() initListener() } private fun initFragment() { mFragment1 = Fragment1.newInstance("bai", "yun") mFragment2 = Fragment2.newInstance("111", "222") mFragments = mutableListOf() mFragments.add(mFragment1) mFragments.add(mFragment2) } private fun initListener() { mFragmentAdapter = MyFragmentAdapter(this.supportFragmentManager, mFragments) vp_fragment.adapter = mFragmentAdapter vp_fragment.setOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrollStateChanged(state: Int) { } override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { //滑动的过程中 } override fun onPageSelected(position: Int) { //viewPager确定某一页选中后 } }) } }