我生成了名为“ adddebtinfo”的新活动+片段
并尝试使用MainActivity中的FAB启动此活动,但它根本无法正常工作。
MainActivity.kt
var fab: FloatingActionButton = findViewById(R.id.fab)
fab.setOnClickListener {
val intent = Intent(this, AddDebtInfoFragment::class.java)
startActivity(intent)
AddDebtInfoFragment.kt
private lateinit var viewModel: AddDebtInfoViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
var binding : AddDebtInfoActivityBinding = DataBindingUtil.inflate(inflater ,R.layout.add_debt_info_fragment,container , false)
return binding.root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(AddDebtInfoViewModel::class.java)
// TODO: Use the ViewModel
}
}
add_debt_info_fragment.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/add_debt_info_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.adddebtinfo.AddDebtInfoFragment">
add_debt_info_activity.xml
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddDebtInfo"/>
答案 0 :(得分:0)
您正在尝试使用Intent启动Fragment。您应在以下行中将 response => {
token = response;
getInfos.getDatastores(token, url)
.then(response => {
datastores = response;
});
getInfos.getPolicies(token, url)
.then(response => {
policies = response;
});
getInfos.getClusters(token, url)
.then(response => {
cluster = response;
});
}
替换为活动名称:
then
答案 1 :(得分:0)
对于添加/替换片段,您需要单击FAB来执行FragmentTrascations。
要打开新活动,您需要针对FAB的活动/上下文执行startActivity(someIntent)
。
答案 2 :(得分:0)
Purusann,您想将FragmentTransaction
与您拥有的容器FrameLayout
一起使用。
fab.setOnClickListener {
// Create a new Fragment to be placed in the activity layout
val firstFragment = AddDebtInfoFragment()
// Add the fragment to the 'container' FrameLayout
supportFragmentManager.beginTransaction()
.add(R.id.container, firstFragment).commit()
}
请阅读以下Android文档页面以了解如何使用片段:https://developer.android.com/training/basics/fragments/fragment-ui