您的位置:首页 > 移动开发 > Android开发

Android Fragment 碎片的初步使用

2017-04-15 18:58 302 查看

项目简介:

该项目为碎片的使用

详细介绍:

该项目详细介绍了Android 3.0版本以后碎片的使用,Android3.0之前的需要做向下兼容。

安装该应用,进入后点击“红色”按钮:



在MainActivity中的EditText控件写入任意数据,然后点击“传递数据给碎片”:



可以看到碎片中显示了MainActivity传来的数据。然后在碎片中写入数据,点击“传给Activity”:



可以看到MainActivity中也接收到了碎片中传递的数据。然后点击绿色按钮,发现屏幕右边画面变成了绿色:



该应用涉及到的知识有:

1.Fragment的使用

  1).创建一个帧布局fl

  2).创建一个xml文件,该文件作为碎片中显示的内容的布局文件

  3).创建Fragment01类,该类继承Fragment类,重写里面的oncreteView方法

  4).在Activity中创建Fragment01类的对象,获取FragmentManager,然后获取FragmentTransaction对象,调用replace方法,最后提交事务

  5)总结一下上面四个步骤,通俗的说就是:

  创建一个碎片,在碎片中加载布局文件,然后在把碎片放在对应的Activity上即可。

举个例子:

  在一张纸A上作画(相当于Activity),然后再在另一张纸B作画(相当于Fragment),然后直接把B贴在A上就可以了,如果不满意,可以随时把B给撕下来贴上C(另个一个Fragment)

  

2.Activity向Fragment传递数据

  直接在Activity中创建Fragment的对象,然后调用Fragment里面的方法,实际上是把数据作为方法的参数传递给Fragment

  

3.Fragment向Activity传递数据

  直接在在该类中获取Activity的实例,然后调用Activity中的方法,实际更Activity想Fragment传递数据的原理一样

注意:

1.该项目是运行在Android 3.0以上的,导入的包一定要选对 android.app.*;

2.对于Android 3.0以下的版本,该项目也可以向下兼容,具体做法如下:

  首先把 android.app.* 包全部换成支持android.support.v4.spp.*,然后把FirstActivity继承的Activity替换为支持包中的FragmentActivity,再更改获取Fragment管理器的语句,换成getSupportFragmentManager(),这样向下兼容就完成了,但是很少用到。对于版本太低的设备,可以直接放弃了。

步骤:

1.创建一个Android应用,编写布局文件

编写activity_main.xml文件,该文件将做为MainActivity的布局加载:

<?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="horizontal" >

<FrameLayout
android:id="@+id/fl"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000ff" >
</FrameLayout>

<LinearLayout
android:layout_width="130dp"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click1"
android:text="红色"
android:textColor="#ff0000"
android:textSize="30sp" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click2"
android:text="绿色"
android:textColor="#00ff00"
android:textSize="30sp" />

<EditText
android:id="@+id/ed"
android:layout_width="match_parent"
andr
4000
oid:layout_height="wrap_content" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click3"
android:text="传递数据给碎片"
android:textColor="#ff00ff"
android:textSize="16sp" />

<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="来自来自的数据" />
</LinearLayout>

</LinearLayout>


整个布局如下所示:



编写fragment01.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:background="@android:color/holo_red_light"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是Fragment01"
android:textSize="36sp" />

<EditText
android:id="@+id/ed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<requestFocus />

<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="传给Activity"
/>

<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="来自MainActivity的数据"
android:textSize="30sp"
android:textColor="#00ff00"
/>

</LinearLayout>


编写fragment02.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:background="@android:color/holo_green_light"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是Fragment02"
android:textSize="36sp" />

</LinearLayout>


相对于第一个碎片的布局,第二个简单的多,所以大部分操作都在第一个完成。

2.新建Fragement类

新建一个Java类,继承Fragment:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
* 创建Fragment对象,并向Activity传递数据和接受来自MainActivity的数据
*
* @author HHH
*
*/
public class Fragment01 extends Fragment {

private EditText ed;
private Button bt;
private TextView tv;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// 使用布局填充器创建View对象
View view = inflater.inflate(R.layout.fragment01, null);

ed = (EditText) view.findViewById(R.id.ed);
bt = (Button) view.findViewById(R.id.bt);
tv = (TextView) view.findViewById(R.id.tv);

// 点击按钮后,将数据返回给MianActivity
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String text = ed.getText().toString();

// 把数据传给FirstActivity,并显示TextView控件上
((MainActivity) getActivity()).setText(text);

}
});

// 返回的View对象会作为fragment01.xml的内容显示在屏幕上
return view;
}

/* 创建一个方法,当该方法被调用时,传入的参数将被显示在fragment01的TextView控件上 */
public void setText(String text) {
tv.setText(text);
}

}


再新建一个Java类,继承Fragment:

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* * 创建Fragment对象
*
* @author HHH
*
*/
public class Fragment02 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment02, null);
return view;
}
}


3.Activity

编写MainActivity代码:

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

/**
* @author HHH 该Activity演示一下内容:
*         1.Fragment如何工作的。即如何点解按钮切换Fragment。
*         2.如何从Activity传数据到Fragment以及如何从Fragment串数据到Activity
*/
public class MainActivity extends Activity {

private EditText ed;
private TextView tv;
private Fragment02 fragment02;
private Fragment01 fragment01;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = (EditText) findViewById(R.id.ed);
tv = (TextView) findViewById(R.id.tv);
}

/**
* 把fragment01的界面显示到帧布局中
*
* @param v
*/
public void click1(View v) {

// 获取Fragment管理器
FragmentManager fragmentManager = getFragmentManager();

// 打开事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

fragment01 = new Fragment01();

// 把内容显示到帧布局
fragmentTransaction.replace(R.id.fl, fragment01);

// 提交事务
fragmentTransaction.commit();
}

/**
* 把fragment02的界面显示到帧布局中
*
* @param v
*/
public void click2(View v) {

// 获取Fragment管理器
FragmentManager fragmentManager = getFragmentManager();

// 打开事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

// 把内容显示到帧布局
fragment02 = new Fragment02();
fragmentTransaction.replace(R.id.fl, fragment02);

// 提交事务
fragmentTransaction.commit();
}

/**
* 把数据传递给Fragment02
*/
public void click3(View view) {
String text = ed.getText().toString();

// 拿到Fragment的对象fragment02,调用Fragment类中的方法setText(该方法自己编写的)
fragment01.setText(text);
}

/**
* 把数据显示在TestView上
*/
public void setText(String text) {
tv.setText(text);
}

}


到这里,整个应用就完成了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 碎片 fragment