1.先來看看fragment的代碼
[java]?view plain?copy
import?java.util.ArrayList;??
import?java.util.List;??
import?com.example.fm_reuse.R;??
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?TestFm?extends?Fragment{??
private?List?list?=?new?ArrayList();??
private?int?flag;??
private?TextView?tv;??
@Override??
public?void?onCreate(Bundle?savedInstanceState)?{??
//?TODO?Auto-generated?method?stub??
super.onCreate(savedInstanceState);??
Bundle?bundle?=this.getArguments();??
if(bundle?!=?null){??
list?=?bundle.getStringArrayList("content");??
flag?=?bundle.getInt("flag");??
????????}??
????}??
@Override??
public?View?onCreateView(LayoutInflater?inflater,?ViewGroup?container,??
????????????Bundle?savedInstanceState)?{??
//?TODO?Auto-generated?method?stub??
View?view?=?inflater.inflate(R.layout.fm_test,?container,false);??
????????initView(view);??
return?view;??
????}??
public?void?initView(View?view){??
????????tv?=?(TextView)view.findViewById(R.id.tv);??
????????tv.setText(list.get(flag));??
????}??
public?static?TestFm?newInstance(List?contentList,int?flag){??
Bundle?bundle?=new?Bundle();??
bundle.putStringArrayList("content",?(ArrayList)?contentList);??
bundle.putInt("flag",?flag);??
TestFm?testFm?=new?TestFm();??
????????testFm.setArguments(bundle);??
return?testFm;??
????}??
}??
fm_test.xml
[html]?view plain?copy
android:layout_width="match_parent"??
android:layout_height="match_parent"??
android:orientation="vertical"?>??
android:id="@+id/tv"??
android:layout_width="match_parent"??
android:layout_height="match_parent"??
android:gravity="center"??
android:textSize="20sp"/>??
上面的代碼很簡單硕噩,newInstance這個方法在實(shí)例化fragment時將會用到,傳入需要的數(shù)據(jù)并通過setArguments方法將數(shù)據(jù)保存秉颗,這樣巨朦,當(dāng)跳轉(zhuǎn)到當(dāng)前fragment時就可以在onCreate方法中通過getArguments拿到數(shù)據(jù),就是這么的一個過程赶撰。下面我們來看一下MainActivity的代碼:
[java]?view plain?copy
import?java.util.ArrayList;??
import?java.util.List;??
import?android.graphics.Color;??
import?android.os.Bundle;??
import?android.support.v4.app.FragmentActivity;??
import?android.support.v4.view.ViewPager;??
import?android.view.Display;??
import?android.view.Gravity;??
import?android.view.View;??
import?android.view.WindowManager;??
import?android.widget.HorizontalScrollView;??
import?android.widget.LinearLayout;??
import?android.widget.LinearLayout.LayoutParams;??
import?android.widget.TextView;??
import?com.example.fm_reuse.adapter.FragmentVPAdapter;??
import?com.example.fm_reuse.fragment.TestFm;??
public?class?MainActivity?extends?FragmentActivity?{??
private?List?titleList?=?new?ArrayList();?//標(biāo)題鏈表??
private?List?contentList?=?new?ArrayList();?//內(nèi)容鏈表??
private?List?fragmentList?=?new?ArrayList();?//碎片鏈表??
private?int?screenWidth;?//屏幕寬度??
private?ViewPager?vp;??
private?HorizontalScrollView?scrollView;??
private?List?textViews?=?new?ArrayList();??
@Override??
protected?void?onCreate(Bundle?savedInstanceState)?{??
super.onCreate(savedInstanceState);??
????????setContentView(R.layout.activity_main);??
initList();//初始化內(nèi)容和標(biāo)題??
//獲取屏幕寬度??
WindowManager?windowManager?=this.getWindowManager();??
????????Display?display?=?windowManager.getDefaultDisplay();??
????????screenWidth?=?display.getWidth();??
????????vp?=?(ViewPager)findViewById(R.id.viewPager);??
????????scrollView?=?(HorizontalScrollView)findViewById(R.id.scrollView);??
//有多少個標(biāo)題就有多少個碎片舌镶,動態(tài)添加??
for(int?i=0;i
TestFm?testFm?=new?TestFm().newInstance(contentList,?i);??
????????????fragmentList.add(testFm);??
????????}??
//初始化導(dǎo)航欄布局??
LinearLayout?navigationLl?=new?LinearLayout(this);??
LinearLayout.LayoutParams?mParams?=new?LayoutParams(LayoutParams.MATCH_PARENT,?LayoutParams.MATCH_PARENT);??
????????navigationLl.setLayoutParams(mParams);??
????????navigationLl.setOrientation(LinearLayout.HORIZONTAL);??
????????navigationLl.setBackgroundColor(Color.GREEN);??
//往導(dǎo)航欄添加標(biāo)題??
if(titleList.size()?<=?3){?//標(biāo)題欄小于4個時,平分屏幕寬度??
LinearLayout.LayoutParams?params?=new?LayoutParams(LayoutParams.MATCH_PARENT,?LayoutParams.MATCH_PARENT);??
params.weight?=1;??
for?(int?i?=?0;?i?<?titleList.size();?i++)?{??
final?TextView?tv?=?new?TextView(this);??
????????????????????tv.setText(titleList.get(i));??
????????????????????tv.setGravity(Gravity.CENTER);??
final?int?finalI?=?i;??
tv.setOnClickListener(new?View.OnClickListener()?{??
@Override??
public?void?onClick(View?view)?{??
????????????????????????????tv.setTextColor(Color.RED);??
????????????????????????????vp.setCurrentItem(finalI);??
????????????????????????}??
????????????????????});??
????????????????????textViews.add(tv);??
navigationLl.addView(tv,?params);//往導(dǎo)航欄添加標(biāo)題??
????????????????}??
????????}??
if(titleList.size()?>?3){?//標(biāo)題大于四個豪娜,重新規(guī)劃textView大小??
LinearLayout.LayoutParams?params1?=new?LayoutParams(LayoutParams.MATCH_PARENT,?LayoutParams.MATCH_PARENT);??
params1.width?=300;??
????????????params1.height?=?LayoutParams.MATCH_PARENT;??
for?(int?i?=?0;?i?<?titleList.size();?i++)?{??
final?TextView?tv?=?new?TextView(this);??
????????????????tv.setText(titleList.get(i));??
????????????????tv.setGravity(Gravity.CENTER);??
final?int?finalI?=?i;??
tv.setOnClickListener(new?View.OnClickListener()?{??
@Override??
public?void?onClick(View?view)?{??
????????????????????????tv.setTextColor(Color.RED);??
????????????????????????vp.setCurrentItem(finalI);??
????????????????????}??
????????????????});??
????????????????textViews.add(tv);??
????????????????navigationLl.addView(tv,?params1);??
????????????}??
????????}??
//第一個標(biāo)題默認(rèn)紅色??
if?(textViews?!=?null?&&?textViews.size()?>?0)?{??
textViews.get(0).setTextColor(Color.RED);??
????????????}??
scrollView.addView(navigationLl);//往scrollView添加導(dǎo)航欄??
vp.setAdapter(new?FragmentVPAdapter(getSupportFragmentManager(),?(ArrayList)?fragmentList));??
vp.setOnPageChangeListener(new?ViewPager.OnPageChangeListener()?{??
@Override??
public?void?onPageScrolled(int?position,?float?positionOffset,?int?positionOffsetPixels)?{??
????????????????}??
@Override??
public?void?onPageSelected(int?position)?{??
????????????????????setSelect(position);??
if(position>=4){??
scrollView.scrollBy((int)?(0.25*screenWidth),?0);??
}else?if(position<4){??
scrollView.scrollBy(-(int)?(0.25*screenWidth),?0);??
????????????????????}??
????????????????}??
@Override??
public?void?onPageScrollStateChanged(int?state)?{??
????????????????}??
????????????});??
????}??
public?void?setSelect(int?position)?{??
????????????vp.setCurrentItem(position);??
for?(int?i?=?0;?i?<?textViews.size();?i++)?{??
????????????????textViews.get(i).setTextColor(Color.BLACK);??
????????????}??
????????????textViews.get(position).setTextColor(Color.RED);??
????????}??
public?void?initList(){??
//添加標(biāo)題??
titleList.add("標(biāo)題一");??
titleList.add("標(biāo)題二");??
titleList.add("標(biāo)題三");??
titleList.add("標(biāo)題四");??
titleList.add("標(biāo)題五");??
titleList.add("標(biāo)題六");??
//添加內(nèi)容??
contentList.add("頁面一");??
contentList.add("頁面二");??
contentList.add("頁面三");??
contentList.add("頁面四");??
contentList.add("頁面五");??
contentList.add("頁面六");??
????}??
}??
注釋也寫得很清楚了餐胀,所以就不多說了,相信大家都能看懂瘤载。
activity_main.xml
[html]?view plain?copy
xmlns:tools="http://schemas.android.com/tools"??
android:layout_width="match_parent"??
android:layout_height="match_parent"??
android:orientation="vertical"??
tools:context=".MainActivity"?>??
android:id="@+id/scrollView"??
android:layout_width="match_parent"??
android:layout_height="50dp"??
android:fillViewport="true"??
android:scrollbars="none"??
>??
android:id="@+id/viewPager"??
android:layout_width="match_parent"??
android:layout_height="match_parent"?>??
最后把viewpager的適配器的代碼也貼出來:
import?android.support.v4.app.Fragment;??
import?android.support.v4.app.FragmentManager;??
import?android.support.v4.app.FragmentPagerAdapter;??
import?android.support.v4.app.FragmentTransaction;??
import?java.util.ArrayList;??
import?com.example.fm_reuse.fragment.TestFm;??
/**
?*?Created?by?_H_JY?on?2015/11/23.
?*/??
public?class?FragmentVPAdapter?extends?FragmentPagerAdapter?{??
private?ArrayList?fragments;??
private?FragmentManager?fm;??
public?FragmentVPAdapter(FragmentManager?fm,?ArrayList?fragments)?{??
super(fm);??
this.fm?=?fm;??
this.fragments?=?fragments;??
????}??
public?void?setFragments(ArrayList?fragments)?{??
if(this.fragments?!=?null){??
????????????FragmentTransaction?ft?=?fm.beginTransaction();??
for(Fragment?f:this.fragments){??
????????????????ft.remove(f);??
????????????}??
????????????ft.commit();??
ft=null;??
????????????fm.executePendingTransactions();??
????????}??
this.fragments?=?fragments;??
????????notifyDataSetChanged();??
????}??
@Override??
public?int?getItemPosition(Object?object)?{??
return?POSITION_NONE;??
????}??
@Override??
public?Fragment?getItem(int?arg0)?{??
return?fragments.get(arg0);??
????}??
@Override??
public?int?getCount()?{??
return?fragments.size();??
????}??
}??