版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、手把手教你打造一个Material Design风格的App(四)3.3实现导航抽屉菜单项的选择尽管导航抽屉已经实现了,但是你会发现选择抽屉列表项并没有反应,这是因为我们还没有实现RecycleView items的点击监听。因为我们在导航抽屉里有3个菜单(Home,Friends & Messages),所以需要为每一个菜单项创建一个独立的Fragment。(24)在res->layout里面,创建一个名为fragment_home.xml的文件并添加如下代码。html view plain copyfragment_home.xml <RelativeLayout xm
2、lns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".materialdesign.activity.HomeFragment"> <TextView android:id="+id/label"
3、; android:layout_alignParentTop="true" android:layout_marginTop="100dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="45dp" android:text="HOME" androi
4、d:textStyle="bold"/> <TextView android:layout_below="id/label" android:layout_centerInParent="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="12dp" android:layout_marginTop="10dp&qu
5、ot; android:gravity="center_horizontal" android:text="Edit fragment_home.xml to change the appearance" /> </RelativeLayout> (25)在activity包下,创建一个名为HomeFragment.java的类,并添加如下代码。java view plain copyHomeFragment.java import android.app.Activity; import android.os.Bundle; impo
6、rt android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class HomeFragment extends Fragment public HomeFragment() / Required empty public constructor Override public void onCreate(Bundle savedInstanceState) super.onCreat
7、e(savedInstanceState); Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bndle savedInstanceState) View rootView = inflater.inflate(R.layout.fragment_home, container, false); / Inflate the layout for this fragment return rootView; Override public void onAttach(Activity
8、activity) super.onAttach(activity); Override public void onDetach() super.onDetach(); (26)同样,创建FriendsFragment.Java和MessagesFragment.java两个fragment类,并创建fragment_friends.xml和fragment_messages.xml布局文件,代码请参考前两步。(27)打开MainActivity.java并作如下修改。>displayView()方法会显示当导航菜单选中时对应的fragment视图,这个方法应该在某个导航菜单选中时,在
9、onDrawerItemSelected()中调用,来展示对应的视图。java view plain copyMainActivity.java import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.widget.DrawerLayout; import android.s
10、upport.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; publicclassMainActivityextendsActionBarActivityimplements FragmentDrawer.FragmentDrawerListener private static Str
11、ing TAG = MainActivity.class.getSimpleName(); 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); setS
12、upportActionBar(mToolbar); getSupportActionBar().setDisplayShowHomeEnabled(true); drawerFragment = (FragmentDrawer) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mT
13、oolbar); drawerFragment.setDrawerListener(this); / display the first navigation drawer view on app launch displayView(0); Override public boolean onCreateOptionsMenu(Menu menu) / Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu);
14、return true; Override public boolean onOptionsItemSelected(MenuItem item) / Handle ion bar item clicks here. The action bar will / automatically handle clicks on the Home/Up button, so long / as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); /noinspection Simplifiab
15、leIfStatement if (id = R.id.action_settings) return true; if(id = R.id.action_search) Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show(); return true; return super.onOptionsItemSelected(item); Override public void onDrawerItemSelected(View view
16、, int position) displayView(position); private void displayView(int position) Fragment fragment = null; String title = getString(R.string.app_name); switch (position) case 0: fragment = new HomeFragment(); title = getString(R.string.title_home); break; case 1: fragment = new FriendsFragment(); title = getString(R.string.title_friends); break; case 2: fragment = new MessagesFragment(); title = getString(R.string.title_messages); break; default: break; if (fragment != null) FragmentManager fragmentManager = getSupportFragmentManage
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 黑龙江省绥化市海伦市2024-2025学年八年级下学期7月期末考试物理试卷(含答案)
- GHD患儿家庭支持系统构建-洞察与解读
- 玻璃纤维纹理创新-洞察与解读
- 投资智慧财富之门-掌握投资秘诀稳健驾驭财富航船
- 探索中西医同行之路-打造全新医疗卫生生态
- 2025年国家义务教育(心理健康)质量监测考试练习题答案和解析
- 药物治疗方案的调整与安全控制
- 2025版高血压病的症状解析及护理指南
- 沪教版九年级化学上册《氧气的制备》同步练习题及答案
- 湖南长沙市2025-2026学年高一年级上册期中物理练习卷2(原卷及解析)
- 2025-2030中国视觉电生理测试仪行业市场发展趋势与前景展望战略研究报告
- 大学生校园安全教育课件模板
- JJF 2239-2025火花试验机校准规范
- 超星尔雅学习通《俄国近代思想史(北京师范大学)》2025章节测试附答案
- 勘察项目项目管理田园综合体勘察与建设考核试卷
- 《樱桃栽培》课件
- 职业院校产教融合
- 《留置胃管的护理》课件
- 无处不在-传染病知到智慧树章节测试课后答案2024年秋南昌大学
- 旅行社安全生产例会制度模版(2篇)
- 小学数学圆的周长与面积应用题100道及答案解析
评论
0/150
提交评论