gpt4 book ai didi

java - Android 更好地实现了用于导航的 fragment

转载 作者:行者123 更新时间:2023-12-02 03:54:21 24 4
gpt4 key购买 nike

我的应用程序有一个导航 View 和绘制布局,并且我正在使用 fragment 在 View 之间切换。我目前已经像这样实现并且工作正常:

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 中设置初始 fragment :

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

当用户选择一个菜单项时,我设置交换 fragment ,如下所示:

@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();
...

我感觉初始 fragment 集中以及 if 语句中有很多冗余代码

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

最佳答案

您可以仅使用 fragment 的新对象在抽屉导航中调用这些方法

    @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的 fragment

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 {
}
}

关于java - Android 更好地实现了用于导航的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35624615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com