用kotlin写一个适用于安卓的关注和粉丝界面,要求要在同一个activity中,通过关注和粉丝两个标签来切换列表,列表使用recycleview,用xml语言写布局文件
时间: 2024-05-02 10:23:45 浏览: 102
。
好的,我明白了。关注和粉丝界面可以通过使用ViewPager实现,ViewPager中包含两个Fragment,分别加载关注和粉丝的RecycleView,然后在主Activity中通过标签切换ViewPager页。以下是一份实现代码,你可以作为参考:
1.首先,在主activity布局文件中,添加ViewPager和标签栏TabLayout:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</RelativeLayout>
```
2.在主activity中初始化ViewPager和TabLayout,设置TabLayout和ViewPager的关联:
```
private lateinit var viewPager: ViewPager
private lateinit var tabs: TabLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewPager = findViewById(R.id.viewPager)
tabs = findViewById(R.id.tabs)
setupViewPager()
tabs.setupWithViewPager(viewPager)
}
private fun setupViewPager() {
val adapter = ViewPagerAdapter(supportFragmentManager)
adapter.addFragment(FollowingFragment(), "关注")
adapter.addFragment(FollowerFragment(), "粉丝")
viewPager.adapter = adapter
}
internal class ViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {
private val fragmentList: MutableList<Fragment> = ArrayList()
private val titleList: MutableList<String> = ArrayList()
override fun getItem(position: Int): Fragment {
return fragmentList[position]
}
override fun getCount(): Int {
return fragmentList.size
}
fun addFragment(fragment: Fragment, title: String) {
fragmentList.add(fragment)
titleList.add(title)
}
override fun getPageTitle(position: Int): CharSequence {
return titleList[position]
}
}
```
3.创建两个Fragment,分别加载关注和粉丝的RecycleView:
FollowingFragment.kt
```
class FollowingFragment : Fragment() {
private lateinit var recyclerView: RecyclerView
private lateinit var followingAdapter: FollowingAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_following, container, false)
recyclerView = view.findViewById(R.id.recyclerView_following)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(activity)
followingAdapter = FollowingAdapter(getFollowingList())
recyclerView.adapter = followingAdapter
return view
}
private fun getFollowingList(): MutableList<String> {
val list = mutableListOf<String>()
list.add("小明")
list.add("小红")
list.add("小芳")
list.add("小青")
//添加想要的数据
return list
}
}
```
FollowerFragment.kt
```
class FollowerFragment : Fragment() {
private lateinit var recyclerView: RecyclerView
private lateinit var followerAdapter: FollowerAdapter
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_follower, container, false)
recyclerView = view.findViewById(R.id.recyclerView_follower)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(activity)
followerAdapter = FollowerAdapter(getFollowerList())
recyclerView.adapter = followerAdapter
return view
}
private fun getFollowerList(): MutableList<String> {
val list = mutableListOf<String>()
list.add("小明")
list.add("小红")
list.add("小芳")
list.add("小青")
//添加想要的数据
return list
}
}
```
4.最后,在layout文件夹中创建两个RecycleView布局文件:
fragment_following.xml
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_following"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
```
fragment_follower.xml
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_follower"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
```
以上就是Kotlin写的适用于安卓的关注和粉丝界面的实现,希望对您有所帮助。
阅读全文