我在我的应用中遇到了一个非常令人沮丧的错误,让我解释一下: 我有一个带有3个标签的appBar的MainActivity,每个标签显示一个片段。 当我尝试在另一个片段中进行午餐并使其替换所有屏幕时出现问题。新片段显示在替换的片段上,两者可见。
这是我的代码:
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;
}
}}
main_layout:
<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();
}
});
如果有人可以提供帮助,那将是善意的。 感谢。
答案 0 :(得分:1)
您不需要替换像这样的片段
getFragmentManager().beginTransaction().replace(R.id.mainLayout,new DeliverStatsFragment()).commit();
您需要使用viewpager.setCurrentItem()
来更改位置
或强>
将 DeliverStatsFragment 背景颜色设为白色并设置 clickable =“true”