如何碎片分开,在Android Studio中不同的文件? [英] How to separate fragments to different files in android studio?

查看:582
本文介绍了如何碎片分开,在Android Studio中不同的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发在Android Studio中的Andr​​oid应用程序,我不断在MainActivity类写作越来越多的碎片。我的问题是如何将这些分离到另一个文件?也许我这样做了错误的方式,如果是能有人告诉我,我应该怎么办呢?

我的code:

 公共类MainActivity扩展ActionBarActivity
    实现NavigationDrawerFragment.NavigationDrawerCallbacks {



公共静态类PlaceholderFragment1扩展片段{...}
公共静态类PlaceholderFragment2扩展片段{...}
公共静态类PlaceholderFragment3扩展片段{...}
}


解决方案

由于他们是静态内部类,AndroidStudio可以很容易地重构这些你。选择 PlaceholderFragment1 (只是把文本光标在其上)和preSS F6(或右键单击片段名称 - > refactor->移动)并选择`移动内部类[片段名]上一级,改变如果你想要的名称,包装,打重构。

具有片段的静态内部类是罚款(将工作在技术上),但如果你想重用在另一个活动片段​​,最好的重构出来。此外,大多数人喜欢把班级尽可能小,如果片段的功能在逻辑上的分离活动有什么理由把它作为一个内部类。

I am trying to develop an android app in android studio and I keep writing more and more fragments in the mainActivity class. My question is how to separate these to another file? Probably I am doing it in a wrong way and if so could somebody show me how should I do it?

My code:

public class MainActivity extends ActionBarActivity
    implements NavigationDrawerFragment.NavigationDrawerCallbacks {
.
.
.
public static class PlaceholderFragment1 extends Fragment {...}
public static class PlaceholderFragment2 extends Fragment {...}
public static class PlaceholderFragment3 extends Fragment {...}
}

解决方案

Since they are static inner classes, AndroidStudio can easily refactor these for you. Select PlaceholderFragment1 (just put the text cursor on it) and press F6 (or right click the fragment name->refactor->move) and select `Move inner class [fragment name] to upper level', change the name and package if you want and hit refactor.

Having a static inner class for a Fragment is fine (will work technically), but if you want to reuse the fragment in another activity, best to refactor it out. Also, most people like to keep classes as small as possible, and if the function of the fragment is logically separate from the activity there is little reason to keep it as an inner class.

这篇关于如何碎片分开,在Android Studio中不同的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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