更新的答案:
使用支持库23.1.1或更高版本的app:actionLayout
将支持以下自定义布局。
创建您的自定义计数器布局,如下所示。
menu_counter.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
在xml中的抽屉菜单项中引用它。
menu/drawer.xml:
<item
android:id="@+id/navigation_drawer_item_1"
android:icon="@drawable/ic_menu_1"
android:title="@string/navigation_drawer_item_1"
app:actionLayout="@layout/menu_counter"
/>
请注意,您应该使用app
命名空间,而不要尝试使用android
。
或者,您可以使用MenuItem.setActionView()
方法手动设置操作视图。
查找菜单项并按照以下代码设置计数器:
private void setMenuCounter(@IdRes int itemId, int count) {
TextView view = (TextView) navigationView.getMenu().findItem(itemId).getActionView();
view.setText(count > 0 ? String.valueOf(count) : null);
}
请注意,如果你需要支持 Android 2.x
版本,你将需要使用 MenuItemCompat
。
之前的答案(对于较旧的版本):
可以通过以下代码在 NavigationView
中使用 ListView
解决问题...
<android.support.design.widget.NavigationView
android:id="@+id/my_courses_nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="150dp" >
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:cacheColorHint="@android:color/transparent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</FrameLayout>
</android.support.design.widget.NavigationView>
在您的活动设置列表项中,如下所示...
private final String[] mMenuTitles = { getResources().getString(R.string.bookmarks), getResources().getString(R.string.alerts), getResources().getString(R.string.settings) };
private final int[] mMenuIconId = { R.drawable.ic_drawer_bookmarks, R.drawable.ic_drawer_alerts, R.drawable.ic_drawer_settings };
ListView mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
private ArrayList<SlideMenuItem> drawerItemList = new ArrayList<SlideMenuItem>();
for( int i = 0; i < mMenuTitles.length; i++ ) {
SlideMenuItem item = new SlideMenuItem();
item.setTitle(mMenuTitles[i]);
item.setIconID(mMenuIconId[i]);
drawerItemList.add(item);
}
MenuAdapter mMenuAdapter = new MenuAdapter( MyCoursesActivity.this, R.layout.drawer_list_item, drawerItemList);
mDrawerList.setAdapter(mMenuAdapter);
导航抽屉中ListView的单击监听器...
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
mDrawerLayout.closeDrawers();
SlideMenuItem item = (SlideMenuItem) parent.getItemAtPosition(position);
switch (item.getIconId()) {
case R.drawable.ic_drawer_bookmarks: {
}
break;
case R.drawable.ic_drawer_alerts: {
}
break;
case R.drawable.ic_drawer_settings: {
}
break;
default: {
}
break;
}
} catch (Exception e) {
}
}
}
MenuAdapter..java
public class MenuAdapter extends ArrayAdapter<SlideMenuItem> {
private Activity activity;
private List<SlideMenuItem> itemList;
private SlideMenuItem item;
private int row;
public MenuAdapter(Activity act, int resource, List<SlideMenuItem> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.itemList = arrayList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
holder = new ViewHolder();
holder.tvTitle = (TextView) view.findViewById(R.id.menu_title);
holder.imgView = (ImageView) view.findViewById(R.id.menu_icon);
holder.tvUnread = (TextView) view.findViewById(R.id.unread_count);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if ((itemList == null) || ((position + 1) > itemList.size()))
return view;
item = itemList.get(position);
holder.tvTitle.setText(item.getTitle());
holder.imgView.setImageResource(item.getIconId());
if( item.getUnreadCount() > 0 ) {
holder.tvUnread.setVisibility(View.VISIBLE);
holder.tvUnread.setText(item.getUnread());
if( MyCoursesActivity.DRAWER_MENU_ALERTS_POSITION == position ) {
holder.tvUnread.setBackgroundResource(R.drawable.round_unread_count_bg_red);
}
else {
holder.tvUnread.setBackgroundResource(R.drawable.round_unread_count_bg_green);
}
}
else {
holder.tvUnread.setVisibility(View.GONE);
}
return view;
}
public class ViewHolder {
public TextView tvTitle;
public ImageView imgView;
public TextView tvUnread;
}
}
抽屉列表项.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/drawar_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/menu_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:gravity="center_vertical"
android:src="@drawable/ic_drawer" />
<TextView
android:id="@+id/menu_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/unread_count"
android:layout_toRightOf="@+id/menu_icon"
android:minHeight="?attr/listPreferredItemHeightSmall"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="About Us"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/unread_count"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:gravity="center"
android:text="99+"
android:textColor="@android:color/white"
android:textSize="10sp"
android:visibility="gone" />
SlideMenuItem.java
public class SlideMenuItem {
private Bitmap icon;
private String title;
private String unread;
private int iconID;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Bitmap getIcon() {
return icon;
}
public void setIcon(Bitmap icon) {
this.icon = icon;
}
public int getIconId() {
return iconID;
}
public void setIconID(int icon) {
this.iconID = icon;
}
public String getUnread() {
return unread;
}
public int getUnreadCount() {
int count = Flinnt.INVALID;
try {
if( null != unread ) {
count = Integer.parseInt(unread);
}
} catch (Exception e) {
}
return count;
}
public void setUnread(String unread) {
this.unread = unread;
}
}
NavigationView
来实现这个功能?在您提供的链接中没有使用NavigationView
。 - Priyank Patel