Android 设置片段顺序首选项
我正在创建我的应用程序的一部分,我在其中添加了两个片段。它们是两个完全相同的滚动视图 不同的文本。我想在他们之间做出选择 所以我的想法是: 我们有一个片段a,它总是首先显示,而片段B在屏幕上不可见。我想在用户需要的时候放置一个按钮来交换这两个片段 那么,发展这一理念的最佳方式是什么?我发布了一些与片段A相关的代码:Android 设置片段顺序首选项,android,android-fragments,kotlin,Android,Android Fragments,Kotlin,我正在创建我的应用程序的一部分,我在其中添加了两个片段。它们是两个完全相同的滚动视图 不同的文本。我想在他们之间做出选择 所以我的想法是: 我们有一个片段a,它总是首先显示,而片段B在屏幕上不可见。我想在用户需要的时候放置一个按钮来交换这两个片段 那么,发展这一理念的最佳方式是什么?我发布了一些与片段A相关的代码: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollSoloList"
android:layout_marginTop="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textListaSolo" tools:targetApi="o"/>
</ScrollView>
主要片段活动:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listaMain">
<fragment android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.comprasapp.Lists.ListsFragments.ListViewSolo"
android:id="@+id/scrollSoloList"/>
</LinearLayout>
片段A:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollSoloList"
android:layout_marginTop="8dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textListaSolo" tools:targetApi="o"/>
</ScrollView>
控制器类的片段:
class ListViewSolo : Fragment(){
var facade = Facade()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.lista_solo, container, true)
}
@RequiresApi(Build.VERSION_CODES.O)
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
view?.findViewById<TextView>(R.id.textListaSolo)?.autoSizeMaxTextSize
view?.findViewById<TextView>(R.id.textListaSolo)?.text = (facade.setListSolo())//I'm just setting the text on my TextView here
}
companion object{
// Aqui simplemente estás creando un objeto ListViewSolo por si en algun momento tienes que utilizarlo
fun newInstance(): ListViewSolo {
return ListViewSolo()
}
}
}
类ListViewSolo:Fragment(){
var facade=facade()
覆盖创建视图(充气机:布局充气机,容器:ViewGroup?,savedInstanceState:Bundle?):视图{
返回充气机。充气(R.layout.lista_solo,容器,真实)
}
@RequiresApi(Build.VERSION\u code.O)
覆盖活动创建的乐趣(savedInstanceState:Bundle?){
super.onActivityCreated(savedInstanceState)
查看?.findViewById(R.id.textListaSolo)?.AutoSizeMaxSize
view?.findViewById(R.id.textListaSolo)?.text=(facade.setListSolo())//我只是在这里设置TextView上的文本
}
伴星{
//这是一个简单的列表视图,它是一个实用的视图
fun newInstance():ListViewSolo{
返回ListViewSolo()
}
}
}
如果我能弄清楚一些东西,请问一下,谢谢。你不使用什么
ViewPager
,它的主要用途是达到你的标准
它还可以让你在片段之间轻松滑动
请查看教程,了解如何在应用程序中实现
ViewPager
您不使用什么ViewPager
,其主要用途是达到您的标准
它还可以让你在片段之间轻松滑动
请查看教程,了解如何在应用程序中实现
ViewPager
您可以使用容器在片段之间切换
主要片段活动:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listaMain">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
您可以使用容器在片段之间切换 主要片段活动:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listaMain">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
您可能需要带有
FragmentStatePagerAdapter的ViewPager
->您可能需要带有FragmentStatePagerAdapter的ViewPager
->