[英]Add fragment from ViewModel in MVVM architecture
I am using DataBinding
and following MVVM architecture , now i am stuck on how to add new fragment from ViewModel
as we need defined click event on ViewModel
. 我正在使用DataBinding
并遵循MVVM体系结构 ,现在我被困在如何从ViewModel
添加新片段上,因为我们需要在ViewModel
上定义click事件。 Here is my MainViewModel
class 这是我的MainViewModel
类
public class MainViewModel {
private Context context;
public MainViewModel (Context context) {
this.context = context;
}
public void onClick(View v) {
}
}
here is my xml where i have defined click event 这是我定义单击事件的XML
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewmodel"
type="com.example.MainViewModel" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{viewmodel::onClick}"
android:text="click me"/>
</RelativeLayout>
</layout>
now how can i get supportFragmentManager
or childFragmentManager
from my ViewModel class? 现在如何从我的ViewModel类中获取supportFragmentManager
或childFragmentManager
? I have tried to use activity.getSupportFragmentManager()
and activity.getChildFragmentManager()
but it doesn't have that kind of method. 我尝试使用activity.getSupportFragmentManager()
和activity.getChildFragmentManager()
但是它没有这种方法。
I know we can add fragment with following code 我知道我们可以使用以下代码添加片段
getActivity().getSupportFragmentManager().beginTransaction()
.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, android.R.anim.fade_in, android.R.anim.fade_out).
add(R.id.container, fragment, "").addToBackStack("main").commit();
but how to do it in ViewModel
class 但是如何在ViewModel
类中做到这一点
Since you have your Context
available, you have two possibilities: 由于您有可用的Context
,因此有两种可能性:
public class MainViewModel {
private Context context;
public MainViewModel (Context context) {
this.context = context;
}
public void onClick(View v) {
//use context:
((AppCompatActivity) context).getSupportFragmentManager();
//OR use the views context:
if(v.getContext() instanceof AppCompatActivity) {
((AppCompatActivity) v.getContext()).getSupportFragmentManager();
}
}
}
It may be useful to check whether the context is an instance of your activity (like MainActivity
) or AppCompatActivity
, or if it is null
before calling any method. 在调用任何方法之前,检查上下文是您的活动的实例(例如MainActivity
)还是AppCompatActivity
,或者它是否为null
可能很有用。
I don't know if it's possible but here is what i suggest: 我不知道是否可能,但这是我的建议:
Define an interface and let the Activity or Fragment implement this interface 定义一个接口,并让Activity或Fragment实现此接口
public interface FragmentProvider {
void showFragment(...);
}
Pass an instance of FragmentProvider into your ViewModel 将FragmentProvider的实例传递到您的ViewModel中
public class MainViewModel {
private Context context;
private FragmentProvider provider;
public MainViewModel (FragmentProvider provider) {
this.provider = provider;
}
public void onClick(View v) {
// delegate the action
provider.showFragment(...);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.