您的位置:首页 > 移动开发

《笑话大全》APP代码详解3

2016-06-12 03:17 295 查看
前面两章讲的启动页和引导页都不是重点,重点在本章所要讲到的主页面,因为主页面才应该是一个程序的核心,程序要展现什么,有什么功能都能够在主页面上体现出来,话不多说,还是先看代码。

public class MainActivity extends AppCompatActivity {
//主页面的侧滑控件(Google官方控件)
private DrawerLayout drawerLayout;
private ScrollView mScrollView;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//初始化数据
initFragment();
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
mScrollView=(ScrollView)findViewById(R.id.drawer_scroll);
}
private void initFragment(){ FragmentManager fm=getSupportFragmentManager(); //开启FragmentManager的事务 FragmentTransaction fmTransaction=fm.beginTransaction(); //利用事务机制替换掉主页面内容的布局文件。不直接在布局文件中写入控件是为了解耦。 fmTransaction.replace(R.id.content_frameLayout,new ContentFragment()); //开启事务。 fmTransaction.commit(); //禁用DrawerLayout的滑动 }
public void stClick(View v){
if (drawerLayout != null && mScrollView != null){
drawerLayout.openDrawer(mScrollView);
}
}
}
就这么几十行代码,看起来很简单的样子,大体分为这么几个部分:加载主页面布局,初始化数据的函数,和一个按钮的监听函数,监听函数里面的内容就是控制侧滑菜单的。先看加载的布局文件activity_main:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout">
<LinearLayout
android:id="@+id/drawer_linear"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/content_activity"/>
</LinearLayout>

<ScrollView
android:id="@+id/drawer_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start">
<fragment
android:id="@+id/drawer_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
android:name="com.example.tangyi.jokes.Tools.MyFragment"/>
</ScrollView>
</android.support.v4.widget.DrawerLayout>布局文件中使用的不是线性布局也不是相对布局,而是DrawerLayout,这是Google官方的侧滑控件,它是作为一个布局控件来使用的,使用侧滑控件时第一个View必须是显示内容的View,也就是说第一个View必须是主页面才行,第二个View才是侧滑菜单的内容。可以看到上面布局文件的代码中第一个View是引用的content_activity,第二个View使用的是fragment,先去看看content_activity中的内容吧:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

</FrameLayout>是的,content_activity是空白的,什么都没有,那主页面是在哪里完成加载的呢?相信你已经发现了,在MainActivity中有如下几行代码:
private void initFragment(){

FragmentManager fm=getSupportFragmentManager();
//开启FragmentManager的事务
FragmentTransaction fmTransaction=fm.beginTransaction();
//利用事务机制替换掉主页面内容的布局文件。不直接在布局文件中写入控件是为了解耦。
fmTransaction.replace(R.id.content_frameLayout,new ContentFragment());
//开启事务。
fmTransaction.commit();
//禁用DrawerLayout的滑动
}
原来在主页面加载的时候利用事务机制替换掉了布局文件,原因上面写得很清楚,是为了解耦。替换成了ContentFragment,这是个类文件,本程序重点部分包括网络请求,JSON数据解析的方法都在里面,所以在下一章单独说明,本章节不做讲解。
继续向下看,就是侧滑菜单的内容了,布局文件中引用的是MyFragment,看看它的内容吧:

public class MyFragment extends Fragment {
private LinearLayout aboutLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.setting_layout,null);
aboutLayout=(LinearLayout)view.findViewById(R.id.about_layout);
aboutLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),AboutActivity.class));
}
});
return view;
}
}MyFragment中加载的是setting_layout布局文件,下面是它的详细代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/setting_back"
android:id="@+id/setting_layout">
<LinearLayout
android:id="@+id/about_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:showDividers="end">
<ImageView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_marginLeft="27dp"
android:layout_marginRight="30dp"
android:src="@drawable/ic_about"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#ffffff"
android:textSize="25sp"
android:gravity="center"
android:text="关于"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffffff"/>

</LinearLayout>就只有一个子线性布局和一条view分割线,效果如下:



在MyFragment中为子线性布局定义了点击时间监听,就是一个显式Intent跳转,这里不做赘述。

这个APP的确是很简单,除了主页面以外其他的所有部分到这里都已经说明完毕了,我自己都觉得有点丢人...

下一章可能也是最后一章,接着说明主页面中的内容。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: