color="#fafafa"/>
设置了点击和默认的时候的显示状态
最后来实现OneFm类
[java]viewplaincopyprint?
packagecom.gjn.mynavigation;
importjava.util.ArrayList;
importjava.util.List;
importandroid.os.Bundle;
importandroid.support.annotation.Nullable;
importandroid.support.v4.app.Fragment;
importandroid.support.v4.view.ViewPager;
importandroid.support.v4.view.ViewPager.OnPageChangeListener;
importandroid.util.DisplayMetrics;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.widget.HorizontalScrollView;
importandroid.widget.RadioButton;
importandroid.widget.RadioGroup;
importandroid.widget.RadioGroup.LayoutParams;
importandroid.widget.RadioGroup.OnCheckedChangeListener;
publicclassOneFmextendsFragmentimplementsOnPageChangeListener{
privateViewview;
privateRadioGrouprg_;
privateViewPagervp_;
privateHorizontalScrollViewhv_;
privateListnewsList=newArrayList();
privateOneFmAdapteradapter;
@Override
publicViewonCreateView(LayoutInflaterinflater,
@NullableViewGroupcontainer,@NullableBundlesavedInstanceState){
if(view==null){
//初始化view
view=inflater.inflate(R.layout.fm_one,container,false);
rg_=(RadioGroup)view.findViewById(R.id.one_rg);
vp_=(ViewPager)view.findViewById(R.id.one_view);
hv_=(HorizontalScrollView)view.findViewById(R.id.one_hv);
//设置RadioGroup点击事件
rg_.setOnCheckedChangeListener(newOnCheckedChangeListener(){
@Override
publicvoidonCheckedChanged(RadioGroupgroup,intid){
vp_.setCurrentItem(id);
}
});
//初始化顶部导航栏
initTab(inflater);
//初始化viewpager
initView();
}
/*
*底部导航栏切换后由于没有销毁顶部设置导致如果没有重新设置view
*导致底部切换后切回顶部页面数据会消失等bug
*以下设置每次重新创建view即可
*/
ViewGroupparent=(ViewGroup)view.getParent();
if(parent!
=null){
parent.removeView(view);
}
returnview;
}
/***
*初始化viewpager
*/
privatevoidinitView(){
ListhTabs=HTabDb.getSelected();
for(inti=0;iOneFm1fm1=newOneFm1();
Bundlebundle=newBundle();
bundle.putString("name",hTabs.get(i).getName());
fm1.setArguments(bundle);
newsList.add(fm1);
}
//设置viewpager适配器
adapter=newOneFmAdapter(getActivity().getSupportFragmentManager(),newsList);
vp_.setAdapter(adapter);
//两个viewpager切换不重新加载
vp_.setOffscreenPageLimit
(2);
//设置默认
vp_.s