두 탭을 구현하려고하는데 탐색 서랍도 있습니다. 나는 두 가지를 따로 할 수 있지만 두 가지를 결합하려고 할 때 나는 문제를 겪는다. 원래는 MainActivity 내에서 두 개의 탭과 탐색 서랍을 생성하는 코드가 있었지만 그 당시에는 Fragment Activity 였지만 Nav 서랍에 Home 옵션이있는 것이 명확 해지면 해당 코드를 리팩터링하기로 결정했습니다. 이전과 같이 두 조각으로 채워진 탭이있는 조각으로 탭을 옮기십시오.탐색 서랍 항목은 탭이있는 조각입니다.
시작시에는 괜찮은 것처럼 보이지만 탐색 서랍에서 홈 옵션을 클릭하면 각 탭의 조각이 제거 된 것처럼 보이며 옵션을 클릭 할 때마다 두 개의 탭이 추가됩니다.
private void selectItem(int position) {
Fragment fragment = new NullFragment();
switch (position){
case 0:
fragment = new HomeFragment();
break;
case 1:
//Placeholder
fragment = new PlanetFragment();
}
// update the main content by replacing fragments
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
drawerListView.setItemChecked(position, true);
setTitle(drawerItemTitles[position]);
drawerLayout.closeDrawer(drawerListView);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
HomeFragment.java
public class HomeFragment extends Fragment implements ActionBar.TabListener {
AppSectionsPagerAdapter appSectionsPagerAdapter;
ViewPager viewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = null;
/*if(savedInstanceState == null){*/
rootView = inflater.inflate(R.layout.activity_home, container, false);
appSectionsPagerAdapter = new AppSectionsPagerAdapter(getFragmentManager());
final ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
viewPager.setAdapter(appSectionsPagerAdapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(
actionBar.newTab()
.setIcon(R.drawable.ic_sync_to_cloud)
.setTabListener(this));
actionBar.addTab(
actionBar.newTab()
.setIcon(R.drawable.ic_send_to_device)
.setTabListener(this));
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
return rootView;
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
내가 HomeFragment의 새로운 인스턴스마다 작성되어 레이아웃에있는 유일한 것은이 ViewPager 때문에이 가정. 이 작업을 수행하는 새로운 방법이 없으므로 조각을 저장하고 복원 할 수 있습니다. 더 많은 코드가 필요한지 알려주십시오.
된다! 고마워, 너 한테 빚 졌어. – Unem
안녕하세요 @ntrrobng 나는 (문제 1) 같은 문제가 있지만 ActionBar 대신 탭 레이아웃을 사용하면 제발 나를 도울 수 있습니다 – angel