最近在做侧滑菜单优化,结果用了系统自带的NavigationView,样式不太美观,修改UI也不方便。决定用其他方式完成侧滑菜单。下面是用listview完成的自定义菜单。
1.先贴需要的资源图
2. MenuItemAdapter.java
public class MenuItemAdapter extends BaseAdapter { private final int mIconSize; private LayoutInflater mInflater; private Context mContext; public MenuItemAdapter(Context context) { mInflater = LayoutInflater.from(context); mContext = context; mIconSize = DimenUtils.dp2px(mContext, 24); } private List<LvMenuItem> mItems = new ArrayList<LvMenuItem>(); public void setItems(List<LvMenuItem> items) { this.mItems = items; notifyDataSetChanged(); } public void clear(){ if(mItems.size()>0){ this.mItems.clear(); notifyDataSetChanged(); } } @Override public int getCount() { return mItems.size(); } @Override public Object getItem(int position) { return mItems.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getViewTypeCount() { return 3; } @Override public int getItemViewType(int position) { return mItems.get(position).type; } @Override public View getView(int position, View convertView, ViewGroup parent) { LvMenuItem item = mItems.get(position); switch (item.type) { case LvMenuItem.TYPE_NORMAL: if (convertView == null) { convertView = mInflater.inflate(R.layout.design_drawer_item, parent, false); } TextView itemView = (TextView) convertView; itemView.setText(item.name); Drawable icon = mContext.getResources().getDrawable(item.icon); // setIconColor(icon); if (icon != null) { icon.setBounds(0, 0, mIconSize, mIconSize); TextViewCompat.setCompoundDrawablesRelative(itemView, icon, null, null, null); } break; case LvMenuItem.TYPE_NO_ICON: if (convertView == null) { convertView = mInflater.inflate(R.layout.design_drawer_item_subheader, parent, false); } TextView subHeader = (TextView) convertView; subHeader.setText(item.name); break; case LvMenuItem.TYPE_SEPARATOR: if (convertView == null) { convertView = mInflater.inflate(R.layout.design_drawer_item_separator, parent, false); } break; } return convertView; } public void setIconColor(Drawable icon) { int textColorSecondary = android.R.attr.textColorSecondary; TypedValue value = new TypedValue(); if (!mContext.getTheme().resolveAttribute(textColorSecondary, value, true)) { return; } int baseColor = mContext.getResources().getColor(value.resourceId); icon.setColorFilter(baseColor, PorterDuff.Mode.MULTIPLY); } }
3.LvMenuItem.java
public class LvMenuItem { public LvMenuItem(int icon, String name) { this.icon = icon; this.name = name; if (icon == NO_ICON && TextUtils.isEmpty(name)) { type = TYPE_SEPARATOR; } else if (icon == NO_ICON) { type = TYPE_NO_ICON; } else { type = TYPE_NORMAL; } if (type != TYPE_SEPARATOR && TextUtils.isEmpty(name)) { throw new IllegalArgumentException("you need set a name for a non-SEPARATOR item"); } } public LvMenuItem(String name) { this(NO_ICON, name); } public LvMenuItem() { this(null); } private static final int NO_ICON = 0; public static final int TYPE_NORMAL = 0; public static final int TYPE_NO_ICON = 1; public static final int TYPE_SEPARATOR = 2; public int type; public String name; public int icon; }
4.HomeActivity.java
public class HomeActivity extends BaseActivity { private ActionBar mActionbar; private DrawerLayout drawerLayout; private ListView mLvLeftMenu; private View mLeftHeadView; private MenuItemAdapter mMenuItemAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.hm_acti_home, false); initView(); setToolBar(); initLeftDrawer(); refreshMenu(); initUserInfo(); } //记录用户首次点击返回键的时间 private long firstTime = 0; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { if (System.currentTimeMillis() - firstTime > 2000) { Toast.makeText(HomeActivity.this, "再按一次退出程序!", Toast.LENGTH_SHORT).show(); firstTime = System.currentTimeMillis(); } else { finish(); System.exit(0); } return true; } return super.onKeyDown(keyCode, event); } @Override protected void onDestroy() { super.onDestroy(); } /** * 初始化Tab */ private void initView() { drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mLvLeftMenu = (ListView) findViewById(R.id.id_lv_left_menu); } private void setToolBar() { Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mActionbar = getSupportActionBar(); mActionbar.setDisplayHomeAsUpEnabled(true); mActionbar.setHomeAsUpIndicator(R.drawable.cm_ic_menu); mActionbar.setTitle(getString(R.string.app_name)); } private void initLeftDrawer() { LayoutInflater inflater = LayoutInflater.from(this); mMenuItemAdapter = new MenuItemAdapter(this); mLeftHeadView = inflater.inflate(R.layout.cm_nav_header_main, mLvLeftMenu, false); mLvLeftMenu.addHeaderView(mLeftHeadView); mLvLeftMenu.setAdapter(mMenuItemAdapter); } private void refreshMenu() { mMenuItemAdapter.setItems(Arrays.asList( new LvMenuItem(R.drawable.mt_icon_left_day_s, "AAAAAA"), new LvMenuItem(R.drawable.mt_icon_left_history_s, "AAAAAAAAAAAA"), new LvMenuItem(R.drawable.mt_icon_left_file_s, "vvvvv"), new LvMenuItem(R.drawable.mt_icon_left_radio_s, "会议录音"), new LvMenuItem(R.drawable.mt_icon_left_help_s, "帮助中心"))); mLvLeftMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 2: ActivityUtils.showToast(mContext, "help"); break; } drawerLayout.closeDrawers(); } }); } @Override public boolean onOptionsItemSelected(MenuItem item) { // handle item selection switch (item.getItemId()) { case android.R.id.home: //Menu icon drawerLayout.openDrawer(Gravity.LEFT); return true; default: return super.onOptionsItemSelected(item); } } private void initUserInfo() { //退出按钮 TextView txtQuit = (TextView) mLeftHeadView.findViewById(R.id.txt_quit); txtQuit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); CornerImageView imgAvatar = (CornerImageView) mLeftHeadView.findViewById(R.id.img_avatar); imgAvatar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ActivityUtils.gotoActivityForResult(mContext, MySettingActivity.class, RESULT_REFRESH); } }); User user = GLL.get(aContext).getUser(); //用户名字 TextView txtUserName = (TextView) mLeftHeadView.findViewById(R.id.txt_username); txtUserName.setText(user.getUsername()); //用户电话 TextView txtUserTel = (TextView) mLeftHeadView.findViewById(R.id.txt_usertel); txtUserTel.setText(user.getUtel()); //用户头像 if (!TextUtils.isEmpty(user.getAvatar())) { Glide.with(mContext) .load(user.getAvatar()) .placeholder(R.drawable.cm_ic_avatar_default) .into(imgAvatar); } } }
5.hm_acti_home.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <LinearLayout style="@style/pageAllbackground" android:layout_width="fill_parent" android:layout_height="fill_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical"> <include layout="@layout/cm_layout_header" /> </LinearLayout> <ListView android:id="@+id/id_lv_left_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:background="#ffffffff" android:clipToPadding="false" android:divider="@null" android:listSelector="?attr/selectableItemBackground" android:paddingTop="0dp" /> </android.support.v4.widget.DrawerLayout>
6.design_drawer_item.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/listPreferredItemHeightSmall" android:paddingLeft="?attr/listPreferredItemPaddingLeft" android:paddingRight="?attr/listPreferredItemPaddingRight" android:drawablePadding="10dp" android:gravity="center_vertical|start" android:layout_gravity="center" android:maxLines="1" android:textAppearance="?attr/textAppearanceListItem" android:textColor="?android:attr/textColorPrimary" />
7.design_drawer_item_separator.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="?android:attr/listDivider" /> </FrameLayout>
8.design_drawer_item_subheader.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/listPreferredItemHeightSmall" android:gravity="center_vertical|start" android:maxLines="1" android:paddingLeft="?attr/listPreferredItemPaddingLeft" android:paddingRight="?attr/listPreferredItemPaddingRight" android:textAppearance="?attr/textAppearanceListItem" android:textColor="?android:textColorSecondary" />
9.cm_ic_menu.png
以上这些代码即可完成侧滑样式。