Android -- 使用Fragment
来源:互联网 发布:专卖店软件泰安 编辑:程序博客网 时间:2024/05/17 02:23
Fragment类似与Activity,可以使用它进行多个页面间的切换(A页面跳转到B页面),之前我们了解过,通过Activity也可以实现这样的操作(http://blog.csdn.net/gaopeng0071/article/details/45043967)。那为什么会出现Fragment呢,下面我们来看看
Fragment介绍
Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神马超级大屏的。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment。
可以简单理解,就是Fragment相对Activity更轻量级、效率更高、速度更快
下面我们来看一个简单跳转例子。
我们来看下工程布局
1个Activity,2个Fragment,以及所对应的xml资源文件。
下面我们看代码MainActivity
package gp.com.learnfragment;import android.support.v4.app.Fragment;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(savedInstanceState == null){ getSupportFragmentManager().beginTransaction() .add(R.id.fragment, new MainActivityFragment()).commit(); } } @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); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action 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 SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
代码16到19行将目标Fragment添加到容器中。
下面来看MainActivityFragment
package gp.com.learnfragment;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * A placeholder fragment containing a simple view. */public class MainActivityFragment extends Fragment { public MainActivityFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_main, container, false); view.findViewById(R.id.showOthFragment) .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getFragmentManager().beginTransaction() .replace(R.id.fragment, new OtherFragment()) .commit(); } }); return view; }}
注意onCreateView方法,其中为showOthFragment按钮增加了跳转到另外一个Fragment的事件。
Fragment_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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="呈现另一个fragment" android:id="@+id/showOthFragment" /></LinearLayout>
其中包含showOthFragment按钮
OtherFragment类代码
package gp.com.learnfragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * Created by zm on 2015/7/14. */public class OtherFragment extends Fragment { public OtherFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_other, container, false); }}
Fragment_other.xml代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="我是另外一个fragment页面" android:id="@+id/textView" /></LinearLayout>
至此一套简易的Fragment代码构建用法完成。
- Android Fragment使用
- Android Fragment使用详解
- Android Fragment使用详解
- Android Fragment使用
- android Fragment 使用
- Android Fragment使用详解
- Android Fragment使用
- Android Fragment使用
- Android Fragment使用详解
- Android Fragment的使用
- Android-Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- Android Fragment的使用
- android Fragment 的使用
- Android Fragment的使用
- cocos2dx 单点触摸的简单使用
- 开源日志系统比较
- Project Euler:Problem 61 Cyclical figurate numbers
- Adb 的使用
- 驱动之module_init/module_exit
- Android -- 使用Fragment
- C++类内存分布
- List和ArrayList的区别
- JVM --并发垃圾回收器CMS
- linux中svn命令集
- app应用分析
- Android视频渲染: YUV转RGB
- X264编码的一些收藏
- 数据库设计三大范式