Android毕设之Fragment+TabHost(三)

Fragment + TabHost + RadioGroup 结合使用 , 实现底菜单的效果!

1. 使用FragMent是因为 4.0.3之后 ,摒弃了TabActivity这种用法,

Demo 效果图:

Android毕设之Fragment+TabHost(三)_第1张图片


先上布局XML R.layout.activity_main



    

    	
    	
    	    
			    	
    	
    
    	    
    	    
    	    
    	    
    	    
    	
    		 
	    	
	
	    		
	    		
	    		
	    		
	    		

	    	
	    			

	    	
  
    


这里有个Fragment标签

android:name="com.example.coolsmile.fragment.HomeFragment"

这里的android:name="具体的Fragment的实现类"


这里我的Acitivty继承了FargmentActivity,onCreate方法里

setContentView(R.layout.activity_main);
InitUI(); 

private void InitUI(){

// get Resource R.string
InitUIString();
// set Message Number
TextView main_tab_new_message=(TextView) findViewById(R.id.main_tab_new_message);
main_tab_new_message.setVisibility(View.VISIBLE);
main_tab_new_message.setText("6");


tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();

tabHost.addTab(
tabHost.newTabSpec(main_home).setIndicator(main_home).setContent(R.id.HomeFragment)
);
tabHost.addTab(
tabHost.newTabSpec(main_my_info).setIndicator(main_my_info).setContent(R.id.fragment_tab2)
);
tabHost.addTab(
tabHost.newTabSpec(main_news).setIndicator(main_news).setContent(R.id.fragment_tab3)
);
tabHost.addTab(
tabHost.newTabSpec(main_search).setIndicator(main_search).setContent(R.id.fragment_tab4)
);
tabHost.addTab(
tabHost.newTabSpec(main_settings).setIndicator(main_settings).setContent(R.id.fragment_tab5)
);

tabHost.setCurrentTab(0);

InitClickListener();
}

private void InitClickListener(){

RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.main_tab_home:
tabHost.setCurrentTabByTag(main_home);
break;
case R.id.main_tab_info:
tabHost.setCurrentTabByTag(main_my_info);
break;
case R.id.main_tab_news:
tabHost.setCurrentTabByTag(main_news);
break;
case R.id.main_tab_search:
tabHost.setCurrentTabByTag(main_search);
break;
case R.id.main_tab_settings:
tabHost.setCurrentTabByTag(main_settings);
break;
default:
tabHost.setCurrentTabByTag(main_home);
}
}
});
}
@Override
public void onClick(View v) {


}
public MainHeader getHeader(){
return this.header;
}
private void InitUIString(){

main_home = getResources().getString(R.string.main_home);
main_my_info = getResources().getString(R.string.main_my_info);
main_news = getResources().getString(R.string.main_news);
main_search = getResources().getString(R.string.main_search);
main_settings = getResources().getString(R.string.main_settings);
}

这里实例了一个效果图的HomeFragMent

public class HomeFragment extends Fragment{

	View view;
 
	String[] presidents = { 
		        "Dwight D. Eisenhower", 
		        "John F. Kennedy", 
		        "Lyndon B. Johnson", 
		        "Richard Nixon", 
		        "Gerald Ford", 
		        "Jimmy Carter", 
		        "Ronald Reagan", 
		        "George H. W. Bush", 
		        "Bill Clinton", 
		        "George W. Bush", 
		        "Barack Obama" 
		    }; 
	 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
				// TODO Auto-generated method stub
				view = inflater.inflate(R.layout.fragment_tab1,container,false);
				Init();
				return view ;
	}
	
	protected void Init(){
				
		ListView list = (ListView)view.findViewById(android.R.id.list);
		
		list.setAdapter(new ArrayAdapter(getActivity(), 
	            android.R.layout.simple_list_item_1, presidents));
		list.setOnItemClickListener(listener);
	}
	
	private OnItemClickListener listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(getActivity(), ListContainActivity.class);
         	
         	intent.putExtra("PRESIDENTS_CONTENT",presidents[position]);

         	getActivity().startActivity(intent);    
		}	
	};
	
}


点击: 下载DEMO







你可能感兴趣的:(Android毕设之Fragment+TabHost(三))