如何使用片段组织应用程序?

Mik*_*ike 5 android android-layout android-fragments android-navigation

我目前正在重新编码我的 android 应用程序的大部分后端,以便更紧密地遵循设计指南。目前我正在使用所有活动和零片段。我正在尝试切换到片段以使用滑出导航绘制并最终使用一些滑动选项卡。

对于现在的导航,我有这个下拉菜单,当点击一个项目时会启动一个新的活动:

在此处输入图片说明

“您的统计信息”活动有点像主页,用户也将在其中进入应用程序。我还希望用户能够从应用程序的任何位置返回到该“页面”。

我计划运行绘图的活动我有一个名为 fragment_main 的绘图布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <ListView
        android:id="@+id/drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#FFF"
        android:choiceMode="singleChoice"/>

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

我加载抽屉布局的活动是:

public class MainDraw extends FragmentActivity {
    final String[] data ={"one","two","three"};
    final String[] fragments ={
            "com.beerportfolio.beerportfoliopro.FragmentOne",
            "com.beerportfolio.beerportfoliopro.FragmentTwo",
            "com.beerportfolio.beerportfoliopro.FragmentThree"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        //todo: load statistics

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);

        final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
        final ListView navList = (ListView) findViewById(R.id.drawer);
        navList.setAdapter(adapter);
        navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
                drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
                    @Override
                    public void onDrawerClosed(View drawerView){
                        super.onDrawerClosed(drawerView);
                        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
                        tx.replace(R.id.main, Fragment.instantiate(MainDraw.this, fragments[pos]));
                        tx.commit();
                    }
                });
                drawer.closeDrawer(navList);
            }
        });
        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.main,Fragment.instantiate(MainDraw.this, fragments[0]));
        tx.commit();
    }

}
Run Code Online (Sandbox Code Playgroud)

在我的 //todo: 评论中,我应该加载我的第一个“主页”片段,即我的统计信息“页面”?然后所有其他片段将根据绘制点击进出?

提前感谢您的帮助,我想确保我这样做是正确的,我曾经编写代码只是为了让事情正常工作,这就是为什么我现在要重新编写大量代码。请分享我可能需要的任何其他片段提示!

daf*_*afi 2

首先阅读写得好的文档,它回答了您的疑问。

我会分享我的个人模式,将现有的转换ActivityFragment

  • 创建抽象 Fragment 类,从中派生所有抽屉片段,这可以帮助对公共属性进行分组
  • 使用类似于文档的方法selectItem(),它有助于在第一次运行时显式执行调用(显示“home”片段),然后从onItemClick
  • 将膨胀的 XML 布局从Activity.onCreate()代码移动到Fragment.onCreateView()(即setContentViewinflater.inflate(R.layout.my_layout, container, false)在许多情况下,您可以将所有代码从 复制onCreate()onCreateView
  • 将初始化代码从 移动Activity.onCreate()Fragment.onActivityCreated(),当 Activity(包括片段)和直接片段都存在时,这非常有用,例如,如果您的应用程序公开“共享”操作,您将继续拥有 XML 内包含 a<fragment/>和片段的Activity也可以从抽屉中创建
  • 如果您需要从 Activity 到 Fragment 进行通信,反之亦然,我建议创建一个接口并将其存储在“onAttach()”内(请参阅谷歌示例)
  • 当抽屉打开时,操作栏项目必须隐藏,再次看一下文档中使用的示例,这里是从活动到片段通信的接口非常有用,主活动可以告诉抽屉是否打开,片段可以调用该接口

    public interface FragmentActivityStatus {
        public boolean isDrawerOpen();
    }
    
    Run Code Online (Sandbox Code Playgroud)

活动

    public class MainActivity extends Activity implements FragmentActivityStatus {
        @Override
        public boolean isDrawerOpen() {
            return drawerLayout.isDrawerOpen(drawerList);
        }
    }
Run Code Online (Sandbox Code Playgroud)

片段

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        fragmentActivityStatus = (FragmentActivityStatus)activity;
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        boolean isMenuVisible = !fragmentActivityStatus.isDrawerOpen();
        menu.findItem(R.id.my_menu).setVisible(isMenuVisible);
        super.onPrepareOptionsMenu(menu);
    }
Run Code Online (Sandbox Code Playgroud)

与片段无关,在您的代码中您将类名声明为字符串,Class如果您重构包代码继续工作,请考虑创建一个数组,然后您可以调用Class.getName()来获取要传递给的字符串Fragment.instantiate()

   final Class<?>[] fragments = {
        FragmentOne.class,
        FragmentTwo.class,
        FragmentThree.class};
Run Code Online (Sandbox Code Playgroud)

然后

    FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
    tx.replace(R.id.main, Fragment.instantiate(MainDraw.this,
         fragments[pos].getName()));
    tx.commit();
Run Code Online (Sandbox Code Playgroud)