gpt4 book ai didi

android - 如何在选项卡 Activity 中显示 Fragment

转载 作者:行者123 更新时间:2023-11-30 00:26:02 24 4
gpt4 key购买 nike

我在我的应用程序中遇到了一个非常令人沮丧的错误,让我解释一下:我有一个 MainActivity 和一个包含 3 个选项卡的 appBar,每个选项卡显示一个 fragment 。当我尝试使用另一个 Fragment 并让它替换所有屏幕时,问题就来了。新的 Fragment 显示在被替换的 Fragment 上,并且两个可见。

这是我的代码:

The MainActivity:

public class MainActivity extends AppCompatActivity {

public static Model model;
public static Controller controller;

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;

private static final int PERMISSION_GALERIE = 10;

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

this.model = new Model();
this.controller = new Controller(model,this);

final Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager)findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}

@Override
public boolean onCreateOptionsMenu(android.view.Menu menu) {
getMenuInflater().inflate(R.menu.settings,menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.db_setting) {
getFragmentManager().beginTransaction().replace(R.id.mainLayout, new DBSettingFragment()).commit();
}

return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);



}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode==PERMISSION_GALERIE){
if(grantResults.length==1 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
Snackbar.make(getCurrentFocus(),"Permission accordée",Snackbar.LENGTH_SHORT);
}
}
else{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public android.support.v4.app.Fragment getItem(int position) {
Fragment menusManagementFragment = new MenusManagementFragment();
switch (position) {
case 0:
return menusManagementFragment;
case 1:
OrdersManagementFragment ordersManagementFragment = new OrdersManagementFragment();
return ordersManagementFragment;
case 2:
DeliversManagementFragment deliversManagementFragment = new DeliversManagementFragment();
return deliversManagementFragment;

default: return menusManagementFragment;
}

}

@Override
public int getCount() {
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Gestion des menus";
case 1:
return "Gestion des commandes";
case 2:
return "Gestiond des livreurs";
}
return null;
}
}}

主要布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
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.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

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

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

在 DeliversManagementFragment 中,我这样做:

deliverStatsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getFragmentManager().beginTransaction().replace(R.id.mainLayout,new DeliverStatsFragment()).commit();
}
});

如果有人能提供帮助那就太好了。谢谢。

最佳答案

你不需要像这样替换 fragment

getFragmentManager().beginTransaction().replace(R.id.mainLayout,new DeliverStatsFragment()).commit();

你需要使用viewpager.setCurrentItem()来改变位置

将您的DeliverStatsFragment 背景颜色设置为白色并设置clickable="true"

关于android - 如何在选项卡 Activity 中显示 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45368281/

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