显示Android TV的模式

时间:2018-10-06 14:47:07

标签: android android-tv

我正在尝试在AndroidTV应用中显示模式。我尝试按照本指南创建DialogFragment的子类:https://developer.android.com/reference/android/app/DialogFragment

我有点困惑,因为当我尝试显示对话框时,示例显示:

FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment prev = getFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
    newFragment.show(ft, "dialog");

但是当我尝试创建FragmentTransaction时,Android Studio想要使用我认为不同的FragmentTransaction。

android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
        android.app.Fragment prev = getFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }

        ft.addToBackStack(null);
        DialogFragment newFragment = CartDialogFragment.newInstance(111);

因此,当我尝试执行newFragment.show时,它无法解析该方法。

我的CartDialogFragment导入看起来像:

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

我需要添加到Gradle文件中吗?这种DialogFragment是否与AndroidTV leanback库兼容?

1 个答案:

答案 0 :(得分:0)

尝试用getFragmentManager()替换getSupportFragmentManager(),如下所示:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
    ft.remove(prev);
}
ft.addToBackStack(null);

// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");

由于您的片段扩展了android.support.v4.app.DialogFragment,因此您还应该使用兼容版本的FragmentManager

也可以看看enter image description here