Android中Fragment的简单应用
2015-09-11 12:48
531 查看
这个学期的短学期要求我们在移动平台上进行开发,虽然自己断断续续已经写了大半年的Android了,但是暑假的时候一直在刷PAT,开学了之后也在写HDUOJ,eclipse也好久没打开过了,现在趁这个机会重新捡起Android。
这是第二个小作业,要求我们使用Fragment,我还一直没接触过这个,所以借鉴着网上大牛们的介绍写了一个小DEMO。
Fragment的出现是因为在不同的屏幕大小下,我们的APP无法同时满足不同大小,所以出现了Fragment。(你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。)(摘自大牛的Blog)
activity_main.xml
效果图:
这是第二个小作业,要求我们使用Fragment,我还一直没接触过这个,所以借鉴着网上大牛们的介绍写了一个小DEMO。
Fragment的出现是因为在不同的屏幕大小下,我们的APP无法同时满足不同大小,所以出现了Fragment。(你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。)(摘自大牛的Blog)
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.whisker.fragmenttest.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="36dp" android:orientation="horizontal" android:weightSum="3"> <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/tab_down" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="NO.1" android:textColor="#ffffff"/> </LinearLayout> <ImageView android:layout_width="2dp" android:layout_height="match_parent" android:background="#ffffff" /> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/tab" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="NO.2" android:textColor="#ffffff"/> </LinearLayout> <ImageView android:layout_width="2dp" android:layout_height="match_parent" android:background="#ffffff" /> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/tab" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="NO.3" android:textColor="#ffffff"/> </LinearLayout> </LinearLayout> <FrameLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </LinearLayout>MainActivity.java
package com.whisker.fragmenttest; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; public class MainActivity extends FragmentActivity { <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>private LinearLayout tabLayout1,tabLayout2,tabLayout3; <span style="white-space:pre"> </span>private Fragment tabFragment1,tabFragment2,tabFragment3; <span style="white-space:pre"> </span>private FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentManager = getSupportFragmentManager(); tabLayout1 = (LinearLayout) findViewById(R.id.tab1); tabLayout2 = (LinearLayout) findViewById(R.id.tab2); tabLayout3 = (LinearLayout) findViewById(R.id.tab3); clickListener clickListener = new clickListener(); <span style="white-space:pre"> </span>tabLayout1.setOnClickListener(clickListener); tabLayout2.setOnClickListener(clickListener); tabLayout3.setOnClickListener(clickListener); setDefaultFragment(); } /*初始化默认的Fragment*/ private void setDefaultFragment() { <span style="white-space:pre"> </span>FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); <span style="white-space:pre"> </span>tabFragment1 = new TabFragment1(); <span style="white-space:pre"> </span>fragmentTransaction.replace(R.id.content_layout, tabFragment1); <span style="white-space:pre"> </span>fragmentTransaction.commit(); } public void replaceFragment(Fragment fragment) { <span style="white-space:pre"> </span>FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); <span style="white-space:pre"> </span>if(!fragment.isAdded()){ <span style="white-space:pre"> </span>fragmentTransaction.replace(R.id.content_layout, fragment); <span style="white-space:pre"> </span>fragmentTransaction.commit(); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>else{ <span style="white-space:pre"> </span>fragmentTransaction.show(fragment); <span style="white-space:pre"> </span>} } private class clickListener implements OnClickListener{ <span style="white-space:pre"> </span>@Override <span style="white-space:pre"> </span>public void onClick(View v) { <span style="white-space:pre"> </span>switch (v.getId()) { <span style="white-space:pre"> </span>case R.id.tab1: <span style="white-space:pre"> </span>if(tabFragment1 == null) <span style="white-space:pre"> </span>tabFragment1 = new TabFragment1(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>replaceFragment(tabFragment1); <span style="white-space:pre"> </span>tabLayout1.setBackgroundColor(getResources().getColor(R.color.tab_down)); <span style="white-space:pre"> </span>tabLayout2.setBackgroundColor(getResources().getColor(R.color.tab)); <span style="white-space:pre"> </span>tabLayout3.setBackgroundColor(getResources().getColor(R.color.tab)); <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>case R.id.tab2: <span style="white-space:pre"> </span>if(tabFragment2 == null) <span style="white-space:pre"> </span>tabFragment2 = new TabFragment2(); <span style="white-space:pre"> </span>replaceFragment(tabFragment2); <span style="white-space:pre"> </span>tabLayout2.setBackgroundColor(getResources().getColor(R.color.tab_down)); <span style="white-space:pre"> </span>tabLayout1.setBackgroundColor(getResources().getColor(R.color.tab)); <span style="white-space:pre"> </span>tabLayout3.setBackgroundColor(getResources().getColor(R.color.tab)); <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>case R.id.tab3: <span style="white-space:pre"> </span>if(tabFragment3 == null) <span style="white-space:pre"> </span>tabFragment3 = new TabFragment3(); <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>replaceFragment(tabFragment3); <span style="white-space:pre"> </span>tabLayout1.setBackgroundColor(getResources().getColor(R.color.tab)); <span style="white-space:pre"> </span>tabLayout2.setBackgroundColor(getResources().getColor(R.color.tab)); <span style="white-space:pre"> </span>tabLayout3.setBackgroundColor(getResources().getColor(R.color.tab_down)); <span style="white-space:pre"> </span>break; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span> } }TabFragment1.java
package com.whisker.fragmenttest; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabFragment1 extends Fragment{ @Override @Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_tab1, null); return view; } }fragment_tab1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:text="Tab1" android:textColor="#000000"/> </LinearLayout>TabFragment2,TabFragment3同上。
效果图:
相关文章推荐
- android 6.0 权限管理的学习资料和使用例子
- Android 获取状态栏高度
- 灵活强大的构建系统Gradle
- 美团Android自动化之旅—生成渠道包
- android include 控件详解
- android开发中的常用布局
- Android(java)学习笔记228:服务(service)之绑定服务调用服务里面的方法
- Android中的事件分发机制总结
- android 访问c# webservice 实现登陆注册功能(android 上传json到服务器)
- 怎么在Android中设置一个color.xml快速获取颜色
- ArcGIS Runtime SDKs v10.2.4最新(Android、iOS、OSX和.NET)
- Android笔记之Activity生命周期实测
- [Android]device layout partitions introduction
- popupwindow全屏幕显示被状态栏挡住如何解决+Android获取状态栏高度
- android 应用程序安装卸载(安装应用宝后自动弹出卸载应用宝)
- 用Gradle 构建你的android程序
- Android APK加壳技术方案【1】
- Android中程序与Service交互的方式——交互方式
- android 动态广播与静态广播区别
- Android 和 JAVA通用解压文件方法