How to organize the application using fragments?

I am currently recoding most of my android application to better follow the design instructions. I am currently using all actions and null fragments. I am trying to switch to fragments in order to use navigation shift navigation and eventually some sliding tabs.

For navigation, right now I have this drop-down menu that, when I click on an item, launches a new action:

enter image description here

The activity "Your statistics" is similar to the home page, in which the user will also enter the application. I also want the user to be able to return to this "page" from anywhere in the application.

My activity, with which I plan to start a draw, I have a drawing layout called 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>

, :

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

}

//todo: comment "" , ? ?

, , , , , . , , !

+4
1

, , .

Activity Fragment

  • , ,
  • , selectItem() , ( "home" ), onItemClick
  • XML Activity.onCreate() Fragment.onCreateView() (.. setContentView inflater.inflate(R.layout.my_layout, container, false)), onCreate() onCreateView
  • Activity.onCreate() Fragment.onActivityCreated(), , Activity ( ), , , " ", Activity XML <fragment/>, ,
  • Activity to Fragment , 'onAttach()' (. Google)
  • , , , doc, , ,

    public interface FragmentActivityStatus {
        public boolean isDrawerOpen();
    }
    

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

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

, , Class, , , Class.getName(), Fragment.instantiate()

   final Class<?>[] fragments = {
        FragmentOne.class,
        FragmentTwo.class,
        FragmentThree.class};

    FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
    tx.replace(R.id.main, Fragment.instantiate(MainDraw.this,
         fragments[pos].getName()));
    tx.commit();
+3

All Articles