Android入门之Fragment用法
来源:互联网 发布:打击电信网络诈骗标语 编辑:程序博客网 时间:2024/05/21 10:17
本文翻译自Android developer网站上面,原文参考:Building a Dynamic UI with Fragments
使用Android库
1. 通过SDK Manager下载Android Support Package。
3. 更新manifest文件,设置如下
android:minSdkVersion="4" android:targetSdkVersion="15" />
为了确保没有在旧的系统上面使用新的api特性,却要在使用Fragment的文件中包含如下内容:import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;应该声明FragmentActivity(而不是Activity)来包含Fragments。
创建Fragment
创建Fragment类
就像创建Activity类一样,继承Fragment,实现一些生命周期中的关键函数,并且记得把你自己的功能代码放到里面去。创建一个Fragment,必须使用onCreateView()定义他的layout布局文件。实际上这是让一个Fragment能够运行起来的唯一一个回调函数,看看下面的例子:
import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.ViewGroup;public class ArticleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.article_view, container, false); }}比Activity简单多了,不是吗。当然,就像Activity一样,Fragment也应该实现其他生命周期中的函数,这样我们才可以实现对他的添加删除。举例来说,当activity收到onPause()方法调用的时候,它里面的Fragment同样会被调用到onPause(),所以看好机会,在Fragment里面的onPause()里面放入你写的代码吧。
更详细的Fragment请参考这里。
使用XML将Fragment添加到一个Activity中
下面是一个将两个Fragment添加到一个活动中的例子
res/layout-large/news_articles.xml:
xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> android:name="com.example.android.fragments.HeadlinesFragment" android:id="@+id/headlines_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> android:name="com.example.android.fragments.ArticleFragment" android:id="@+id/article_fragment" android:layout_weight="2" android:layout_width="0dp" android:layout_height="match_parent" />
看到没有,其实跟一般的添加edittext空间没有任何区别,很简单吧。Tip:
下面就是怎么使用这个布局的代码了:
import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); }}Note:通过在xml中定义fragment的方式,我们不能在运行时移除fragment。如果我们想要通过切换fragments来跟用户有更好的互动,那么就需要在activity启动的时候定义fragment了。
创建更灵活的UI(进阶应用)
比如在平板和手机上,当使用fragment时,表现可能就完全不一样。平板上面我们可以将前后的两个Fragment都显示在屏幕上(因为屏幕空间足够显示了,而且不至于让屏幕空间太空),而在手机上这两个Fragment只能进行切换显示了,因为手机屏幕空间太小,只能够一次显示一屏。图片如下:
在运行时添加一个Fragment到Activity
上面一节的在activity的布局文件(layout xml)中添加Fragment的方法我们已经知道了。现在我们将学习另外一种方式,这种方式允许我们在运行时动态的显示和隐藏fragment。为了达到在activity中动态管理Fragment,我们需要用到FragmentManager,并且通过它创建FragmentTransaction(提供针对Fragment操作的一些列api,add/attach/detach/hide等)。
下面的layout是针对前面一节内容的替代,每次只显示一个fragment。为了替换当前的fragment,这个activity的layout必须包含一个FrameLayout用来当做fragment容器。
注意:该布局文件文件名是跟上节一样,但是layout文件夹并没有large修饰,所以你懂的。(如果不懂的话也不要紧。解释:因为没有large修饰,所以这个layout是用在比large小的屏幕上,这样屏幕每次只能显示一个fragment了,不能像上图的平板那样显示两个fragment)。
res/layout/news_articles.xml:
xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" />
在你的activity内,用getSupportFragmentManage下面就是将一个fragment加入一个layout中:
import android.os.Bundle;import android.support.v4.app.FragmentActivity;public class MainActivity extends FragmentActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); // Check that the activity is using the layout version with // the fragment_container FrameLayout if (findViewById(R.id.fragment_container) != null) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null) { return; } // Create an instance of ExampleFragment HeadlinesFragment firstFragment = new HeadlinesFragment(); // In case this activity was started with special instructions from an Intent, // pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManage因为是在运行时加入到FrameLayout容器中,并不是写死在xml中,所以activity能够移除、替换该fragment。r ().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); } }}
如何在两个fragment间切换
替换fragment的过程跟add差不错,区别就是一个是replace()函数,一个是add()函数。时刻要注意的是当执行fragment操作,比如替换或者移除一个fragment,用户经常的操作是回退(backward)或者还原(undo)操作。为了支持用户的这些操作,我们要在commit()一个FragmentTransaction之前先调用函数addToBackStack()函数。
注意:当移除或者替换一个fragment,并且将这个操作放入back stack(回退堆栈?),当fragment被移除的时候,fragment是停止的(并非销毁)。如果用户要回退,会从堆栈中取一个fragment并且重启(restart)它。如果没有加入back stack,当移除或者替换的时候,这个fragment就直接被销毁了。
切换fragment如下:
// Create fragment and give it an argument specifying the article it should showArticleFragment newFragment = new ArticleFragment();Bundle args = new Bundle();args.putInt(ArticleFragment.ARG_POSITION, position);newFragment.setArguments(args);FragmentTransaction transaction = getSupportFragmentManageaddToBackStack()里面的有一个可选的String参数,该参数在需要使用FragmentManager.BackStackEntry类的APIs的时候需要用到。r ().beginTransaction();// Replace whatever is in the fragment_container view with this fragment,// and add the transaction to the back stack so the user can navigate backtransaction.replace(R.id.fragment_container, newFragment);transaction.addToBackStack(null);// Commit the transactiontransaction.commit();
Fragment间通信
定义一个接口
为了允许一个Fragment跟包含他的Activity通信,我们可以在Fragment类中定义一个接口,并且在Activity内实现。Fragment在onAttach()函数里面捕获接口实现,并且调用接口方法和Activity通信。(上面都说了Fragment间只能通过Activity进行通信了。)
看看下面的Fragment和Activity通信的例子:
public class HeadlinesFragment extends ListFragment { OnHeadlineSelectedListen现在Fragment可以通过onArticleSelected()函数,将消息传递到到Activity中。通过使用OnHeadlineSelectedListener mCallback; // Container Activity must implement this interface public interface OnHeadlineSelectedListener { public void onArticleSelected(int position); } @Override public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception try { mCallback = (OnHeadlineSelectedListener ) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener" ); } } ...}
例如:下面的例子中,当点击一个列表的元素的时候,fragment中的一个方法被调用。Fragment使用mcallback这个实例化的东东
,将该事件传递给它所依附的父Activity。
@Override public void onListItemClick(ListView l, View v, int position, long id) { // Send the event to the host activity mCallback.onArticleSelected(position); }实现接口
为了从Fragment中接受事件消息,父activity必须实现fragment类中定义的几个接口。示例如下:
public static class MainActivity extends Activity implements HeadlinesFragment.OnHeadlineSelectedListener { ... public void onArticleSelected(int position) { // The user selected the headline of an article from the HeadlinesFragment // Do something here to display that article }}
传递消息到Fragment中
宿主Activity可以可以通过findFragmentById()函数获取Fragment实例,然后通过访问fragments的共有函数来将消息传递给Fragments。
在下面的实例中,宿主类将在回调函数中获取的信息传递到另外一个显示这些数据的Fragment中:
public static class MainActivity extends Activity implements HeadlinesFragment.OnHeadlineSelectedListener { ... public void onArticleSelected(int position) { // The user selected the headline of an article from the HeadlinesFragment // Do something here to display that article ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager ().findFragmentById(R.id.article_fragment); if (articleFrag != null) { // If article frag is available, we're in two-pane layout... // Call a method in the ArticleFragment to update its content articleFrag.updateArticleView(position); } else { // Otherwise, we're in the one-pane layout and must swap frags... // Create fragment and give it an argument for the selected article ArticleFragment newFragment = new ArticleFragment(); Bundle args = new Bundle(); args.putInt(ArticleFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager ().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } }}
使用 FragmentTransaction 的时候,它提供了这样两个方法,一个 add , 一个 replace ,对这两个方法的区别一直有点疑惑。我觉得使用 add 的话,在按返回键应该是回退到上一个 Fragment,而使用 replace 的话,那个别 replace 的就已经不存在了,所以就不会回退了。但事实不是这样子的。add 和 replace 影响的只是界面,而控制回退的,是事务。
Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.
add 是把一个fragment添加到一个容器 container 里。
Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.
replace 是先remove掉相同id的所有fragment,然后在add当前的这个fragment。
在大部分情况下,这两个的表现基本相同。因为,一般,咱们会使用一个FrameLayout来当容器,而每个Fragment被add 或者 replace 到这个FrameLayout的时候,都是显示在最上层的。所以你看到的界面都是一样的。但是,使用add的情况下,这个FrameLayout其实有2层,多层肯定要比一层的来得浪费,所以还是推荐使用replace。当然有时候还是需要使用add的。比如要实现轮播图的效果,每个轮播图都是一个独立的Fragment,而他的容器FrameLayout需要add多个Fragment,这样他就可以根据提供的逻辑进行轮播了。
而至于返回键的时候,这个跟事务有关,跟使用add还是replace没有任何关系。
- 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回忆(Android入门之Fragment用法)
- Android入门——Fragment详解之基本概念与用法(一)
- Android fragment的用法
- android Fragment用法
- Android中Fragment用法
- 按一定规律输入“*”
- 自定义view实现图文环绕的效果
- 重玩星号图1
- HDU 4435 charge-station - from lanshui_Yang
- 枚举类型
- Android入门之Fragment用法
- oracle job没有运行
- 数据挖掘中的ROC曲线
- html基本知识和网站seo优化
- 提高编程效率的14件事
- UVA 106Fermat vs. Pythagoras(数论)
- 输入金字塔形状的“*”
- read links -- sentiment
- 两个大数相乘、精度很高的小数相乘(小数点后位数没有限制,请写一个高精度算法)