import
java.util.ArrayList;
import
android.content.Context;
import
android.os.Bundle;
import
android.support.v4.app.Fragment;
import
android.support.v4.app.FragmentActivity;
import
android.support.v4.app.FragmentStatePagerAdapter;
//************************自定义pagerAdapter**********************
public
class
MviewPagerAdapter
extends
FragmentStatePagerAdapter{
private
final
ArrayList<TabsInfo> lstTabsInfo;
private
final
Context mContext;
public
static
class
TabsInfo {
private
final
Class<?> clss_;
private
final
Bundle args_;
public
TabsInfo(Class<?> clss, Bundle args) {
this .clss_
= clss;
this .args_
= args;
}
}
public
MviewPagerAdapter(FragmentActivity activity, ArrayList<TabsInfo> lstTabsInfo) {
super (activity.getSupportFragmentManager());
mContext
= activity;
this .lstTabsInfo
= lstTabsInfo;
}
@Override
public
Fragment getItem(
int
position) {
//
TODO Auto-generated method stub
TabsInfo
info = lstTabsInfo.get(position);
return
Fragment.instantiate(mContext, info.clss_.getName(), info.args_);
}
@Override
public
int
getCount() {
//
TODO Auto-generated method stub
return
lstTabsInfo.size();
}
}
//*******************应用************************
public
class
MainActivity
extends
FragmentActivity
implements
OnPageChangeListener, OnCheckedChangeListener{
private
RadioGroup mRadioGroup;
private
ViewPager mViewPager;
private
ArrayList<Integer> lstCheckIds;
private
ArrayList<TabsInfo> lstTabsInfos;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRadioGroup
= (RadioGroup)findViewById(R.id.radiogroup);
mRadioGroup.check(R.id.chengzai);
lstCheckIds
= new
ArrayList<Integer>();
for ( int
i=
0 ;
i< 5 ;
i++) {
lstCheckIds.add(R.id.chengzai+i);
}
mRadioGroup.setOnCheckedChangeListener( this );
mViewPager
= (ViewPager)findViewById(R.id.main_viewPager);
lstTabsInfos
= new
ArrayList<TabsInfo>();
//**********此处为关键代码*************
lstTabsInfos.add( new
TabsInfo(FireDisasterFragment.
class ,
null ));
lstTabsInfos.add( new
TabsInfo(SuperviseFragment.
class ,
null ));
lstTabsInfos.add( new
TabsInfo(Tab1.
class ,
null ));
lstTabsInfos.add( new
TabsInfo(Tab2.
class ,
null ));
lstTabsInfos.add( new
TabsInfo(Tab1.
class ,
null ));
MviewPagerAdapter
adapter = new
MviewPagerAdapter(
this ,
lstTabsInfos);
mViewPager.setAdapter(adapter);
mViewPager.setCurrentItem( 0 );
mViewPager.setOnPageChangeListener( this );
}
@Override
public
void
onCheckedChanged(RadioGroup group,
int
checkedId) {
//
TODO Auto-generated method stub
for ( int
i=
0 ;
i<lstCheckIds.size(); i++) {
if (lstCheckIds.get(i)
== checkedId) {
mViewPager.setCurrentItem(i);
break ;
}
}
}
@Override
public
void
onPageScrollStateChanged(
int
arg0) {
//
TODO Auto-generated method stub
}
@Override
public
void
onPageScrolled(
int
arg0,
float
arg1,
int
arg2) {
//
TODO Auto-generated method stub
}
@Override
public
void
onPageSelected(
int
position) {
//
TODO Auto-generated method stub
mRadioGroup.check(lstCheckIds.get(position));
}
}
//*********另附上
FireDisasterFragment(FragmentTabhost)关键代码**************
@Override
public
View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle
savedInstanceState) {
//
TODO Auto-generated method stub
Log.e( "fire_oncreateview" ,
"oncreateview" );
View
view = (View)inflater.inflate(R.layout.firedisaste_layout, container, false );
mTabHost
= (FragmentTabHost)view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(),
getChildFragmentManager(), R.id.realtabcontent);
View
indicator = (View)inflater.inflate(R.layout.tabwidgets_layout, null );
TextView
textView = (TextView)indicator.findViewById(R.id.tabwidget_indicator);
textView.setText( "tabwidget1" );
mTabHost.addTab(mTabHost.newTabSpec( "tab1" ).setIndicator(indicator),
Tab3. class ,
null );
View
indicator1 = (View)inflater.inflate(R.layout.tabwidgets_layout, null );
TextView
textView2 = (TextView)indicator1.findViewById(R.id.tabwidget_indicator);
textView2.setText( "tabwidget2" );
mTabHost.addTab(mTabHost.newTabSpec( "tab2" ).setIndicator(indicator1),
Tab4. class ,
null );
return
view;
}
//*********************布局代码***************************
<?xml
version= "1.0"
encoding=
"utf-8" ?>
<android.support.v4.app.FragmentTabHost
xmlns:android= "http://schemas.android.com/apk/res/android"
android:id= "@android:id/tabhost"
android:layout_width= "match_parent"
android:layout_height= "match_parent" >
<LinearLayout
android:orientation= "vertical"
android:layout_width= "match_parent"
android:layout_height= "match_parent" >
<TabWidget
android:id= "@android:id/tabs"
android:orientation= "horizontal"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:layout_weight= "0" />
<FrameLayout
android:id= "@android:id/tabcontent"
android:layout_width= "0dp"
android:layout_height= "0dp"
android:layout_weight= "0" />
<FrameLayout
android:id= "@+id/realtabcontent"
android:layout_width= "match_parent"
android:layout_height= "0dp"
android:layout_weight= "1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
|