我有一个抽屉布局需要有两个导航视图,因为有几个选项需要在顶部对齐,另一个在底部对齐,分隔符将它们分开。
我已经设法使其工作,但现在我的设置有两个不同的问题。
1)第一个,我无法更改所选项目的文本颜色(背景颜色通过抽屉选择器工作,但由于某种原因,所选项目的文本颜色始终为colorPrimary)
2)第二个是比较棘手的,因为有两个导航视图,可能会发生在第一个和第二个上有一个选定的行,即使它们都在同一个容器中加载片段。有没有办法管理它,当选择第一个导航视图的项目时,第二个导航视图的所选项目被取消选择?
这是抽屉的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- 1º Layout de la activity -->
<include layout="@layout/activity_main"/>
<!-- 2º Layout del drawer -->
<android.support.design.widget.NavigationView
android:id="@+id/container_navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemBackground="@drawable/drawer_list_selector"
android:nestedScrollingEnabled="true"
android:scrollIndicators="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/divider_height"
android:background="@color/colorAccent"
android:layout_above="@+id/navigation_view"
android:id="@+id/top_separator"/>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_above="@+id/bottom_separator"
app:itemBackground="@drawable/drawer_list_selector"
app:menu="@menu/menu_navigation" />
<View
android:layout_width="match_parent"
android:layout_height="@dimen/divider_height"
android:background="@color/colorAccent"
android:layout_above="@+id/navigation_bottom"
android:id="@+id/bottom_separator"/>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:menu="@menu/menu_navigation_bottom"
app:itemBackground="@drawable/drawer_list_selector"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
这是顶部导航视图的布局:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_tus_ofertas"
android:title="@string/tus_ofertas"
android:icon="@drawable/ic_offer" />
<item
android:id="@+id/nav_movimiento_puntos"
android:title="@string/movimiento_puntos"
android:icon="@drawable/ic_points"/>
<item
android:id="@+id/nav_asociaciones"
android:title="@string/asociaciones"
android:icon="@drawable/ic_flag"/>
</group>
</menu>
底部导航视图的布局:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_terminos"
android:title="@string/terminos"
android:icon="@drawable/ic_document" />
<item
android:id="@+id/nav_contacto"
android:title="@string/contacto"
android:icon="@drawable/ic_email"/>
<item
android:id="@+id/nav_valorar"
android:title="@string/valorar"
android:icon="@drawable/ic_rate_review"/>
<item
android:id="@+id/nav_exit"
android:title="@string/exit"
android:icon="@drawable/ic_shutdown"/>
</group>
</menu>
所选项目的背景选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/colorAccent" />
<item android:state_activated="true" android:drawable="@color/colorAccent" />
<item android:state_selected="true" android:drawable="@color/colorAccent" />
<item android:state_checked="true" android:drawable="@color/colorAccent" />
<item android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:state_activated="false" android:drawable="@android:color/transparent" />
<item android:state_selected="false" android:drawable="@android:color/transparent" />
<item android:state_checked="false" android:drawable="@android:color/transparent" />
</selector>
调用抽屉并处理事件的活动:
public class MainActivity extends AppCompatActivity {
@Bind(R.id.toolbar) Toolbar toolbar;
@Bind(R.id.tabs) TabLayout tabs;
@Bind(R.id.drawer_layout) DrawerLayout drawerLayout;
@Bind(R.id.container_navigation) NavigationView containerNavigator;
@Bind(R.id.navigation_view) NavigationView navigationView;
@Bind(R.id.navigation_bottom) NavigationView navigationBottom;
@Bind(R.id.vsFooter) ViewStubCompat stub;
ImageView userPicture;
TextView userMail;
ViewPager viewPager;
Menu menu;
ActionBar actionBar;
ViewPagerFragment viewPagerFragment;
Usuario currentUser;
public Usuario getCurrentUser() {
return currentUser;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
ButterKnife.bind(this);
currentUser = UserApiManager.getInstance(getApplicationContext()).getCurrentUser();
viewPagerFragment = ViewPagerFragment.newInstance();
setupToolbar();
setupNavigationDrawer();
ApiManager.getInstance(getApplicationContext()).setupFooter(stub, currentUser.getIdLocalidad(), this);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,viewPagerFragment).commit();
}
public void setupViewPager(ViewPager viewPager) {
tabs.setupWithViewPager(viewPager);
}
private void setupToolbar() {
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
if (actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_black);
actionBar.setTitle(getString(R.string.app_title));
}
}
private void setupNavigationDrawer() {
View headerView = navigationView.inflateHeaderView(R.layout.navigation_header);
userPicture = (ImageView) headerView.findViewById(R.id.user_picture);
userMail = (TextView) headerView.findViewById(R.id.user_mail);
userMail.setText(currentUser.getEmail());
Picasso.with(this).load(currentUser.getImagenURL()).transform(new CircleTransformation()).into(userPicture);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_tus_ofertas:
showTabLayout();
actionBar.setTitle(getString(R.string.app_title));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,viewPagerFragment).commit();
break;
case R.id.nav_movimiento_puntos:
hideTabLayout();
Bundle bundle = new Bundle();
bundle.putString("idcliente", currentUser.getId());
MovimientoPuntosFragment movimientoPuntosFragment = MovimientoPuntosFragment.newInstance();
movimientoPuntosFragment.setArguments(bundle);
actionBar.setTitle(getString(R.string.movimiento_puntos));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, movimientoPuntosFragment).commit();
break;
case R.id.nav_asociaciones:
hideTabLayout();
actionBar.setTitle(getString(R.string.asociaciones));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, AsociacionFragment.newInstance()).commit();
break;
}
drawerLayout.closeDrawers();
return true;
}
});
navigationBottom.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_terminos:
hideTabLayout();
actionBar.setTitle(getString(R.string.terminos));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, LegalesFragment.newInstance()).commit();
break;
case R.id.nav_contacto:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + getString(R.string.contacto_email));
intent.setData(data);
startActivity(intent);
break;
case R.id.nav_valorar:
final String appPackageName = BuildConfig.APPLICATION_ID;
try {
Log.i("url", "market://details?id=" + appPackageName);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
break;
case R.id.nav_exit:
break;
}
drawerLayout.closeDrawers();
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_over, menu);
this.menu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
break;
case R.id.action_user:
Intent intent = new Intent(this, PerfilUsuarioActivity.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
public void hideTabLayout(){
tabs.setVisibility(View.GONE);
}
public void showTabLayout(){
tabs.setVisibility(View.VISIBLE);
}
}
任何帮助将不胜感激! 提前谢谢!
答案 0 :(得分:0)
解决! 颜色的问题我用选择器解决了它,它之前没有用,因为我有一个语法错误(主选择器标签内的选择器标签)。
这是颜色选择器的代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFFFFFFF" android:state_checked="true" />
<item android:color="#E6000000" android:state_checked="false"/>
</selector>
第二个问题我通过编程方式取消选择onNavigationItemSelected上的其他导航视图的项目来解决它。
这是我打电话的功能:
public void deselectItems(NavigationView view){
int size = view.getMenu().size();
for (int i = 0; i < size; i++) {
view.getMenu().getItem(i).setChecked(false);
}
}
我知道必须有更好的方法来解决第二个问题,但这个问题有效。