Android侧滑菜单的实现(1)

public void onDrawerClosed(View view) {

getActionBar().setTitle(mTitle);

invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

}

public void onDrawerOpened(View drawerView) {

getActionBar().setTitle(mDrawerTitle);

invalidateOptionsMenu(); // creates call to

onPrepareOptionsMenu()

}

};

mDrawerLayout.setDrawerListener(mDrawerToggle);

drawerLayout左侧菜单(或者右侧)的展开与隐藏可以被DrawerLayout.DrawerListener的实现监听到,这样你就可以在菜单展开与隐藏反生的时刻做一些希望做的事情,比如更新actionbar菜单等。如果你的activity有actionbar的话,还是建议用ActionBarDrawerToggle来监听,ActionBarDrawerToggle实现了DrawerListener,所以他能做DrawerListener可以做的任何事情,同时他还能将drawerLayout的展开和隐藏与actionbar的app 图标关联起来,当展开与隐藏的时候图标有一定的平移效果,点击图标的时候还能展开或者隐藏菜单。

3)如何在菜单展开或者隐藏的时候更新activity的menu

在监听的回调方法中我们用invalidateOptionsMenu通知activity重绘menu,然后activity就有机会在onPrepareOptionsMenu方法中更新menu元素的显示与隐藏。

/* Called whenever we call invalidateOptionsMenu() */

@Override

public boolean onPrepareOptionsMenu(Menu menu) {

// If the nav drawer is open, hide action items related to the content view

boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);

return super.onPrepareOptionsMenu(menu);

}

4)如何让app图标点击的时候能够展开或者隐藏侧边菜单

Activity中使用ActionBarDrawerToggle的onOptionsItemSelected方法。

@Override

public booleanonOptionsItemSelected(MenuItem item) {

// The action bar home/up actionshould open or close the drawer.

// ActionBarDrawerToggle will takecare of this.

if(mDrawerToggle.onOptionsItemSelected(item)) {

return true;

}

……….//处理其他菜单点击事件

returnsuper.onOptionsItemSelected(item);

}

Drawerlayout完整实例(源于官网demo)

Activity:

/*

  • Copyright 2013 The Android Open Source Project

  • Licensed under the Apache License, Version 2.0 (the “License”);

  • you may not use this file except in compliance with the License. * You may obtain a copy of the License at

  • http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software

  • distributed under the License is distributed on an “AS IS” BASIS,

  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  • See the License for the specific language governing permissions and

  • limitations under the License.

*/

package com.example.android.navigationdrawerexample;

import java.util.Locale;

import android.app.Activity;

import android.app.Fragment;

import android.app.FragmentManager;

import android.app.SearchManager;

import android.content.Intent;

import android.content.res.Configuration;

import android.os.Bundle;

import android.support.v4.app.ActionBarDrawerToggle;

import android.support.v4.view.GravityCompat;

import android.support.v4.widget.DrawerLayout;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.Toast;

public class MainActivity extends Activity {

private DrawerLayout mDrawerLayout;

private ListView mDrawerList;

private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;

private CharSequence mTitle;

private String[] mPlanetTitles;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mTitle = mDrawerTitle = getTitle();

mPlanetTitles = getResources().getStringArray(R.array.planets_array);

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

mDrawerList = (ListView) findViewById(R.id.left_drawer);

// set a custom shadow that overlays the main content when the drawer opens

mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

// set up the drawer’s list view with items and click listener

mDrawerList.setAdapter(new ArrayAdapter(this, R.layout.drawer_list_item, mPlanetTitles));

mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

// enable ActionBar app icon to behave as action to toggle nav drawer

getActionBar().setDisplayHomeAsUpEnabled(true);

getActionBar().setHomeButtonEnabled(true);

// ActionBarDrawerToggle ties together the the proper interactions

// between the sliding drawer and the action bar app icon

mDrawerToggle = new ActionBarDrawerToggle(

this, /* host Activity */

mDrawerLayout, /* DrawerLayout object */

R.drawable.ic_drawer, /* nav drawer image to replace ‘Up’ caret */

R.string.drawer_open, /* “open drawer” description for accessibility */

R.string.drawer_close /* “close drawer” description for accessibility */

) {

public void onDrawerClosed(View view) {

getActionBar().setTitle(mTitle);

invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

}

public void onDrawerOpened(View drawerView) {

getActionBar().setTitle(mDrawerTitle);

invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

}

};

mDrawerLayout.setDrawerListener(mDrawerToggle);

if (savedInstanceState == null) {

selectItem(0);

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater();

inflater.inflate(R.menu.main, menu);

return super.onCreateOptionsMenu(menu);

}

/* Called whenever we call invalidateOptionsMenu() */

@Override

public boolean onPrepareOptionsMenu(Menu menu) {

// If the nav drawer is open, hide action items related to the content view

boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);

return super.onPrepareOptionsMenu(menu);

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// The action bar home/up action should open or close the drawer.

// ActionBarDrawerToggle will take care of this.

if (mDrawerToggle.onOptionsItemSelected(item)) {

return true;

}

// Handle action buttons

switch(item.getItemId()) {

case R.id.action_websearch:

// create intent to perform web search for this planet

Intent intent = new

Intent(Intent.ACTION_WEB_SEARCH);

intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());

// catch event that there’s no activity to handle intent

if (intent.resolveActivity(getPackageManager()) != null) {

startActivity(intent);

} else {

Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();

}

return true;

default:

return super.onOptionsItemSelected(item);

}

}

/* The click listner for ListView in the navigation drawer */

private class DrawerItemClickListener implements ListView.OnItemClickListener {

@Override

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

selectItem(position);

}

}

private void selectItem(int position) {

// update the main content by replacing fragments

Fragment fragment = new PlanetFragment();

Bundle args = new Bundle();

args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);

fragment.setArguments(args);

FragmentManager fragmentManager = getFragmentManager

fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

// update selected item and title, then close the drawer

mDrawerList.setItemChecked(position, true);

setTitle(mPlanetTitles[position]);

mDrawerLayout.closeDrawer(mDrawerList);

}

@Override

public void setTitle(CharSequence title) {

mTitle = title;

getActionBar().setTitle(mTitle);

}

/**

  • When using the ActionBarDrawerToggle, you must call it during

  • onPostCreate() and onConfigurationChanged()… */

@Override

protected void onPostCreate(Bundle savedInstanceState) {

super.onPostCreate(savedInstanceState);

// Sync the toggle state after onRestoreInstanceState has occurred.

mDrawerToggle.syncState();

}

@Override

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

// Pass any configuration change to the drawer toggls

mDrawerToggle.onConfigurationChanged(newConfig);

}

/** * Fragment that appears in the “content_frame”, shows a planet */

public static class PlanetFragment extends Fragment {

public static final String ARG_PLANET_NUMBER = “planet_number”;

public PlanetFragment() {

// Empty constructor required for fragment subclasses

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_planet, container, false);

int i = getArguments().getInt(ARG_PLANET_NUMBER);

String planet = getResources().getStringArray(R.array.planets_array)[i];

int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), “drawable”, getActivity().getPackageName());

((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);

getActivity().setTitle(planet); return rootView;

}

}

}

activity_main.xml

<android.support.v4.widget.DrawerLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

android:id=“@+id/drawer_layout”

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<FrameLayout

android:id=“@+id/content_frame”

android:layout_width=“match_parent”

android:layout_height=“match_parent” />

<ListView

android:id=“@+id/left_drawer”

android:layout_width=“240dp”

android:layout_height=“match_parent”

android:layout_gravity=“start”

android:choiceMode=“singleChoice”

android:divider=“@android:color/transparent”

android:dividerHeight=“0dp”

android:background=“#111”/>

</android.support.v4.widget.DrawerLayout>

fragment_planet.xml

<ImageView xmlns:android=“http://schemas.android.com/apk/res/android”

android:id=“@+id/image”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background=“#000000”

总结

这次面试问的还是还是有难度的,要求当场写代码并且运行,也是很考察面试者写代码
因为Android知识体系比较庞大和复杂的,涉及到计算机知识领域的方方面面。在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)

里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。**

附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-mpBN0wHk-1714362487242)]
里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值