实际上我是
Android的新手并且陷入了导航抽屉..
我已成功设计了材料设计的导航抽屉及其菜单项.
但是,当我点击该菜单时,它会打开一个独立的片段,而我希望它在Tab Layout中的特定选项卡中打开它.
我用自定义适配器制作了单独的Tab布局. mytablayout .xml文件为
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
android:id="@+id/tabs"
app:tabGravity="fill"
app:tabMode="fixed"
app:elevation="0dp"
android:background="#6ec6c5"
app:tabIndicatorColor="#000000"
app:tabSelectedTextColor="@color/textColor"
app:tabTextColor="#A8DCDC"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
我的Tabfragment类在那里:
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.fragment_tab, null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager(),int_items ));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
}
MyAdapter is :
public class MyAdapter extends FragmentPagerAdapter {
int int_items;
public MyAdapter(FragmentManager fm,int int_items) {
super(fm);
this.int_items = int_items;
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new ProductFragment();
case 1 : return new ProductFragment();
case 2 : return new ProductFragment();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "PRODUCTS";
case 1 :
return "FEATURED";
case 2 :
return "FAVOURITES";
}
return null;
}
}
我的SplitviewActivity显示如下:
public class SplitViewActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private NavigationView navigationView;
FragmentManager mFragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_split_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new TabFragment()).commit();
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
switch (menuItem.getItemId()) {
case R.id.navigation_item_products:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new TabFragment()).commit();
Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
// updateDisplay(new AttachmentFragment());
break;
case R.id.navigation_item_new_Releases:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new ProductFragment()).commit();
/* FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container,new ProductFragment()).commit();*/
Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
// updateDisplay(new ImageFragment());
break;
case R.id.navigation_item_favorites:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, new TabFragment()).commit();
Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
// updateDisplay(new MyLocationFragment());
break;
case R.id.navigation_item_about_us:
Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
// updateDisplay(new MyLocationFragment());
break;
case R.id.navigation_item_notification:
Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
// updateDisplay(new MyLocationFragment());
break;
case R.id.navigation_sub_item_01:
Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 01 Clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.navigation_sub_item_02:
Toast.makeText(SplitViewActivity.this, "Navigation Sub Item 02 Clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的导航菜单item.xml文件如下:
android:id="@+id/navigation_item_products"
android:checked="true"
android:title="Products" />
android:id="@+id/navigation_item_new_Releases"
android:title="New Releases" />
android:id="@+id/navigation_item_favorites"
android:title="favorites" />
android:id="@+id/navigation_item_about_us"
android:title="About Us" />
android:id="@+id/navigation_item_notification"
android:title="Notifications" />
android:id="@+id/navigation_sub_item_01"
android:title="Configure" />
android:id="@+id/navigation_sub_item_02"
android:title="Logout" />
一切都很好,除了.所有三个标签已经显示.导航抽屉也很好用.
只有我无法理解的事情,我怎么能在选项卡上点击导航菜单项打开一个片段.当我点击菜单项时,它打开一个独立的片段不在tab.How我能实现这一点..任何帮助将在先进的..