Fragment初识

来源:互联网 发布:manifest.json 编辑:程序博客网 时间:2024/05/21 05:38

RZMars个人Blog地址: http://www.rzmars.com

fragment

package com.rzmars.fragmenttutorial;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* @author RZMars
* @date 2013-10-21
* @see http://www.rzmars.com
*
*/
// 最近很多人在群里问Fragment的问题
// 其本质原因就一个不会用,不会用,不知道怎么用,然后就开始baidu,google,这中法式可能是达到效率的最快法式
// 但我认为不是最佳的方式,这样只会削弱你的学习能力,加入某一天这个问题一直search不出来怎么办?
// 然后就坐等问题消失?所以遇到问题第一时间看官网文档,今个我们就一起看看最典型的使用案例
// Fragment 不新鲜啦 12年的时候就已经讨论的沸沸扬扬
// 这个东西其实最重要的目的就是为了更好的管理界面之间的调整,当然它也比普通的Activity跳转更加的流畅了
// 我估计这个设计原理是从iOS而来的,因为在iOS中ViewControll就是采用栈的模式来管理UI

public class MainActivity extends Activity {
int mStackLevel = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
if (savedInstanceState == null) {
// Do first time initialization — add initial fragment.
Fragment newFragment = MyFragment.newInstance(mStackLevel);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragment_layout, newFragment).commit();
mStackLevel++;
} else {
mStackLevel = savedInstanceState.getInt(“text”);
}
}

// initialization control and add action.
private void initUI() {
findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
addFragment();
}
});
findViewById(R.id.btnPop).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
/*
* 源码注释: Parameters: name If non-null, this is the name of a
* previous back state to look for; if found, all states up to
* that state will be popped. The POP_BACK_STACK_INCLUSIVE flag
* can be used to control whether the named state itself is
* popped. If null, only the top state is popped. flags Either 0
* or POP_BACK_STACK_INCLUSIVE.
*/

// getFragmentManager().popBackStack();逐个的pop
// getFragmentManager().popBackStack(name, flags);
// getFragmentManager().popBackStack(id, flags);

/*
*
* name 字符串类型;flags int类型,id int类型
* 其中的的name表示要pop到哪个fragment,是FragmentTransaction中的addToBackStack
* (name);这个方法而来的 id 可以理解为压入的个数的标识,从0开始 递增 flags 根据源码的注释可得知
* 如果flags等于0标识自己不背pop,其他则pop自己
* 比如:你现在一共压入5个fragment,你想回到第一个,也就是跟页面,那么你就使用id=0 flags!=1 作为参数
* 可以说是:popBackStack(0, 0)==popBackStack(1, 1)
*/
getFragmentManager().popBackStack(1, 1);

}
});

}

public void addFragment() {
// fragment采用事务模式管理 ,这种模式也在android中多出可见

Fragment newInstances = MyFragment.newInstance(mStackLevel);
// 开始事务
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_layout, newInstances);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

/*
* Add this transaction to the back stack. This means that the
* transaction will be remembered after it is committed, and will
* reverse its operation when later popped off the stack. Parameters:
* name An optional name for this back stack state, or null.
*
* 很明确 根注释得知,如果我们补添加这个方法,那么事务就无法记住压入的fragment,因为你没有告诉事务你压入了fragment,
* 那么当你pop或者按back按钮的时候自然直接退出程序(因为栈里是空的) 如果你没有特别的要求直接给name=null就可以了,假如
* 你有压入很多个页面,其中有一个首页,那么给首页的name=”homepage”
* 当你popBackStack就会直接跳跃中间的过程直接跳转到首页了
*/
// ft.addToBackStack(name);
ft.addToBackStack(mStackLevel + “”);
// 提交事务
ft.commit();
mStackLevel++;
}
}

// custom my Fragment

class MyFragment extends Fragment {
int index;

public static MyFragment newInstance(int text) {
MyFragment fragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putInt(“text”, text);
fragment.setArguments(bundle);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
index = getArguments() != null ? getArguments().getInt(“text”) : 1;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
TextView tv = new TextView(getActivity());
tv.setText(“第” + index + “个Fragment”);
tv.setTextColor(Color.RED);
tv.setGravity(Gravity.CENTER);
return tv;
}

}

// xml layout

<RelativeLayout 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”
tools:context=”.MainActivity” >

<LinearLayout
android:id=”@+id/btn_layout”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:orientation=”horizontal” >

<Button
android:id=”@+id/btn”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”BtnPush” />

<Button
android:id=”@+id/btnPop”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”BtnPop” />
</LinearLayout>

<FrameLayout
android:id=”@+id/fragment_layout”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@id/btn_layout” >
</FrameLayout>

</RelativeLayout>


原创粉丝点击