android (13) Fragment使用下

一.Fragment使用:

    要在你的activity中管理Fragment,需要使用FragmentManager,可以通过getFragmentManager(),这里注意要是在v4包要用getSupportFragmentManager()方法。

FragmentManager可以开一个事物调用beginTransaction()方法返回FragmentTransaction对象,这是我们可以用FragmentTransaction去执行删除增加Fragment的一些操作:

(1).add():往activity添加一个Fragment


(2).remove():从Activity移除一个Fragment,当这个Fragment没有添加进回退栈,则会被销毁。当添加进入回退栈之后则会销毁视图层,在显示这个Fragment则会调用onDestoryView和onCreateView。


(3).replace():使用另一个Fragment替换当前的Fragment,也就是remove操作和add合体执行。


(4).hide():隐藏当前的Fragment,仅仅是设为不可见,并不会销毁。


(5).show():显示之前隐藏的Fragment。


(6).detach():会将view从UI中移除,和remove()不同,此时fragment的状态依然由FragmentManager维护。


(7).attach():重建view视图,附加到UI上并显示。


(8).addToBackStack():添加Fragment事务到回退栈中。传null表示当前Fragment。


(9).commit():提交事务。


注意当以使用了add添加Fragment之后,你仅仅是想隐藏Fragment,而且保持当前Fragment输入的数据,你只需要用hide(),如果你不想用户在看到数据,直接使用replace()比使用add()在remove()方便的多。

二.如何使用回退栈保持数据:

实现效果,当进入第一个Fragment后点击改变文字按钮,会把显示在屏幕中央的TextView内容改变,然后点击进入第二个Fragment,在点击返回按钮,屏幕中央的TextView内容会还原成未改变前的,这就是说明了退回栈把Fragment的视图销毁了,但是实例并没有销毁,如果你想要不把数据也销毁,则就像上面说的使用hide()和show().

图片就不上传了,大家可是自行测试源码(要源码留下邮箱,这个是在上篇文章基础上修改的):

主Activity:

public class MainActivity extends Activity  {
	RelativeLayout r1;
	RelativeLayout r2;
	RelativeLayout r3;
	RelativeLayout view = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bottom_layout);

		r1 = (RelativeLayout) findViewById(R.id.layout1);
		r2 = (RelativeLayout) findViewById(R.id.layout2);
		r3 = (RelativeLayout) findViewById(R.id.layout3);

		setDefaultFragment();
	}
	private void setDefaultFragment() {
		FragmentManager fm = getFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		MyFragment my = new MyFragment();
		transaction.add(R.id.frame_layout1, my,"ONE");
		transaction.addToBackStack(null);
		transaction.commit();
	}
	
	
}

Fragment1:

public class MyFragment extends Fragment implements OnClickListener {

	private Button button1;
	private Button button2;
	private TextView textView1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		System.out.println("onCreateView");
		View view = inflater.inflate(R.layout.fragment_1, container, false);
		button1 = (Button) view.findViewById(R.id.button1);
		button1.setOnClickListener(this);
		button2 = (Button) view.findViewById(R.id.button2);
		button2.setOnClickListener(this);
		textView1 = (TextView) view.findViewById(R.id.textView1);
		return view;
	}
	
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.button1:
			textView1.append("哈");
			break;
		case R.id.button2:
			MyFragment2 f2 = new MyFragment2();
			FragmentManager fm = getFragmentManager();
			FragmentTransaction tx = fm.beginTransaction();
			tx.replace(R.id.frame_layout1, f2, "TWO");
			tx.addToBackStack(null);
			tx.commit();
			break;
		}
	}

}

Fragment2:

public class MyFragment2 extends Fragment implements OnClickListener {
	private Button button1;
	private Button button2;
	private TextView textView1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		System.out.println("onCreateView");
		View view = inflater.inflate(R.layout.fragment_2, container, false);
		button1 = (Button) view.findViewById(R.id.button11);
		button1.setOnClickListener(this);
		button2 = (Button) view.findViewById(R.id.button21);
		button2.setOnClickListener(this);
		textView1 = (TextView) view.findViewById(R.id.textView2);
		return view;
	}
	
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.button11:
			textView1.append("哈");
			break;
		case R.id.button21:
			MyFragment3 f3 = new MyFragment3();
			FragmentManager fm = getFragmentManager();
			FragmentTransaction tx = fm.beginTransaction();
			tx.hide(this);
			tx.add(R.id.frame_layout1, f3, "THREE");
	// tx.replace(R.id.id_content, fThree, "THREE");
			tx.addToBackStack(null);
			tx.commit();
			break;
		}
	}
	
}
Fragment1布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="第一个页面" />

       <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:text="改变文字" />
    
    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_below="@id/button1"
        android:text="跳转第二个界面" />

  

