Android侧滑菜单的实现,Android高级面试题

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip204888 (备注Android)
img

正文

1.DrawerLayout最好为界面的根布局,官网是这样说的,否则可能会出现触摸事件被屏蔽的问题;

2.主内容区的布局代码要放在侧滑菜单布局的前面, 因为 XML 顺序意味着按 z 序(层叠顺序)排序,并且抽屉式导航栏必须位于内容顶部;

3.侧滑菜单部分的布局(这里是ListView)必须设置layout_gravity属性,他表示侧滑菜单是在左边还是右边,而且如果不设置在打开关闭抽屉的时候会报错,设置了layout_gravity="start/left"的视图才会被认为是侧滑菜单。

2)建议用ActionBarDrawerToggle来监听

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);

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希望=技能+面试

  • 技能
  • 面试技巧+面试题

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

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
img

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

那么对于想坚持程序员这行的真的就一点希望都没有吗?
其实不然,在互联网的大浪淘沙之下,留下的永远是最优秀的,我们考虑的不是哪个行业差哪个行业难,就逃避掉这些,无论哪个行业,都会有他的问题,但是无论哪个行业都会有站在最顶端的那群人。我们要做的就是努力提升自己,让自己站在最顶端,学历不够那就去读,知识不够那就去学。人之所以为人,不就是有解决问题的能力吗?挡住自己的由于只有自己。
Android希望=技能+面试

  • 技能
    [外链图片转存中…(img-RwUFOWge-1713709888711)]
  • 面试技巧+面试题
    [外链图片转存中…(img-ltqgqiYL-1713709888711)]

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

需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-OFT6QASs-1713709888711)]

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

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当涉及到Android面试时,高级级别的问通常会涉及更深入的主和技术。以下是一些可能的高级Android面试及其答案: 1. 请解释一下Android中的四大组件。 答:Android中的四大组件是Activity、Service、BroadcastReceiver和ContentProvider。Activity用于用户界面的展示和交互,Service用于在后台执行长时间运行的任务,BroadcastReceiver用于接收和处理系统广播消息,ContentProvider用于数据共享和访问。 2. 请解释一下Android中的MVC、MVP和MVVM架构模式。 答:MVC(Model-View-Controller)是一种软件设计模式,将应用程序分为模型(数据)、视图(用户界面)和控制器(逻辑处理)三个部分。MVP(Model-View-Presenter)是MVC的变体,将控制器替换为Presenter,使视图和模型之间的耦合更低。MVVM(Model-View-ViewModel)是一种新的架构模式,通过数据绑定实现视图和模型之间的通信。 3. 请解释一下Android中的内存泄漏以及如何避免。 答:内存泄漏是指应用程序中未使用的内存无法被垃圾回收器回收,导致内存占用不断增加。在Android中,常见的内存泄漏原因包括静态引用、匿名内部类、Handler引起的内存泄漏等。避免内存泄漏的方法包括及时释放资源、避免使用静态引用、避免匿名内部类等。 4. 请解释一下Android中的线程和线程间通信方式。 答:在Android中,可以使用Thread类或者HandlerThread类创建线程。线程间通信的方式包括使用Handler进行消息传递、使用AsyncTask进行异步任务处理、使用BroadcastReceiver进行广播消息传递等。 5. 请解释一下Android中的性能优化技巧。 答:Android性能优化的技巧包括减少内存使用、优化布局和绘制、使用合适的数据结构和算法、避免频繁的IO操作、使用缓存等。此外,还可以使用工具如Lint、TraceView和HierarchyViewer进行性能分析和调优。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值