简体   繁体   English

在MVVM架构中从ViewModel添加片段

[英]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类中获取supportFragmentManagerchildFragmentManager 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.

相关问题 从 MVVM 架构中的 ViewModel 更改片段 - Change fragment from ViewModel in MVVM architecture 在 Android MVVM 架构中显示来自 ViewModel 的对话框 - Show Dialog from ViewModel in Android MVVM Architecture MVVM体系结构中的ViewModel操作 - ViewModel manipulation in MVVM Architecture 使用架构组件 MVVM 进行身份验证,将令牌从 Repository 传递到 ViewModel - Authentication with Architecture Components MVVM, passing token from Repository to ViewModel 如何在Android的MVVM架构中将动作从View发送到ViewModel - How to send actions from View to ViewModel in MVVM architecture in Android 在 MVVM 架构中从 ViewModel 启动 Activity 的最佳方法 - Best approach to start an Activity from ViewModel in MVVM Architecture 处理ViewModel中的数据和MVVM中的Fragment - Handle data in ViewModel and Fragment in MVVM 从 MvvM 架构和 Kotlin 中的广播接收器更新片段 ui - Update fragment ui from broadcast receiver in MvvM architecture and Kotlin 单击基于 MVVM 的架构不起作用的片段的 XML 按钮的侦听器 - Click Listener of button from the XML of a fragment that is MVVM based architecture not working 使用 MVVM 架构的 ViewModel 注入(内部视图) - ViewModel injection (inside View) with MVVM Architecture
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM