如何为一个活动处理多个片段 [英] How to handle multiple Fragments for a single Activity

查看:63
本文介绍了如何为一个活动处理多个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始研究为我的android设备开发应用程序.对我来说,引起这种兴趣的是我正在和一些arduino一起玩耍,这个想法非常棒,可以让他们与我的手机进行通讯,比如说我在arduino本身上测量的任何值的接口.现在,我可以采取简单的方法,并使用公共资源来完成此任务,但是要学习的方法则不多,我希望以自己想要的方式来学习.

I have recently started to take a look at developing apps for my android device. What started this interest for me is I was playing around with a few arduinos had the great idea to make them communicate with my phone, as say an interface for whatever values I am measuring on the arduino itself. Now I could take the easy way out and use a public source to accomplish this but there isn't as much to learn that way, and I would like it the way I want it.

现在,我想我要问的第一个问题是,多个片段/单个活动是我完成此任务的最佳方法吗?基本上,我想要1连接到arduino,拉出所有值,但是根据我选择的选项卡",我希望某些值以某些方式显示.我决定将每个选项卡制作为不同的片段,并仅以不同的方式显示值.就像我说的那样,我才刚刚开始android开发经验,所以没有太多的选择依据.

Now I suppose the first question I need to ask is, would multiple fragments/single activity be the best way for me to accomplish this? Basically, I want 1 connection to the arduino, pull all the values, but depending on the "tab" I have selected I want certain values displayed certain ways. I decided to make each tab a different fragment and just display the values different ways. Like I said, I am just beginning android development experience so don't have much to base this choice off of.

因此,我被这个多片段想法所吸引:

So being fixated on this multiple fragment idea I have:

  1. 创建了多个Fragment.xml文件
  2. 为每个单独的视图定义一个类
  3. 创建了一个列表以显示可用的片段
  4. 实例化并显示选定的片段

因此,基本上我的onMenuItemSelect看起来像这样.

So essentially my onMenuItemSelect looked like this.

FragmentTransaction FT = getFragmentManager.beginTransaction();
switch(position){
    case 1:
        FT.replace(R.id.fragment_container, new MyFragment()).commit();
        break;
    case 2:
        FT.replace(R.id.fragment_container, new MySecondFragment()).commit();
        break;
}

上面的代码可以正常工作,它实现了我想要的功能,没有任何问题.不过,我并不是很喜欢,因为对于每个我想添加的Fragment,我都需要向交换机添加一个新的case.而且,即使已经创建了一个片段,它也会每次实例化一个新片段.那是问题吗?

The above code worked, it did what I wanted it to without any issues. I don't really like this though, because for each and every Fragment I wanted to add I would need to add a new case to the switch. Also this instantiates a new fragment every time, even if one was already created. Is that a problem?

我遇到的最大问题是,它不是最容易扩展的.对于2-3个碎片(在我眼中),这并不是最糟糕的处理方式.我希望能够拥有尽可能多的片段,而在交换机中每个片段都没有单独的情况.因此,我创建了一个fragmentList,以保存每个片段的单个实例.

The biggest problem I had with it is that it isn't the easiest to scale. For 2-3 fragments this isn't the worst way to handle it (in my eyes). I want to be able to have as many fragments I want without an individual case for each one in the switch. So what I did was created a fragmentList to hold a single instance of each of my fragment.

List<Fragment> fragmentList;

private void populateFragmentList();{
    fragmentList = new ArrayList<Fragment>();
    fragmentList.add(new HomeFrag());
    fragmentList.add(new BluetoothFragment());
    fragmentList.add(new USBFragment());
    fragmentList.add(new RCInfoFragment());
    fragmentList.add(new ControllerFragment());
    fragmentList.add(new FingerCordsFrag());
}
public void onMenuItemSelect(int position, int curPosition){
    if(fragmentList.get(position).isAdded()){
       getFragmentManager().beginTransaction().show(fragmentList.get(postition))
        .hide(fragmentList.get(curPosition)).commit();
    }
    else
        getFragmentManager().beginTransaction().add(R.id.fragment_container, fragmentList.get(position)).show(fragmentList.get(position)).hide(fragmentList.get(curPosition)).commit();
}

这种方法也有效.我可以显示所有片段,而不必每次都重新实例化每个片段.我相信这可以满足我的要求,并且可以很好地扩展(比IMO开关/案例更好).我现在遇到的问题是,当我改变方向时,一切都会变得疯狂.到目前为止,我只测试人像模式,以其他方向选择它们时,我无法查看任何片段.我可以在任一方向上启动它,但是它可以工作,但是当我在运行时更改它时,我只能看到更改方向时打开的一个片段.

And this method also worked. I could get it to display all of my fragments, without have to re-instantiate each fragment each time. I believe this does what I am wanting it to do, it scales fairly well(better than a switch/case IMO). The problem that I have now is that it all goes crazy when I change orientation. Up until now I was only testing portrait mode, I am not able to view any of my fragments when I select them in other orientation. I can start it in either orientation, and it works, but when I change it during run-time, I am only able to see the one fragment I had open when I changed orientation.

现在,每个片段"onCreateView"都被调用,只是显示没有显示.我相信我已将其范围缩小到没有附加到方向改变所创建的新活动中.无论如何,我可以将已经创建的片段重新附加到新活动中.

Now, each fragments "onCreateView" is being called, it is just that the display isn't being shown. I believe I have it narrowed down to it isn't being attach to the new activity created from the orientation change. Is There anyway I can reattach fragments that are already created to a new activity.

总而言之,我的问题是:

In summary, the questions I have are:

  1. 此模型是否对我的应用程序来说是一个好方法?
  2. 是否有一种不错的方式来处理可扩展的片段?不能似乎找到了任何例子.
  3. 每次打开不同的标签时,正在使用"new MyFragment()"实现这一目标的合理方法?
  4. 将片段存储在列表中的一种合理方法是处理他们?
  5. 如何将片段重新附加到新的活动上方向改变了?

谢谢您的时间.

*由于某些原因,我无法即时输入所有这些代码,因此我无法正确格式化C/P代码.

*Had to type all this code on the fly because I, for some reason, couldn't get my C/P'd code to format correctly.

推荐答案

我相信使用片段并从

I believe it a good choice to use fragments and start with this example...

您绝对应该重写某些适配器"以更轻松地处理所有交易...

You should definitely override some "Adapter" to handle all the transactions more easily...

此处中检查方向问题...

Check here for the orientation problem...

这篇关于如何为一个活动处理多个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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