super.onAttach(activity);
adapter = new ArrayAdapter(activity, android.R.layout.simple_list_item_1, menuItems);
}
/**
- 加载menu_fragment布局文件,为ListView绑定了适配器,并设置了监听事件。
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.menu_fragment, container, false);
menuList = (ListView) view.findViewById(R.id.menu_list);
menuList.setAdapter(adapter);
menuList.setOnItemClickListener(this);
return view;
}
/**
- 当Activity创建完毕后,尝试获取一下布局文件中是否有details_layout这个元素,如果有说明当前
- 是双页模式,如果没有说明当前是单页模式。
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (getActivity().findViewById(R.id.details_layout) != null) {
isTwoPane = true;
} else {
isTwoPane = false;
}
}
/**
- 处理ListView的点击事件,会根据当前是否是双页模式进行判断。如果是双页模式,则会动态添加Fragment。
- 如果不是双页模式,则会打开新的Activity。
*/
@Override
public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {
if (isTwoPane) {
Fragment fragment = null;
if (index == 0) {
fragment = new SoundFragment();
} else if (index == 1) {
fragment = new DisplayFragment();
}
getFragmentManager().beginTransaction().replace(R.id.details_layout, fragment).commit();
} else {
Intent intent = null;
if (index == 0) {
intent = new Intent(getActivity(), SoundActivity.class);
} else if (index == 1) {
intent = new Intent(getActivity(), DisplayActivity.class);
}
startActivity(intent);
}
}
}
这个类的代码并不长,我简单的说明一下。在 onCreateView 方法中加载了 menu_fragment 这个布局,这个布局里面包含了一个 ListView,然后我们对这个 ListView 填充了两个简单的数据 “Sound” 和 “Display” 。又在 onActivityCreated 方法中做了一个判断,如果 Activity 的布局中包含了 details_layout 这个元素,那么当前就是双页模式,否则就是单页模式。onItemClick 方法则处理了 ListView 的点击事件,发现如果当前是双页模式,就动态往 details_layout 中添加 Fragment,如果当前是单页模式,就直接打开新的 Activity。
我们把 MenuFragment 中引用到的其它内容一个个添加进来。新建 menu_fragment.xml 文件,加入如下代码:
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent” >
android:id=“@+id/menu_list”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
然后新建 SoundFragment,里面内容非常简单:
public class SoundFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sound_fragment, container, false);
这里 SoundFragment 需要用到 sound_fragment.xml 布局文件,因此这里我们新建这个布局文件,并加入如下代码:
<?xml version="1.0" encoding="utf-8"?>同样的道理,我们再新建 DisplayFragment 和 display_fragment.xml 布局文件:
public class DisplayFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.display_fragment, container, false);
return view;
}
}
然后新建 SoundActivity,代码如下:
public class SoundActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sound_activity);
}
}
这个 Activity 只是加载了一个布局文件,现在我们来实现 sound_activity.xml 这个布局文件:
<?xml version="1.0" encoding="utf-8"?>这个布局文件引用了 SoundFragment,这样写的好处就是,以后我们只需要在 SoundFragment 中修改代码,SoundActivity 就会跟着自动改变了,因为它所有的代码都是从 SoundFragment 中引用过来的。
好,同样的方法,我们再完成 DisplayActivity:
public class DisplayActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_activity);
}
}
然后加入 display_activity.xml:
<?xml version="1.0" encoding="utf-8"?>四
现在所有的代码就都已经完成了,我们来看一下效果吧。
首先将程序运行在手机上,效果图如下:
分别点击 Sound 和 Display,界面会跳转到声音和显示界面:
然后将程序在平板上运行,点击 Sound,效果图如下:
然后点击 Display 切换到显示界面,效果图如下:
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(资料价值较高,非无偿)
![](https://i-blog.csdnimg.cn/blog_migrate/73a88c982840e8480e94696f0961a5e6.jpeg)
结尾
如何才能让我们在面试中对答如流呢?
答案当然是平时在工作或者学习中多提升自身实力的啦,那如何才能正确的学习,有方向的学习呢?为此我整理了一份Android学习资料路线:
这里是一份BAT大厂面试资料专题包:
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划。来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
11605213805)]
好了,今天的分享就到这里,如果你对在面试中遇到的问题,或者刚毕业及工作几年迷茫不知道该如何准备面试并突破现状提升自己,对于自己的未来还不够了解不知道给如何规划。来看看同行们都是如何突破现状,怎么学习的,来吸收他们的面试以及工作经验完善自己的之后的面试计划及职业规划。