官网地址:
http://developer.android.com/training/basics/fragments/index.html
一、创建一个Fragment
你可以认为一个Fragment是一个Activity的模块化区域。它有它自己生命周期,接收它自己的输入事件,你可以在Activity运行的时候动态添加和移除它。
1.1 创建一个Fragment类
和Activity一样,需要继承各自特有的父类(Fragment),然后重载它的生命周期函数,在函数中插入程序逻辑。
我没有看到有像创建一个Activity一样可以直接通过IDE直接创建的(new ->others -> android -> android activity),看来我们要从创建一个class开始了。new -> class 我们熟悉的节奏,这里我只添加了一个方法。里面都是自动生成的,我还没有写任何代码。
import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class ArticleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return super.onCreateView(inflater, container, savedInstanceState); } }
1.2 用XML把Fragment添加到一个Activity中
首先我们新建一个layout文件,new -> other -> Android -> Android XML Layout File (new_articles.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/headlines_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:baselineAligned="false" > <fragment android:id="@+id/article_fragment" android:name="com.example.p6.ArticleFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" /> </LinearLayout>
因为Fragment是嵌入到Activity中的,所以我们需要一个特殊的Activity。
public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_articles); } }
注意到和其它的不一样的是,我们已经把Layout修改为news_articles。同时,通过XML的形式将Fragment添加到Layout中的形式,不能在运行时灵活的移除它。
二、构造一个灵活的UI
当你为不同屏幕大小的android设备设计应用时,你可以重用你的Fragment在适应不同的设备。
官网上给出如下的图片来说明
2.1 运行时添加一个Fragment到一个Activity中
首先我们把我们之前添加到layout中的Fragment清空,并且LinerLayout 修改为FrameLayout res/layout/news_articles.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />
这样 这个 Layout中没有任何东西了。接下来我们在代码中添加,修改MainActivity 中的onCreate()方法
public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); if (findViewById(R.id.fragment_container) != null) { if (savedInstanceState != null) { return; } ArticleFragment af = new ArticleFragment(); af.setArguments(getIntent().getExtras()); getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, af).commit(); } } }
2.2 切换Fragment
我们需要另外一个Fragment来完成切换,ArticleFragment2
public class ArticleFragment2 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return super.onCreateView(inflater, container, savedInstanceState); } }
切换的时候和以前很像
ArticleFragment2 af2=new ArticleFragment2(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, af2); transaction.addToBackStack(null); transaction.commit();
三、 和其它Fragment进行交互
关于这一块官网上说的感觉有点乱,代码零零散散的。其实总结起来很简单。
核心方法在于如何在Activity中找到fragment和各种View,如何在Fragment中得到Activity
getActivity();//获得Activity getActivity().findViewById(R.id.receive_text);//获得View getFragmentManager().findFragmentById(R.id.ReceiveFragment);//获得Fragment
我这里自己写了一个例子,在一个Activity中包含两个Fragments,输入完之后点击第一个fragment中的按钮,第二个Fragment现实消息。
首先是layout
activity_man.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/headlines_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:baselineAligned="false" android:orientation="vertical" > <fragment android:id="@+id/SendFragment" android:name="com.tang.test.SendFragment" android:layout_width="match_parent" android:layout_height="200dp" /> <fragment android:id="@+id/ReceiveFragment" android:name="com.tang.test.ReceiveFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
send.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/headlines_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#FFC000" android:baselineAligned="false" android:orientation="vertical" > <EditText android:id="@+id/sendInput" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="text" > </EditText> <Button android:id="@+id/sendButtion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/send" /> </LinearLayout>
receive.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/headlines_fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="#FFCC00" android:baselineAligned="false" > <TextView android:id="@+id/receive_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/receive" /> </LinearLayout>
然后是类
MainActivity
public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
SendFragment
public class SendFragment extends Fragment { public static final String ARG_POSITION = "ARG_POSITION"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.send, container, false); } @Override public void onStart() { super.onStart(); Button button = (Button) getActivity().findViewById(R.id.sendButtion); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText inputText = (EditText) getActivity().findViewById( R.id.sendInput); ReceiveFragment receiveFragment = (ReceiveFragment) getFragmentManager() .findFragmentById(R.id.ReceiveFragment); receiveFragment.showText(inputText.getText().toString()); } }); } }
ReceiveFragment
public class ReceiveFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.receive, container, false); } public void showText(String text){ TextView textView = (TextView)getActivity().findViewById(R.id.receive_text); textView.setText(text); } }
代码这么详细,跑起来应该很简单了。哎,写这篇文章加上跑例子,要了大概5个小时。泪奔呀
相关推荐
你可以认为fragment是activity的模块化组件,它拥有自己的生命周期,接受它自己的输入事件,你也可以在运行activity的时候添加或者移除它(有点像“子...如此你的app(应用)就能与运行android1.6老版本的系统设备兼容 。
Trying to meet these demands using Android's traditional activity-centric UI design model is difficult at best. As developers, we need more control than that afforded by activities. We need a new ...
使用Fragment如何创建高性能UI;使用Fragment如何创建高性能UI
Create engaging apps with fragments to provide a rich user interface that dynamically adapts to the individual characteristics of your customers' tablets and smartphones About This Book From an ...
基于Android框架的动态UI构建(Creating Dynamic UI with Android Fragments, 2nd Edition)-2016英文原版,0积分——该书是2016年最新的第2版,全书154页。
Long gone are the days of the mobile apps with a static UI squished onto a tiny screen. Today's users expect mobile apps to be dynamic and highly interactive. They expect an app to look fantastic when...
Long gone are the days of mobile apps with a static UI squished on a tiny screen. Today's users expect mobile apps to be dynamic and highly interactive. They expect an app to look fantastic when they ...
[Packt Publishing] Creating Dynamic UI with Android Fragments E Book ☆ 图书概要:☆ Leverage the power of Android fragments to develop dynamic user interfaces for your apps Overview Learn ...
Creating Dynamic UI with Android Fragments Leverage the power of Android Fragments to develop dynamic user interfaces for your apps
android的不断革新,是android的UI控件更加的人性化,Building a Dynamic UI with Fragments,动态的UI更加的增强了界面的交互
Learn smart solutions that will help you avoid pitfalls while building apps that can scale up to a multitude of possible Android device-and-browser combinations. Teaches practical techniques for ...
cvpr2012 oral paper, 一篇不错的文章
Android Fragments, 2014, pdf
Android UI Design:Plan, design, and build engaging user interfaces for your Android applications What You Will Learn Develop a user interface that adheres to all the core material design ...
After this crash-course, we’ll dive deeper into Android programming and you’ll learn how to create applications with a professional-standard UI through fragments, make location-aware apps with ...
Creating and Configuring a New Android Project 69 Core Files and Directories of the Android Application 73 Creating an AVD for Your Project 75 Creating a Launch Configuration for Your Project 75 ...
android-tutorial, 在android中,特性动画和 Fragments 教程 Android教程这里项目包含两个部分。 属性动画与 fragment 在Android中的应用。属性动画一个简单而完整的演示,可以在安卓中实现。 获取更多信息在 ...
Chapter 7: Building Layouts with Lists and Adapters Chapter 8: Working with Input Methods, Menus and Dialogs Chapter 9: Adopting the Action Bar Chapter 10: The Life of an Activity Part III: A World ...
fragements例子 博文链接:https://dasheng.iteye.com/blog/986809
Serving as a hands-on guide to building mobile apps using Android, the book walks you through a series of increasingly sophisticated projects, each introducing a new Android platform feature and ...