`
tmj_159
  • 浏览: 700144 次
  • 性别: Icon_minigender_1
  • 来自: 永州
社区版块
存档分类
最新评论

上官网学android之六(Building a Dynamic UI with Fragments)

阅读更多

官网地址:

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个小时。泪奔呀

 

 

 

  • 大小: 35.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics