Android更好地实现了导航片段

时间:2016-02-25 10:26:32

标签: java android android-layout android-fragments android-navigation

我有一个导航视图和我的应用程序的绘制布局,我正在使用片段在视图之间切换。我目前已经实现了这样,它工作正常:

app_bar_main.xml:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

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

我在应用程序首次在onCreateMainActivity中启动时设置了初始片段:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    //Provide global access to navigationview and toolbar
    NavigationView navigationView = null;
    Toolbar toolbar = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Set initial fragment
        NewFragment fragment = new NewFragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
...

当用户选择一个菜单项时,我设置换掉这样的片段:

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_new) {
            NewFragment fragment = new NewFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
        } else if (id == R.id.nav_start) {
            StartFragment fragment = new StartFragment();
            android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
...

我觉得我在初始片段集中以及在if语句中都有很多冗余代码

有什么更好的方法来压缩代码并换出片段而不像这样重复?

2 个答案:

答案 0 :(得分:1)

您可以创建如下函数,并将片段对象作为参数传递:

private void launchActivity(Fragement fragment)
{
    android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
}

现在只需创建一个对象,并在需要的地方调用此函数。 它将避免重复代码。

答案 1 :(得分:1)

您只需使用片段的新对象

,即可在导航栏中调用这些方法
    @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.search_near) {
              addNewFragmentWithBackStack(new FragmentOne());
           }else if(.....){
              addNewFragmentWithBackStack(new FragmentTwo());
           }
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;

    }

用于添加带有backstack的片段

public void addNewFragmentWithBackStack(Fragment fragment) {
    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.mainContent, fragment, fragment.getClass().getSimpleName())
                .addToBackStack(fragment.getClass().getSimpleName())
                .commit();
    } else {
    }
}