以前,我使用TabLayout
和ViewPager1
来显示DialogFragment中的内容,但是现在我想将Spinner
和ViewPager2
一起使用。当我使用与Spinner
相关的代码时,它会导致应用程序崩溃,并且Spinner显示为空白,这使我感到困惑。有谁知道为什么我的微调框不显示任何项目,而ViewPager2
不显示任何Fragments
,以及如何确认呢?
问题似乎出在这部分代码,logcat指向该部分:
val arrayAdapter = ArrayAdapter(view!!.context, R.layout.my_spinnerItem, myTabTitles)
arrayAdapter.setDropDownViewResource(R.layout.my_spinnerItem)
mSpinner.adapter = arrayAdapter
XML(我以前使用过)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/myTabLayout"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:tabMode="scrollable"
app:tabGravity="fill" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/myViewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
XML(我想改用什么)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/mySpinner"
style="@style/Widget.AppCompat.Spinner.Underlined"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dialog" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/myViewPagerV2"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
Kotlin(我以前使用过的东西)
class TabbedDialog(
private val myTitle: String,
private val myTabTitles: ArrayList<String>,
private val myTabDescriptions: ArrayList<String>
) : DialogFragment() {
private lateinit var customView: View
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return customView
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val b = AlertDialog.Builder(activity)
.setTitle(myTitle)
.setPositiveButton(getString(android.R.string.ok)) { dialog, _ -> dialog.dismiss() }
customView = activity!!.layoutInflater.inflate(R.layout.fragment_dialog, null)
val tabLayout = customView.myTabLayout
val viewPager = customView.myViewPager
val adapter = MyViewPagerAdapter(childFragmentManager)
myTabTitles.forEachIndexed { index, item ->
adapter.addFragment(
item,
FragmentDialogContent.createInstance(myTabDescriptions[index])
)
}
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewPager)
b.setView(customView)
return b.create()
}
}
MyViewPagerAdapter类
class MyViewPagerAdapter(manager: FragmentManager) :
FragmentPagerAdapter(manager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
private val titleList: MutableList<String> = ArrayList()
private val fragmentList: MutableList<Fragment> = ArrayList()
override fun getItem(position: Int): Fragment {
return fragmentList[position]
}
override fun getCount(): Int {
return fragmentList.size
}
fun addFragment(title: String, fragment: Fragment) {
titleList.add(title)
fragmentList.add(fragment)
}
override fun getPageTitle(position: Int): CharSequence? {
return titleList[position]
}
}
科特琳(我尝试了什么)
class TabbedDialog1(
private val myTitle: String,
private val myTabTitles: ArrayList<String>,
private val myTabDescriptions: ArrayList<String>
) : DialogFragment() {
private lateinit var customView: View
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return customView
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val b = AlertDialog.Builder(activity)
.setIcon(R.drawable.ic_night)
.setTitle(myTitle)
.setPositiveButton(getString(android.R.string.ok)) { dialog, _ -> dialog.dismiss() }
customView = activity!!.layoutInflater.inflate(R.layout.fragment_dialog_1, null)
val mSpinner = customView.mSpinnerV2
val mViewPager = customView.mViewPagerV2
val mAdapter = MyViewPagerAdapter(childFragmentManager)
val arrayAdapter = ArrayAdapter(view!!.context, R.layout.my_spinnerItem, myTabTitles)
arrayAdapter.setDropDownViewResource(R.layout.my_spinnerItem)
mSpinner.adapter = arrayAdapter
myTabTitles.forEachIndexed { index, item ->
mAdapter.addFragment(
item,
FragmentDialogContent.createInstance(myTabDescriptions[index])
)
}
mViewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL
b.setView(customView)
return b.create()
}
}
片段类
class MyFragment : androidx.fragment.app.Fragment() {
private lateinit var mRecyclerView: RecyclerView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.my_layout, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
val v = view
mRecyclerView = v!!.mRecyclerViewSansToolbar
mRecyclerView.layoutManager = LinearLayoutManager(activity)
val listFacilities = ArrayList<Item>()
val myDialogTitle = "Hello World"
val myTabTitles = ArrayList<String>()
myTabTitles.add("Tab 1")
myTabTitles.add("Tab 2")
myTabTitles.add("Tab 3")
myTabTitles.add("Tab 4")
myDialogTabTitles.add("Tab 5")
val myTabDescriptions = ArrayList<String>()
myTabDescriptions.add("Description 1")
myTabDescriptions.add("Description 2")
myTabDescriptions.add("Description 3")
myTabDescriptions.add("Description 4")
myDialogTabDescriptions.add("Description 5")
super.onActivityCreated(savedInstanceState)
}
}