为什么我不能使用PreferenceFragment()的实例而不是Fragment()的实例?

时间:2018-06-15 07:15:46

标签: android kotlin

我看过艺术https://developer.android.com/reference/kotlin/android/preference/PreferenceFragment

我认为PreferenceFragment是Fragment的子类

所以我认为我总是可以使用PreferenceFragment()而不是Fragment()。

但是在代码A中,当我尝试使用return MyPreferenceFragment()而不是返回PlaceholderFragment.newInstance(position + 1)时出现错误,为什么?

代码A

class UITab : AppCompatActivity() {
    private var mSectionsPagerAdapter: SectionsPagerAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_tab)

        mSectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager)

        container.adapter = mSectionsPagerAdapter

        container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
        tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))
    }


    inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            //return MyPreferenceFragment()   //It will cause error
            return PlaceholderFragment.newInstance(position + 1)
        }

        override fun getCount(): Int {
            return 2
        }
    }



    class PlaceholderFragment : Fragment() {

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                                  savedInstanceState: Bundle?): View? {
            val rootView = inflater.inflate(R.layout.layout_fragment, container, false)
            rootView.section_label.text =arguments?.getInt(ARG_SECTION_NUMBER)?.toString()

            return rootView
        }

        companion object {
            private val ARG_SECTION_NUMBER = "section_number"
            fun newInstance(sectionNumber: Int): PlaceholderFragment {
                val fragment = PlaceholderFragment()
                val args = Bundle()
                args.putInt(ARG_SECTION_NUMBER, sectionNumber)
                fragment.arguments = args
                return fragment
            }
        }
    }


    class MyPreferenceFragment : PreferenceFragment() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            addPreferencesFromResource(R.xml.mypreference)
        }
    }

}

致Mike M 谢谢!以下代码是我的导入

import android.support.design.widget.TabLayout
import android.support.v7.app.AppCompatActivity
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.os.Bundle
import android.preference.*
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import info.dodata.mirror.R
import kotlinx.android.synthetic.main.layout_fragment.view.*
import kotlinx.android.synthetic.main.layout_tab.*

1 个答案:

答案 0 :(得分:2)

FragmentPagerAdapter是支持库的一部分,因此使用android.support.v4.app.FragmentPreferenceFragment是一个继承自android.app.Fragment

的框架片段

您应该使用PreferenceFragmentCompat兼容性类来代替PreferenceFragment