我的活动包含ViewPager,TabLayout和NavigationDrawer。导航抽屉包含项目(例如10)。当导航抽屉项目被点击时,我还得到FrameLayout来加载片段。
在viewpager中我有3个片段(Say BreakFast,Lunch,Dinner)。在导航抽屉列表项中,我也有与viewpager中相同的项目。
现在,当我点击前三项时,viewpager片段工作正常。点击抽屉中的第四项(即检查卡路里)后,Framelayout的检查卡路里片段已成功加载。现在,当我点击BreakFast,午餐或晚餐项目时,通过替换FrameLayout片段不加载viewpager片段。想出一些解决方案。
这是我的代码
public class HomeScreen扩展了AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
RelativeLayout menuNavigation, menuImageLayout;
DrawerLayout mDrawerLayout;
NavigationListItemAdapter navigationListItemAdapter;
ListView mDrawerList;
ImageView homepageMenu, locationImage;
de.hdodenhof.circleimageview.CircleImageView profileImage;
Button checkout;
TextView title;
FloatingActionButton filterActionButton;
FrameLayout fragmentContainer;
Fragment fragment = null;
boolean doubleBackToExitPressedOnce = false;
int imageArray[] = {R.raw.break_fast_white, R.raw.lunch_white, R.raw.dinner_white, R.raw.calories_white, R.raw.profile_white,
R.raw.about_us_white, R.raw.contact_us_white, R.raw.help_white, R.raw.policy_info_white, R.raw.logout_color};
String titleArray[] = {"Break Fast", "Lunch", "Dinner", "Check Calories", "My Profile", "About Us", "Contact Us", "Help", "Privacy Info", "Logout"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activty_home_screen);
StatusBarTheme.setStatusBarColor(this);
initiateView();
fontStyle();
try {
LunchBoxUtil.setImageDrawable(this, R.raw.menu, homepageMenu);
LunchBoxUtil.setImageDrawable(this, R.raw.filter, filterActionButton);
LunchBoxUtil.setImageDrawable(this, R.raw.location, locationImage);
LunchBoxUtil.setImageDrawable(this, R.raw.photo_circle, profileImage);
title.setText(R.string.app_name);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
navigationListItemAdapter = new NavigationListItemAdapter(getApplicationContext(), titleArray, imageArray);
mDrawerList.setAdapter(navigationListItemAdapter);
menuImageLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(menuNavigation);
if (!drawerOpen) {
mDrawerLayout.openDrawer(menuNavigation);
} else {
mDrawerLayout.closeDrawer(menuNavigation);
}
}
});
filterActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(HomeScreen.this, FilterByCategory.class);
startActivity(intent);
//overridePendingTransition(R.anim.expand_in, R.anim.demo_slide_out);
}
});
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
displayFragments(position);
}
});
checkout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(HomeScreen.this, DemoScreenMain.class);
startActivity(intent);
overridePendingTransition(R.anim.demo_slide_in, R.anim.demo_slide_out);
}
});
if (savedInstanceState == null) {
fragmentContainer = new FrameLayout(this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(menuNavigation)) {
mDrawerLayout.closeDrawer(menuNavigation);
} else {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
}
private void displayFragments(int position) {
switch (position) {
case 0:
mDrawerLayout.closeDrawer(menuNavigation);
fragmentContainer.setVisibility(View.GONE);
viewPager.setCurrentItem(0);
break;
case 1:
mDrawerLayout.closeDrawer(menuNavigation);
fragmentContainer.setVisibility(View.GONE);
viewPager.setCurrentItem(1);
break;
case 2:
mDrawerLayout.closeDrawer(menuNavigation);
fragmentContainer.setVisibility(View.GONE);
viewPager.setCurrentItem(2);
break;
case 3:
mDrawerLayout.closeDrawer(menuNavigation);
fragment = new CalculateCalories();
title.setText(R.string.calories);
filterActionButton.setVisibility(View.GONE);
break;
case 4:
mDrawerLayout.closeDrawer(menuNavigation);
Intent intent = new Intent(this, UserProfile.class);
startActivity(intent);
break;
case 5:
mDrawerLayout.closeDrawer(menuNavigation);
fragment = new AddressDetails();
title.setText(R.string.address_details);
filterActionButton.setVisibility(View.GONE);
break;
case 6:
mDrawerLayout.closeDrawer(menuNavigation);
fragment = new ContactUs();
title.setText(R.string.contactus);
filterActionButton.setVisibility(View.GONE);
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container_fragment, fragment);
fragmentTransaction.commit();
}
}
private void initiateView() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
menuNavigation = (RelativeLayout) findViewById(R.id.relative_navigtion);
menuImageLayout = (RelativeLayout) findViewById(R.id.menu_image_rl);
homepageMenu = (ImageView) findViewById(R.id.home_menu_Img);
locationImage = (ImageView) findViewById(R.id.user_location_Img);
profileImage = (de.hdodenhof.circleimageview.CircleImageView) findViewById(R.id.profile_picture);
filterActionButton = (FloatingActionButton) findViewById(R.id.fab);
fragmentContainer = (FrameLayout) findViewById(R.id.frame_container_fragment);
mDrawerList = (ListView) findViewById(R.id.drawer_menu_items_List);
title = (TextView) findViewById(R.id.home_title_Txt);
viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
checkout = (Button) findViewById(R.id.checkout_Btn);
}
private void fontStyle() {
Typeface robotoRegular = FontUtility.getRobotoRegular(this);
Typeface robotoMedium = FontUtility.getRobotoMedium(this);
title.setTypeface(robotoMedium);
checkout.setTypeface(robotoRegular);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new BreakFastFragment(), "BreakFast");
adapter.addFragment(new LunchFragment(), "Lunch");
adapter.addFragment(new DinnerFragment(), "Dinner");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
这是我的xml布局代码
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/main_lay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/action_bar"
layout="@layout/tool_bar_layout" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/action_bar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabTextAppearance="@style/MyCustomTextAppearance" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/checkout_Btn"
android:layout_below="@+id/tabs" />
<Button
android:id="@+id/checkout_Btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/standard_brown"
android:text="Checkout"
android:textColor="@color/colorAccent"
android:textSize="@dimen/text_size_18sp" />
</RelativeLayout>
<FrameLayout
android:id="@+id/frame_container_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?actionBarSize" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="100dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:visibility="visible"
app:backgroundTint="@color/standard_brown"
app:fabSize="normal" />
</android.support.design.widget.CoordinatorLayout>
<RelativeLayout
android:id="@+id/relative_navigtion"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clickable="false">
<include
android:id="@+id/navgation_drawer"
layout="@layout/navigation_drawer_layout" />
</RelativeLayout>