gpt4 book ai didi

android - 如何使用TabLayout在Fragment中添加ListView?

转载 作者:行者123 更新时间:2023-12-02 13:39:53 25 4
gpt4 key购买 nike

我在Kotlin真的很新。我尝试使用TabLayout制作可滑动的页面。我有3种不同的Fragments,我需要在该片段上添加ListViewRecyclerView,但是在这一点上,我不知道必须在哪里添加ListView适配器。我在片段的Kotlin文件上添加了适配器类,但无法连接到片段和片段。(我认为)我收到一个错误,即ListView不能为null,因此我可以说我的适配器不能正常工作。我该如何解决?
我在这里添加代码:
我的SecondFragment.kt代码:

class SecondFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val listView = view!!.findViewById<ListView>(R.id.main_listview)


}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val listView = view!!.findViewById<ListView>(R.id.main_listview)
return inflater.inflate(R.layout.fragment_second, container, false)


}



private class MyCustomAdapter(context: Context): BaseAdapter(){

private val mContext : Context

private val names = arrayListOf<String>("Test Name", "Steve Jobs", "Tim Cook", "Kobe King")

init {
mContext = context
}

// BURADA KAC TANE ROW OLDUGUNUN SOYLER..
override fun getCount(): Int {
return names.size
}

override fun getItem(position: Int): Any {

return "Test String"
}

override fun getItemId(position: Int): Long {
return position.toLong()
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
//val textView = TextView(mContext)
//textView.text = "Here my first row for list view.."
//return textView

val layoutInflater = LayoutInflater.from(mContext)
val rowMain = layoutInflater.inflate(R.layout.row_for_list, parent, false)


val nameTextView = rowMain.findViewById<TextView>(R.id.name_textView)
nameTextView.text = names.get(position)

val positionTextView = rowMain.findViewById<TextView>(R.id.position_textview)
positionTextView.text = "Row Number: $position"

return rowMain

}


}
}
这是我的ViewPagerAdapter.kt文件代码:
class ViewPagerAdapter(supportFragmentManager: FragmentManager) : FragmentPagerAdapter(supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

private val mFragmentList = ArrayList<Fragment>()
private val mFragmentTitleList = ArrayList<String>()


override fun getCount(): Int {
return mFragmentList.size
}

override fun getItem(position: Int): Fragment {
return mFragmentList[position]
}

override fun getPageTitle(position: Int): CharSequence? {
return mFragmentTitleList[position]
}

fun addFragment(fragment: Fragment, title: String){
mFragmentList.add(fragment)
mFragmentTitleList.add(title)
}




}
,这里是我的MainActivity.kt文件代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

setUpTabs()

}
private fun setUpTabs() {

val adapter = ViewPagerAdapter(supportFragmentManager)
adapter.addFragment(HomeFragment(), "Home")
adapter.addFragment(SecondFragment(), "Second")
adapter.addFragment(ThirdFragment(), "Third")

viewPager.adapter = adapter
tabs.setupWithViewPager(viewPager)

tabs.getTabAt(0)!!.setIcon(R.drawable.ic_baseline_account_box_24)
tabs.getTabAt(1)!!.setIcon(R.drawable.ic_baseline_add_ic_call_24)
tabs.getTabAt(2)!!.setIcon(R.drawable.ic_baseline_adb_24)

}

}

最佳答案

您必须设置实例化适配器并将其设置为listView,我还在注释中解释了一些内容。

class SecondFragment : Fragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

//don't do this in onCreate, do it in onViewCreated or onCreateView
//val listView = view!!.findViewById<ListView>(R.id.main_listview)


}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val root = inflater.inflate(R.layout.fragment_second, container, false)

//you can set adapter here if you want, but i did it in onViewCreated

//return it
return root
}


override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

//get reference to ListView
val listView = root.findViewById<ListView>(R.id.main_listview)

//set adapter
listView.adapter = MyCustomAdapter(requireContext())
}


private class MyCustomAdapter(private val mContext: Context): BaseAdapter(){

//do initiation here as it needs to be done only once
private val layoutInflater: LayoutInflater = LayoutInflater.from(mContext)

private val names = arrayListOf<String>("Test Name", "Steve Jobs", "Tim Cook", "Kobe King")


// BURADA KAC TANE ROW OLDUGUNUN SOYLER..
override fun getCount(): Int {
return names.size
}

override fun getItem(position: Int): Any {

return "Test String"
}

override fun getItemId(position: Int): Long {
return position.toLong()
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
//val textView = TextView(mContext)
//textView.text = "Here my first row for list view.."
//return textView

val rowMain = layoutInflater.inflate(R.layout.row_for_list, parent, false)


val nameTextView = rowMain.findViewById<TextView>(R.id.name_textView)
nameTextView.text = names.get(position)

val positionTextView = rowMain.findViewById<TextView>(R.id.position_textview)
positionTextView.text = "Row Number: $position"

return rowMain

}


}
}

关于android - 如何使用TabLayout在Fragment中添加ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64571954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com