封装UI和代码利器—Fragment(六)

                Fragment 与回退栈 (导航)  

回退栈就不多说,看个例子就什么都明白了。

package com.example.fragmentnovigation_01;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentManager.OnBackStackChangedListener;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

@SuppressLint("NewApi")
public class FragmentNovigationActivity extends Activity implements OnBackStackChangedListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragment_novigation);
		nextFragment(false);
		onBackStackChanged();
	}

	public void onClick_nextPage(View view){
		nextFragment(true);
	}
	public void onClick_prePage(View view){
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.popBackStack();
	}
	private void nextFragment(boolean backStackFlag){
		
		FragmentManager fragmentManager = getFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		FragmentPage  fragmentPage = new FragmentPage();
		transaction.add(R.id.fragment_container, fragmentPage);
		if(backStackFlag){
			transaction.addToBackStack(null);
		}
		transaction.commit();
		fragmentManager.addOnBackStackChangedListener(this);
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.fragment_novigation, menu);
		return true;
	}

	@Override
	public void onBackStackChanged() {
		// TODO Auto-generated method stub
		setTitle("当前第"+(getFragmentManager().getBackStackEntryCount()+1)+"页");
	}

}



package com.example.fragmentnovigation_01;


import java.util.Random;


import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;


@SuppressLint("NewApi")
public class FragmentPage extends Fragment{


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View  view = inflater.inflate(R.layout.myfragment,container, false);
		EditText editText = (EditText)view.findViewById(R.id.edittext);
		editText.setText(String.valueOf(Math.abs(new Random().nextLong())));
		return view;
	}
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#aa0000"> <EditText android:id="@+id/edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一页" /></LinearLayout>



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#0000aa"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:background="#00aa00"
        >

        <Button
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"
            android:onClick="onClick_prePage"
            android:text="上一页" android:layout_weight="1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick_nextPage"
            android:text="下一页" android:layout_weight="1" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0000aa"
         />

</LinearLayout>


代码很详细,可以复习了!

你可能感兴趣的:(封装UI和代码利器—Fragment(六))