这是我的自定义片段:
接口:
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();
}
但是当我尝试使用相同的片段调用Fragment Transaction但初始化(传递Bundle)时,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();
}
可能这是一个愚蠢的面向对象的概念被误解了。
编辑:错误显示是:
错误:错误的第二个参数类型。找到:'com.blabla.NotVerifiedWifiCardFragment',必填:'android.support.v4.app.Fragment'
答案 0 :(得分:0)
如果我理解你的问题,你可以尝试使用
public static Fragment instantiate(Context context, String fname, @Nullable Bundle args)
NotVerifiedWifiCardFragment fm = (NotVerifiedWifiCardFragment) Fragment.instantiate(context, "some_name", your_args);
答案 1 :(得分:0)
由于实现NotVerifiedWifiCardFragment
的任何对象实际上都不是一个片段(您可以声明任何对象类并使其实现此协议),因此您不能将NotVerifiedWifiCardFragment视为片段(您给出的接口名称是误)。
例如:
class Test implements NotVerifiedWifiCardFragment {
void loadSSIDName(String ssid){}
void setArguments(Bundle bundle){}
....
}
测试只是一个简单的类,而不是片段。
快速解决方案将取代
NotVerifiedWifiCardFragment fm = new NotVerifiedWifiCardFragmentImpl();
与
NotVerifiedWifiCardFragmentImpl fm = new NotVerifiedWifiCardFragmentImpl()
在这种情况下,您真正想要的是创建一个名为NotVerifiedWifiCardFragment
的抽象片段 类,并使其他片段扩展它。
示例代码
abstract class NotVerifiedWifiCardFragment extends Fragment {
abstract void loadSSIDName(String ssid);
abstract void setArguments(Bundle bundle);
}