使用 navArgs 将 Fragment 转换为 Dialog Fragment,如何从 Fragment 外部导航到 DialogFragment class
Convert Fragment to Dialog Fragment with navArgs, How to navigate to DialogFragment from outside the Fragment class
我想导航到DetailsFragment onClick 列表项(book_item.xml)
book_item.xml
<LinearLayout
...
onBooksClickListener="@{result}">
...
</LinearLayout>
现在它适用于 Fragment(全屏)
我想让它覆盖片段所以想把它转换成对话框片段
片段
的工作代码
片段Class
class DetailsFragment : Fragment() {
private val args by navArgs<DetailsFragmentArgs>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_details, container, false)
}
}
行绑定Class
class BooksRowBinding {
companion object {
@ExperimentalCoroutinesApi
@BindingAdapter("onBooksClickListener")
@JvmStatic
fun onBooksClickListener(linearLayout: LinearLayout, result: Result){
linearLayout.setOnClickListener{
try {
val action = BooksListFragmentDirections.actionBooksListFragmentToDetailsFragment(result)
linearLayout.findNavController().navigate(action)
}catch (e: Exception){
Log.d("OnBooksClickListener",e.toString())
}
}
}
为了将它转换为 DailogFragment,我尝试使用 google android 文档,但它没有解释如何从 Fragment 外部打开 dialogFragment class
DialogFragment
的损坏代码
片段Class
class DetailsFragment : DailogFragment() {
private val args by navArgs<DetailsFragmentArgs>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_details, container, false)
}
}
行绑定Class
class BooksRowBinding {
companion object {
@ExperimentalCoroutinesApi
@BindingAdapter("onBooksClickListener")
@JvmStatic
fun onBooksClickListener(linearLayout: LinearLayout, result: Result){
linearLayout.setOnClickListener{
try {
DetailsFragment().show(childFragmentManager,DetailsFragment.TAG)
/****************ERROR *******************
this works only if you call this from within the
onCreateMethod of the Fragment
******************************************/
}catch (e: Exception){
Log.d("OnBooksClickListener",e.toString())
}
}
}
现在我收到错误:Unresolved reference: childFragmentManager
my_nav.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_nav"
app:startDestination="@id/booksListFragment">
<fragment
android:id="@+id/booksListFragment"
android:name="we.are.suvikranth.ui.BooksListFragment"
android:label="Suvikranth"
tools:layout="@layout/fragment_books_list">
<action
android:id="@+id/action_booksListFragment_to_detailsFragment"
app:destination="@id/detailsFragment" />
</fragment>
<fragment
android:id="@+id/detailsFragment"
android:name="we.are.suvikranth.ui.DetailsFragment"
android:label="Books Details"
tools:layout="@layout/fragment_details" >
<argument
android:name="result"
app:argType="we.are.suvikranth.models.Result" />
</fragment>
</navigation>
使用前面的代码导航到对话框片段
但在my_nav.xml
中将片段更改为对话框
现在 RowBinding Class 看起来像
class BooksRowBinding {
companion object {
@ExperimentalCoroutinesApi
@BindingAdapter("onBooksClickListener")
@JvmStatic
fun onBooksClickListener(linearLayout: LinearLayout, result: Result){
linearLayout.setOnClickListener{
try {
val action = BooksListFragmentDirections.actionBooksListFragmentToDetailsFragment(result)
linearLayout.findNavController().navigate(action)
}catch (e: Exception){
Log.d("OnBooksClickListener",e.toString())
}
}
}
my_nav.xml长得像
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_nav"
app:startDestination="@id/booksListFragment">
<fragment
android:id="@+id/booksListFragment"
android:name="we.are.suvikranth.ui.BooksListFragment"
android:label="Suvikranth"
tools:layout="@layout/fragment_books_list">
<action
android:id="@+id/action_booksListFragment_to_detailsFragment"
app:destination="@id/detailsFragment" />
</fragment>
<dialog
android:id="@+id/detailsFragment"
android:name="we.are.suvikranth.ui.DetailsFragment"
android:label="Books Details"
tools:layout="@layout/fragment_details" >
<argument
android:name="result"
app:argType="we.are.suvikranth.models.Result" />
</dialog>
</navigation>
我想导航到DetailsFragment onClick 列表项(book_item.xml)
book_item.xml
<LinearLayout
...
onBooksClickListener="@{result}">
...
</LinearLayout>
现在它适用于 Fragment(全屏)
我想让它覆盖片段所以想把它转换成对话框片段
片段
的工作代码片段Class
class DetailsFragment : Fragment() {
private val args by navArgs<DetailsFragmentArgs>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_details, container, false)
}
}
行绑定Class
class BooksRowBinding {
companion object {
@ExperimentalCoroutinesApi
@BindingAdapter("onBooksClickListener")
@JvmStatic
fun onBooksClickListener(linearLayout: LinearLayout, result: Result){
linearLayout.setOnClickListener{
try {
val action = BooksListFragmentDirections.actionBooksListFragmentToDetailsFragment(result)
linearLayout.findNavController().navigate(action)
}catch (e: Exception){
Log.d("OnBooksClickListener",e.toString())
}
}
}
为了将它转换为 DailogFragment,我尝试使用 google android 文档,但它没有解释如何从 Fragment 外部打开 dialogFragment class
DialogFragment
的损坏代码片段Class
class DetailsFragment : DailogFragment() {
private val args by navArgs<DetailsFragmentArgs>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_details, container, false)
}
}
行绑定Class
class BooksRowBinding {
companion object {
@ExperimentalCoroutinesApi
@BindingAdapter("onBooksClickListener")
@JvmStatic
fun onBooksClickListener(linearLayout: LinearLayout, result: Result){
linearLayout.setOnClickListener{
try {
DetailsFragment().show(childFragmentManager,DetailsFragment.TAG)
/****************ERROR *******************
this works only if you call this from within the
onCreateMethod of the Fragment
******************************************/
}catch (e: Exception){
Log.d("OnBooksClickListener",e.toString())
}
}
}
现在我收到错误:Unresolved reference: childFragmentManager
my_nav.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_nav"
app:startDestination="@id/booksListFragment">
<fragment
android:id="@+id/booksListFragment"
android:name="we.are.suvikranth.ui.BooksListFragment"
android:label="Suvikranth"
tools:layout="@layout/fragment_books_list">
<action
android:id="@+id/action_booksListFragment_to_detailsFragment"
app:destination="@id/detailsFragment" />
</fragment>
<fragment
android:id="@+id/detailsFragment"
android:name="we.are.suvikranth.ui.DetailsFragment"
android:label="Books Details"
tools:layout="@layout/fragment_details" >
<argument
android:name="result"
app:argType="we.are.suvikranth.models.Result" />
</fragment>
</navigation>
使用前面的代码导航到对话框片段
但在my_nav.xml
中将片段更改为对话框现在 RowBinding Class 看起来像
class BooksRowBinding {
companion object {
@ExperimentalCoroutinesApi
@BindingAdapter("onBooksClickListener")
@JvmStatic
fun onBooksClickListener(linearLayout: LinearLayout, result: Result){
linearLayout.setOnClickListener{
try {
val action = BooksListFragmentDirections.actionBooksListFragmentToDetailsFragment(result)
linearLayout.findNavController().navigate(action)
}catch (e: Exception){
Log.d("OnBooksClickListener",e.toString())
}
}
}
my_nav.xml长得像
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_nav"
app:startDestination="@id/booksListFragment">
<fragment
android:id="@+id/booksListFragment"
android:name="we.are.suvikranth.ui.BooksListFragment"
android:label="Suvikranth"
tools:layout="@layout/fragment_books_list">
<action
android:id="@+id/action_booksListFragment_to_detailsFragment"
app:destination="@id/detailsFragment" />
</fragment>
<dialog
android:id="@+id/detailsFragment"
android:name="we.are.suvikranth.ui.DetailsFragment"
android:label="Books Details"
tools:layout="@layout/fragment_details" >
<argument
android:name="result"
app:argType="we.are.suvikranth.models.Result" />
</dialog>
</navigation>