Android的力量碎片重建查看 [英] Android force Fragment to rebuild View

查看:126
本文介绍了Android的力量碎片重建查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的应用程序,有两个片段,当在横向模式下,这两个片段是由侧方显示在我的肖像显示片段A,然后如果他们选择一个选项,启动一个活动,显示碎片B.我的问题就是当我在肖像模式和显示片段B,如果用户选择我想刷新或重绘与片段B相关联,可以不知道如何使这项工作在查看菜单选项。我试过getView方法和getLayoutInflater方法​​,但屏幕没有改变,因为我觉得我创建一个新的看法。我也试图让一个参考片段的想法我可以调用它的程序来建立一个新的片段取代片段B,但不能引用它,因为它不被显示。我真正需要做的是正义的力量的onCreateView方法再次呼吁膨胀新的看法,但我尝试了好几种方法来尝试这样做,但不能得到onCreateView再次调用。任何思考如何呢?

I have a simple app that has two fragments and when in landscape mode, both fragments are shown side by side and in portrait I show Fragment A and then if they select an option, start an Activity that shows Fragment B. My problem is when I am in Portrait mode and showing Fragment B, if the user selects a menu option I want to refresh or redraw the View that is associated with Fragment B and can’t understand how to make this work. I tried the getView method and getLayoutInflater method but the screen doesn’t change because I think I am creating a new view. I also tried to get a reference to Fragment A thinking I could call its routine to build a new fragment and replace Fragment B but can’t get a reference to it because it is not being displayed. What I really need to do is just force the onCreateView method to be called again to inflate the new view but I have tried several methods to try to do this but can’t get the onCreateView to be called again. Any thoughts on how to this?

推荐答案

您将要执行 FragmentTransaction 并使用更换()方法

这应该不是太难的事,但答案将取决于在您的菜单中的位置(即是你的 onOptionsItemSelected()再打你父母的活动里或者是在任一片段A / B?)。

This shouldn't be too hard to do, but the answer will depend on where your Menu is located (i.e. is your onOptionsItemSelected() call back inside your parent Activity or is it in either Fragment A/B?).

假设为简单起见,您的菜单实现和 onOptionsItemSelected()是父活动,并且要重塑片段在menu_option1选择的事件。它看起来是这样的:

Suppose for simplicity's sake, your menu implementation and onOptionsItemSelected() is in the parent activity, and you want to reshape the fragments in the event that menu_option1 is chosen. It would look something like this:

@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);
}
}

另外,如果你的菜单是你的片段之一(它应该是为了更多的可重复使用的code)的孩子,再一个方法是,要求主机的活动,以落实片段定义的接口中,可以用作回呼。 而在 onOptionsItemSelected()回调的片段类中,您只需拨打电话,以这种回调方法。

Alternatively, if your menu is a child of one of your Fragments (which it should be for the sake of more reusable code), then one method is to require that host Activity to implement an interface defined by the Fragment, that can be used as a call back. And in the onOptionsItemSelected() callback inside your fragment class, you simply make a call to this callback method.

虽然这听起来像一个满口,你只有真正需要做两件事情。 pretend这是你的片段类

Although it sounds like a mouthful, you only really need to do a couple of things. Pretend that this is your Fragment class

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);
}
}
...
}

然后在活动,你会看到类似这样的:

Then in the Activity, you would see something like:

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的力量碎片重建查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