繁体   English   中英

为什么我可以替换一个新片段,而不替换一个初始化片段?

[英]Why can I replace a new fragment but not a initialized fragment?

这是我的自定义片段:

接口:

public interface NotVerifiedWifiCardFragment{
    void loadSSIDName(String ssid);

    void setArguments(Bundle bundle);
}

类:

public class NotVerifiedWifiCardFragmentImpl extends Fragment implements NotVerifiedWifiCardFragment {

    private TextView ssid;
    private View view;

    //@Inject
    //NotVerifiedWifiCardPresenter presenter;

    @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_card_connected_not_verified, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        ssid = (TextView) view.findViewById(R.id.card_not_verified_wifi_ssid);

        Bundle bundle = getArguments();
        ssid.setText(bundle.getString("SSID"));
        //presenter.setView(this);
    }

    @Override
    public void loadSSIDName(String ssid) {
        this.ssid.setText(ssid);
    }
}

当我用FragmentTransaction替换新的CustomFragment时,我可以毫无问题地做到这一点:

@Override
    public void showNotVerifiedWifiCard(String ssid) {
        // Begin the transaction
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        // Replace the contents of the container with the new fragment
        ft.setCustomAnimations(R.anim.slide_up_anim, R.anim.slide_down_anim);
        ft.replace(R.id.main_small_card, new NotVerifiedWifiCardFragmentImpl());

        // or ft.add(R.id.your_placeholder, new FooFragment());
        // Complete the changes added above
        ft.commit();
    }

但是,当我尝试使用相同的片段但已初始化(通过捆绑包)调用片段事务时,replace方法不允许我。 当您需要传递数据包时如何解决?

@Override
    public void showNotVerifiedWifiCard(String ssid) {
        Bundle bundle = new Bundle();
        bundle.putString("SSID", ssid);

        NotVerifiedWifiCardFragment fm = new NotVerifiedWifiCardFragmentImpl();
        fm.setArguments(bundle);

        // Begin the transaction
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        // Replace the contents of the container with the new fragment
        ft.setCustomAnimations(R.anim.slide_up_anim, R.anim.slide_down_anim);
        ft.replace(R.id.main_small_card, fm);

        // or ft.add(R.id.your_placeholder, new FooFragment());
        // Complete the changes added above
        ft.commit();
    }

可能是被误解的愚蠢的面向对象的concexpt。

编辑:错误显示为:

错误:第二个参数类型错误。 找到:“ com.blabla.NotVerifiedWifiCardFragment”,必需:“ android.support.v4.app.Fragment”

如果我正确理解了您的问题,则可以尝试使用

public static Fragment instantiate(Context context, String fname, @Nullable Bundle args)

像这样 :

NotVerifiedWifiCardFragment fm = (NotVerifiedWifiCardFragment) Fragment.instantiate(context, "some_name", your_args);

由于没有任何实现NotVerifiedWifiCardFragment对象实际上是一个片段(您可以声明任何对象类并使其实现此协议),因此您不能将NotVerifiedWifiCardFragment视为Fragment(您给接口命名的名称是错误的)。

例如:

class Test implements NotVerifiedWifiCardFragment {
void loadSSIDName(String ssid){}

void setArguments(Bundle bundle){}
....
}

测试只是一个简单的类,而不是一个片段。

一个快速的解决方案将被替换

NotVerifiedWifiCardFragment fm = new NotVerifiedWifiCardFragmentImpl();

NotVerifiedWifiCardFragmentImpl fm = new NotVerifiedWifiCardFragmentImpl()

在这种情况下,您真正​​想要的是创建一个名为NotVerifiedWifiCardFragment的抽象Fragment ,并使其他片段对其进行扩展。

范例程式码

abstract class NotVerifiedWifiCardFragment extends Fragment {
     abstract void loadSSIDName(String ssid);
     abstract void setArguments(Bundle bundle);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM