动态添加fragment与手势切换(二)(android)

接着上一个文章。

因为用的是fragment,所以我们需要建立fragment,在案例中,是在两个layout中同时添加fragment,所以我们建立了两个,为了省功夫,我沿用了我之前用过的文件。

1、fragmengt1

XML代码为:




    



JAVA代码为:


package com.example.testandroid;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class TabOfFragment01 extends Fragment {

	private TextView textView;  
    private View view;  
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		//return super.onCreateView(inflater, container, savedInstanceState);
		
		view= inflater.inflate(R.layout.tab_of_fragment_01, container,false);
		textView=(TextView)view.findViewById(R.id.textView1);
		
		String pageNo=null;
		Bundle bundle = getArguments();  
		if(null!=bundle)
			pageNo= bundle.getString("pageNo");  
		
	
		if(null!=pageNo)
		  if(!"".equals(pageNo))
		     textView.setText(" THE FRAGMENT OF "+pageNo);
		
		return view;
	}

	
}

2、fragment2

XML代码为:




    



JAVA代码为:

package com.example.testandroid;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TabOfFragment02 extends Fragment {
	private TextView textView;  
    private View view;  
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		//return super.onCreateView(inflater, container, savedInstanceState);
		view= inflater.inflate(R.layout.tab_of_fragment_02, container,false);
		textView=(TextView)view.findViewById(R.id.textView12);
		
		String pageNo=null;
		Bundle bundle = getArguments();  
		if(null!=bundle)
			pageNo= bundle.getString("pageNo");  
		
	
		if(null!=pageNo)
		  if(!"".equals(pageNo))
		     textView.setText("THE BOTTOM FRAGMENT OF "+pageNo);
		
		return view;
	}

	
}


你可能感兴趣的:(动态添加fragment与手势切换(二)(android))