gpt4 book ai didi

Android 强制 Fragment 重建 View

转载 作者:可可西里 更新时间:2023-11-01 19:06:33 25 4
gpt4 key购买 nike

我有一个简单的应用程序,它有两个 fragment ,在横向模式下,两个 fragment 并排显示,在纵向模式下,我显示 fragment A,然后如果他们选择一个选项,则启动一个显示 fragment B 的 Activity 。我的问题当我处于纵向模式并显示 Fragment B 时,如果用户选择了一个菜单选项,我想刷新或重绘与 Fragment B 关联的 View ,但无法理解如何进行这项工作。我尝试了 getView 方法和 getLayoutInflater 方法,但屏幕没有改变,因为我认为我正在创建一个新 View 。我还尝试获取对 fragment A 的引用,认为我可以调用它的例程来构建一个新 fragment 并替换 fragment B,但无法获取对它的引用,因为它没有被显示。我真正需要做的只是强制再次调用 onCreateView 方法以膨胀新 View ,但我尝试了几种方法来尝试执行此操作但无法再次调用 onCreateView。对此有何想法?

最佳答案

您需要执行一个FragmentTransaction 并使用replace() 方法

这不应该太难做,但答案将取决于您的菜单所在的位置(即您的 onOptionsItemSelected() 回调是在您的父 Activity 中还是在任一 Fragment 中A/B?)。

为简单起见,假设您的菜单实现和 onOptionsItemSelected() 位于父 Activity 中,并且您希望在选择 menu_option1 的情况下 reshape fragment 。它看起来像这样:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
//...
switch (item.getItemId()) {
case R.id.menu_option1:
//do something
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment newFragment = new YourFragmentClass();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.your_fragment_id, newFragment);
transaction.addToBackStack(null);
transaction.commit();
return true;
case R.id.menu_option2:
//do something else;
return true;
default:
return super.onOptionsItemSelected(item);
}
}

或者,如果您的菜单是您的一个 Fragment 的子项(为了代码的可重用性更高,应该这样做),那么一种方法是要求宿主 Activity 实现由 Fragment 定义的接口(interface),该接口(interface)可以用作回调。在 fragment 类中的 onOptionsItemSelected() 回调中,您只需调用此回调方法即可。

虽然这听起来很啰嗦,但您实际上只需要做几件事。假装这是你的 Fragment 类

public static class FragmentA extends ListFragment {
OnSelectedListener mListener;
// Container Activity must implement this interface
public interface OnSelectedListener {
public void onSelected();
}
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//This is to ensure that the Activity has implemented the interface we set up above
try {
mListener = (OnSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnSelectedListener");
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
//...
switch (item.getItemId()) {
case R.id.menu_option1:
//do something
getActivity().onSelected();
return true;
case R.id.menu_option2:
//do something else;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
...
}

然后在 Activity 中,您会看到如下内容:

public class MainActivity extends Activity implements FragmentA.onSelectedListener{
...
public void onSelected(){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment newFragment = new YourFragmentClass();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.your_fragment_id, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}

关于Android 强制 Fragment 重建 View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7796952/

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