Android 更好地实现导航片段
Android better implementation of fragments for 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>
我在 MainActivity
的 onCreate
中首次启动应用程序时设置了初始片段 class:
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 语句中有很多冗余代码
有什么更好的方法来压缩代码并交换片段而不像这样重复?
您可以创建如下函数并将片段对象作为参数传递:
private void launchActivity(Fragement fragment)
{
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
现在只需创建一个对象并在需要的任何地方调用此函数即可。
它将避免重复代码。
您可以仅使用片段的新对象在导航抽屉中调用这些方法
@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 {
}
}
我的应用程序有一个导航视图和绘制布局,我正在使用片段在视图之间切换。我目前是这样实现的并且工作正常:
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>
我在 MainActivity
的 onCreate
中首次启动应用程序时设置了初始片段 class:
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 语句中有很多冗余代码
有什么更好的方法来压缩代码并交换片段而不像这样重复?
您可以创建如下函数并将片段对象作为参数传递:
private void launchActivity(Fragement fragment)
{
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
现在只需创建一个对象并在需要的任何地方调用此函数即可。 它将避免重复代码。
您可以仅使用片段的新对象在导航抽屉中调用这些方法
@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 {
}
}