您好我使用View Pager实现了Tab布局。我有七个片段实现说Frag1,Frag 2 ... Frag 7对应Tab 1,Tab 2 .... Tab7。我还实现了抽屉布局。导航也有相应的7项我的要求如下:
当我在标签1中并拉出抽屉时,必须突出显示导航抽屉中的第一个项目。当我在标签5中时,必须突出显示第5项,依此类推。
我尝试过看到一些教程,但无法完成。
我的activity_main.xml
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="scrollable"
app:tabTextAppearance="@style/TabTextAppearance"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navheader"
app:menu="@menu/menu_navigation" />
My Main Activity.Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Adding Toolbar to Main screen
mHandler = new Handler();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Setting ViewPager for each Tabs
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// Set Tabs inside Toolbar
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
// Create Navigation drawer and inlfate layout
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
// Adding menu icon to Toolbar
ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
VectorDrawableCompat indicator =
VectorDrawableCompat.create(getResources(), R.drawable.ic_menu, getTheme());
indicator.setTint(ResourcesCompat.getColor(getResources(), R.color.white, getTheme()));
supportActionBar.setHomeAsUpIndicator(indicator);
supportActionBar.setDisplayHomeAsUpEnabled(true);
}
// Set behavior of Navigation drawer
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
// public boolean onNavigationItemSelected(MenuItem menuItem) {
// // Set item in checked state
// menuItem.setChecked(true);
//
// // TODO: handle navigation
//
// // Closing drawer on item click
// mDrawerLayout.closeDrawers();
// return true;
// }
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.nav_home:
navItemIndex = 0;
CURRENT_TAG = TAG_HOME;
break;
case R.id.nav_history:
navItemIndex = 1;
CURRENT_TAG = TAG_HISTORY;
break;
case R.id.nav_location:
navItemIndex = 2;
CURRENT_TAG = TAG_LOCATION;
break;
case R.id.nav_developments:
navItemIndex = 3;
CURRENT_TAG = TAG_DEVELOPMENTS;
break;
case R.id.nav_donations:
navItemIndex = 4;
CURRENT_TAG = TAG_DONATIONS;
break;
case R.id.nav_resources:
navItemIndex = 5;
CURRENT_TAG = TAG_RESOURCES;
break;
case R.id.nav_feedback:
navItemIndex = 6;
CURRENT_TAG = TAG_FEEDBACK;
break;
case R.id.nav_contactus:
navItemIndex = 7;
CURRENT_TAG = TAG_CONTACTUS;
break;
default:
navItemIndex = 0;
}
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) {
menuItem.setChecked(false);
} else {
menuItem.setChecked(true);
}
menuItem.setChecked(true);
mDrawerLayout.closeDrawers();
viewPager.setCurrentItem(navItemIndex);
return true;
}
});
请帮助我实现这一目标。
答案 0 :(得分:1)
使用Tablayout.addOnTabSelectedListener
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
int position = tab.getPosition();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
从此代码中,您将获得所选的标签位置。
现在使用NavigationView
中的位置值并使用索引
navigationView.getMenu().getItem(position).setChecked(true);