1
私のアプリでかなり不満なバグに直面しています。 私は3つのタブを含むappBarでMainActivityを持っており、各タブにはフラグメントが表示されます。 問題は、別の断片を昼食し、すべての画面を置き換えるときに発生します。新しいフラグメントが置換されたフラグメントに表示され、2つが表示されます。ここでタブにフラグメントを表示する方法アクティビティ
は私のコードです:
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();
}
});
誰かがそれは次のようになります助けることができる場合種類。おかげさまで
こんにちはRahulとあなたの返事をありがとう。 私はタブバーを含むすべての画面ビューを置き換えたいと思います。 – OTmn
OK、** DeliverStatsFragment **背景色を白く設定してください** clickable = "true" ** –
それは賢いです。私はこれを試してみましょう。どうしてクリック可能をtrueに設定する必要がありますか? – OTmn