</RelativeLayout>

Fragment3:

public class MyFragment3 extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    		Bundle savedInstanceState) {
    	return inflater.inflate(R.layout.fragment_3, container, false);
    }
}

这样就ok了。


三.Activity与Fragment之间的回调:

因为要考虑Fragment的重复使用,所以必须降低Fragment与Activity的耦合,而且Fragment更不应该直接操作别的Fragment,毕竟Fragment操作应该由它的管理者Activity来决定。


就用上边的例子,第一种回调:

public class MyFragment extends Fragment implements OnClickListener {

	private Button button1;
	private Button button2;
	private TextView textView1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		System.out.println("onCreateView");
		View view = inflater.inflate(R.layout.fragment_1, container, false);
		button1 = (Button) view.findViewById(R.id.button1);
		button1.setOnClickListener(this);
		button2 = (Button) view.findViewById(R.id.button2);
		button2.setOnClickListener(this);
		textView1 = (TextView) view.findViewById(R.id.textView1);
		return view;
	}
	
	public interface MyFragmentOneClick{
		void onMyOneBtnClick();  
	}
	
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.button1:
			textView1.append("哈");
			break;
		case R.id.button2:
			//第一种回调方式
			if (getActivity() instanceof MyFragmentOneClick)  
	        {  
	            ((MyFragmentOneClick) getActivity()).onMyOneBtnClick();  
	        }  
			break;
		}
	}

}


第二种回调:

public class MyFragment2 extends Fragment implements OnClickListener {
	private Button button1;
	private Button button2;
	private TextView textView1;
	private MyFragmentTwoClick myFragmentTwoClick ;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		System.out.println("onCreateView");
		View view = inflater.inflate(R.layout.fragment_2, container, false);
		button1 = (Button) view.findViewById(R.id.button11);
		button1.setOnClickListener(this);
		button2 = (Button) view.findViewById(R.id.button21);
		button2.setOnClickListener(this);
		textView1 = (TextView) view.findViewById(R.id.textView2);
		return view;
	}
	
	public interface MyFragmentTwoClick{
		void onMyTwoBtnClick();  
	}
	
	//第二种回调方式,设置回调接口  
    public void setMyTwoBtnClickListener(MyFragmentTwoClick myFragmentTwoClick)  
    {  
        this.myFragmentTwoClick = myFragmentTwoClick;  
    }  
	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.button11:
			textView1.append("哈");
			break;
		case R.id.button21:
			if(myFragmentTwoClick != null)  
	        {  
				myFragmentTwoClick.onMyTwoBtnClick();  
	        }  
			break;
		}
	}
	
}

主Activity实现:


public class MainActivity extends Activity implements MyFragmentOneClick,MyFragmentTwoClick {
	RelativeLayout r1;
	RelativeLayout r2;
	RelativeLayout r3;
	RelativeLayout view = null;
	MyFragment f1; 
	MyFragment2 f2 ;
	MyFragment3 f3; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bottom_layout);

		r1 = (RelativeLayout) findViewById(R.id.layout1);
		r2 = (RelativeLayout) findViewById(R.id.layout2);
		r3 = (RelativeLayout) findViewById(R.id.layout3);

		setDefaultFragment();
	}
	private void setDefaultFragment() {
		FragmentManager fm = getFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		f1 = new MyFragment();
		transaction.add(R.id.frame_layout1, f1,"ONE");
		transaction.addToBackStack(null);
		transaction.commit();
	}
	

	@Override
	public void onMyOneBtnClick() {

		if (f2 == null)  
        {  
			f2 = new MyFragment2();  
			f2.setMyTwoBtnClickListener(this);  
        }  
		FragmentManager fm = getFragmentManager();
		FragmentTransaction tx = fm.beginTransaction();
		tx.replace(R.id.frame_layout1, f2, "TWO");
		tx.addToBackStack(null);
		tx.commit();
	}
	@Override
	public void onMyTwoBtnClick() {
		if (f3 == null)  
        {  
			f3 = new MyFragment3();  
  
        }  
		FragmentManager fm = getFragmentManager();
		FragmentTransaction tx = fm.beginTransaction();
		tx.hide(f2);
		tx.add(R.id.frame_layout1, f3, "THREE");
		tx.addToBackStack(null);
		tx.commit();		
	}
	
}
这样也实现了上述的功能。




    


   

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。