gpt4 book ai didi

android - 实例化 fragment 的不同方式

转载 作者:行者123 更新时间:2023-11-29 23:22:48 24 4
gpt4 key购买 nike

在下面的代码中,我想知道实例化静态 fragment 之间的区别,如下面的代码_1 所示。我发布了 fragment 的代码,如下面的 code_2 部分所示。

请告诉我这两种实例化之间的区别,以及何时使用它们。

code_1:

  StudentMVCView mStudentMVCViewInitializedInstance = (StudentMVCView) this.getSupportFragmentManager().findFragmentById(R.id.mvc_view_fragment);
Fragment mStudentMVCViewFragmentInitializedInstance = this.getSupportFragmentManager().findFragmentById(R.id.mvc_view_fragment);

code_2:

public class StudentMVCView extends Fragment implements View.OnClickListener{

private final static String TAG_LOG = StudentMVCView.class.getSimpleName();
private View mMainView = null;
private TextView mTextViewValue = null;
private EditText mEditTextValue = null;
private Button mBtn = null;
private TextView mTextViewBtnValue = null;
private StudentMVCModel mStudentMVCModel = null;
private int counter = 1;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Log.v(TAG_LOG, "onCreateView");
this.mMainView = inflater.inflate(R.layout.mvc_view_layout, null);
this.mTextViewValue = this.mMainView.findViewById(R.id.mvc_view_textView_value);
this.mEditTextValue = this.mMainView.findViewById(R.id.mvc_view_editText_value);
this.mBtn = this.mMainView.findViewById(R.id.mvc_view_button);
this.mBtn.setOnClickListener(this);
this.mTextViewBtnValue = this.mMainView.findViewById(R.id.mvc_view_textView_btnValue);

return this.mMainView;
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.v(TAG_LOG, "onViewCreated");
view.findViewById(R.id.mvc_view_textView_value);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.v(TAG_LOG, "onActivityCreated");
Bundle bundledArgs = this.getArguments();
StudentMVCModel studentMVCModel = (StudentMVCModel) this.getSerializedfromBundle(bundledArgs, "p");
Log.v(TAG_LOG, "studentMVCModel.getStudentId()" + studentMVCModel.getStudentId());

this.setStudentMVCModelObject(studentMVCModel);
}

private void setStudentMVCModelObject(StudentMVCModel studentMVCModel) {
this.mStudentMVCModel = studentMVCModel;
}

private StudentMVCModel getStudentMVCModelObject() {
return this.mStudentMVCModel;
}
private Bundle getBundledArguments() {
Log.d(TAG_LOG, "getBundledArguments");
if (this.getArguments() !=null) {
return this.getArguments();
} else {
Log.e(TAG_LOG, "this.getArguments() is NULL.");
throw new NullPointerException("getArguments is NULL");
}
}
private Object getSerializedfromBundle(Bundle bundle, String key) {
Log.d(TAG_LOG, "getSerializedfromBundle");
if (bundle != null) {
return bundle.get(key);
} else {
Log.e(TAG_LOG, "bundle is NULL.");
throw new NullPointerException("bundle is null");
}
}

@Override
public void onDestroy() {
super.onDestroy();
Log.v(TAG_LOG, "onDestroy");
}

public void setStudentIdToView(int id) {
if (this.mMainView != null) {
TextView textView = this.mMainView.findViewById(R.id.mvc_view_textView_value);
Log.v(TAG_LOG, "TextView contains: " + textView.getText().toString());
}
}

public void setTextViewValueFor(int id) {
if (this.mTextViewValue != null) {
this.mTextViewValue.setText("" + id);
} else {
Log.e(TAG_LOG, "setTextViewValueFor is NULL.");
}
}

public void setEditTextValueFor(String str) {
if (this.mEditTextValue != null) {
this.mEditTextValue.setText(str);
} else {
Log.e(TAG_LOG, "mEditTextValue is NULL.");
}
}

public void clearEditText() {
if (this.mEditTextValue != null) {
this.mEditTextValue.setText("");
} else {
Log.e(TAG_LOG, "mEditTextValue is NULL.");
}
}

@Override
public void onClick(View v) {
int id = this.getStudentMVCModelObject().getStudentId();
Log.i(TAG_LOG, "onClick: id: " + id + " counter: " + counter++);
}
}

最佳答案

在第一行中,您将 fragment 转换到 StudentMVCView类型,这样您就可以访问添加到其中的额外成员,例如 setTextViewValueFor(int id) , setEditTextValueFor(String str) , ..等

  StudentMVCView mStudentMVCViewInitializedInstance = (StudentMVCView) this.getSupportFragmentManager().findFragmentById(R.id.mvc_view_fragment);

在第二行中,您将获得 fragment ,因为它是 Android 框架的 Fragment 的父类(super class)型。类型,在这种情况下,您无法访问 StudentMVCView 中的这些额外成员类型

  Fragment mStudentMVCViewFragmentInitializedInstance = this.getSupportFragmentManager().findFragmentById(R.id.mvc_view_fragment);

关于android - 实例化 fragment 的不同方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53980685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com