簡體   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