版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、手把手教你打造一个Material Design风格的App(三)3.2添加抽屉导航添加导航抽屉跟Android 5.0之前是一样的,只是以前我们使用ListView来作为菜单容器,现在我们则使用Material Design风格的RecyclerView。(14)在你工程的Java文件夹中,创建3个名为activity、adapter、model的包,将MainActivity.java移到activtiy包中,这样做使得你的代码可以很好地组织和管理。(15)打开位于app模块下的build.gradle文件并添加如下依赖。添加完依赖之后,点击Build-Rebuild Project下载所
2、需要的类库。html view plain copybuild.gradle dependencies compile fileTree(dir: libs, include: *.jar) compile com.android.support:appcompat-v7:22.0.0 compile com.android.support:recyclerview-v7:21.0.+ (16)在model包里,创建名为NavDrawerItem.java的类,然后添加以下代码。这个模型类是一个简单的POJO(Plain Oridinary Java Object即简单的java对象)类,它定
3、义了导航抽屉的菜单项。java view plain copyNavDrawerItem.java package .materialdesign.model; public class NavDrawerItem private boolean showNotify; private String title; public NavDrawerItem() public NavDrawerItem(boolean showNotify, String title) this.showNotify = showNotify; this.title = title
4、; public boolean isShowNotify() return showNotify; public void setShowNotify(boolean showNotify) this.showNotify = showNotify; public String getTitle() return title; public void setTitle(String title) this.title = title; (17)在res-layout下,创建一个名为nav_drawer_row.xml的文件并添加如下代码。这个布局渲染了导航抽屉菜单的每一行,如果你想自定义抽屉
5、菜单项,你需要修改这个布局文件。在这个例子中,它仅包含一个TextView。html view plain copynav_drawer_row.xml android:layout_width=match_parent android:layout_height=wrap_content android:clickable=true (18)下载个人资料图标并将它放入drawable文件夹,这一步是可选的,但是这个图标会在导航抽屉的头部用到。(19)创建另一个名为fragment_navigation_drawer.xml的布局文件并添加如下代码。这个布局文件渲染了整个导航抽屉的视图,这个布
6、局包括一个显示个人信息的头部和一个显示列表的RecycleView。html view plain copyfragment_navigation_drawer.xml android:layout_width=match_parent android:layout_height=match_parent android:background=android:color/white (20)因为RecycleView是自定义的,我们还需要一个adapter来渲染自定义的xml布局,所以在adapter包下面,创建一个名为NavgationDrawerAdapter.java的类并添加如下代码。
7、这个适配器类inflate了nav_drawer_row.xml并渲染了RecycleView抽屉菜单。java view plain copyimport android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.Collect
8、ions; import java.util.List; /* * Created by Ravi Tamada on 12-03-2015. */ public class NavigationDrawerAdapter extends RecyclerView.Adapter List data = Collections.emptyList(); private LayoutInflater inflater; private Context context; public NavigationDrawerAdapter(Context context, List data) this.
9、context = context; inflater = LayoutInflater.from(context); this.data = data; public void delete(int position) data.remove(position); notifyItemRemoved(position); Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) View view = inflater.inflate(R.layout.nav_drawer_row, par
10、ent, false); MyViewHolder holder = new MyViewHolder(view); return holder; Override public void onBindViewHolder(MyViewHolder holder, int position) NavDrawerItem current = data.get(position); holder.title.setText(current.getTitle(); Override public int getItemCount() return data.size(); class MyViewH
11、older extends RecyclerView.ViewHolder TextView title; public MyViewHolder(View itemView) super(itemView); title = (TextView) itemView.findViewById(R.id.title); (21)在activity包下,创建一个名为FragmentDrawer.java的Fragment。在Android Studio里面,按以下操作来创建一个新的Fragment,在activity包上右键-New-Fragment-Fragment(Blank),然后给Frag
12、ment命名。java view plain copyFragmentDrawer.java import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.s
13、upport.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.GestureDetector; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import and
14、roid.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class FragmentDrawer extends Fragment private static String TAG = FragmentDrawer.class.getSimpleName(); private RecyclerView recyclerView; private ActionBarDrawerToggle mDrawerToggle;
15、private DrawerLayout mDrawerLayout; private NavigationDrawerAdapter adapter; private View containerView; private static String titles = null; private FragmentDrawerListener drawerListener; public FragmentDrawer() public void setDrawerListener(FragmentDrawerListener listener) this.drawerListener = li
16、stener; public static List getData() List data = new ArrayList(); / preparing navigation drawer items for (int i = 0; i titles.length; i+) NavDrawerItem navItem = new NavDrawerItem(); navItem.setTitle(titlesi); data.add(navItem); return data; Override public void onCreate(Bundle savedInstanceState)
17、super.onCreate(savedInstanceState); / drawer labels titles = getActivity().getResources().getStringArray(R.array.nav_drawer_labels); Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) / Inflating view layout View layout = inflater.inflate(R.lay
18、out.fragment_navigation_drawer, container, false); recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList); adapter = new NavigationDrawerAdapter(getActivity(), getData(); recyclerView.setAdapter(apter); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(); recyclerView.add
19、OnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() Override public void onClick(View view, int position) drawerListener.onDrawerItemSelected(view, position); mDrawerLayout.closeDrawer(containerView); Override public void onLongClick(View view, int position
20、) ); return layout; public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) containerView = getActivity().findViewById(fragmentId); mDrawerLayout = drawerLayout; mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string
21、.drawer_close) Override public void onDrawerOpened(View drawerView) super.onDrawerOpened(drawerView); getActivity().invalidateOptionsMenu(); Override public void onDrawerClosed(View drawerView) super.onDrawerClosed(drawerView); getActivity().invalidateOptionsMenu(); Override public void onDrawerSlid
22、e(View drawerView, float slideOffset) super.onDrawerSlide(drawerView, slideOffset); toolbar.setAlpha(1 - slideOffset / 2); ; mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerLayout.post(new Runnable() Override public void run() mDrawerToggle.syncState(); ); public static interface ClickListene
23、r public void onClick(View view, int position); public void onLongClick(View view, int position); static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener private GestureDetector gestureDetector; private ClickListener clickListener; public RecyclerTouchListener(Context context,
24、 final RecyclerView recyclerView, final ClickListener clickListener) this.clickListener = clickListener; gestureDetector=newGestureDetector(context,new GestureDetector.SimpleOnGestureListener() Override public boolean onSingleTapUp(MotionEvent e) return true; Override public void onLongPress(MotionE
25、vent e) View child = recyclerView.findChildViewUnder(e.getX(), e.getY(); if (child != null & kListener != null) clickListener.onLongClick(child, recyclerView.getChildPosition(child); ); Override public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) View child = rv.findChildViewUnder(e
26、.getX(), e.getY(); if (child != null & clickListener != null & gestureDetector.onTouchEvent(e) clickListener.onClick(child, rv.getChildPosition(child); return false; Override public void onTouchEvent(RecyclerView rv, MotionEvent e) public interface FragmentDrawerListener public void onDrawerItemSele
27、cted(View view, int position); (22)最后打开主Activity的布局文件(activity_main.xml)并按下面所示来修改布局。在这个布局里面,我们添加了android.support.v4.widget.DrawerLayout来展示导航抽屉菜单。当然在标签里,你需要给出fragmentDrawer正确的路径。html view plain copyactiivty_main.xml android:id=+id/drawer_layout android:layout_width=match_parent android:layout_height=
28、match_parent 现在所有的布局文件和java类文件都准备到位了,让我们对MainActivity做一些修改来使导航抽屉工作起来。(23)打开MainActivity.java文件并做如下修改。实现FragmentDrawer.FragmentDrawerListener接口并添加onDrawerItemSelected()重载。java view plain copyMainActivity.java import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarActivi
29、ty; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; publicclassMainActivityextendsActionBarActivityimplements FragmentDrawer.FragmentDrawerListener private Toolbar mToolbar; private FragmentDrawer drawerFragment; Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(mToolbar); getSupportActi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二四年度医院安保服务承包合同2篇
- 2024年度广告发布合同:规定广告内容、投放范围与费用3篇
- 2024年养老院膳食供应协议3篇
- 2024儿童游乐场租赁经营合同
- 2024年办公家具定制购销合同3篇
- 2024年度航空货物快递委托合同版B版
- 2024年度模具更新与调整合同合同书一
- 2024年度物业装饰施工合作合同版B版
- 建筑监理安全合同范本
- 二零二四年度健身房会员健康保险合作合同3篇
- 专为装配式建筑施工而生的伸缩式卸料平台
- 十字相乘法因式分解
- 文化创意产品设计及案例PPT完整全套教学课件
- 宠物血液生化检验及其意义课件
- DB37-T 4502-2022-2022滤水模压混凝土板现场制作质量控制规范
- 人工智能(全套课件)
- 保洁员一日工作流程及要求(教学楼)
- 超星尔雅《艺术美学》答案大全
- 三年级上册数学教案 8.1 等量代换 北京版
- 回弹法检测烧结普通砖抗压强度计算表JCT7961999及DGTJ088042005
- 四年级上册美术课件第6课 汉字变成画|沪教版
评论
0/150
提交评论