android :fragment 復(fù)用

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();??

????}??

}??

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末否灾,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鸣奔,更是在濱河造成了極大的恐慌墨技,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挎狸,死亡現(xiàn)場離奇詭異扣汪,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)锨匆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門私痹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事紊遵≌饲В” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵暗膜,是天一觀的道長匀奏。 經(jīng)常有香客問我,道長学搜,這世上最難降的妖魔是什么娃善? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮瑞佩,結(jié)果婚禮上聚磺,老公的妹妹穿的比我還像新娘。我一直安慰自己炬丸,他們只是感情好瘫寝,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著稠炬,像睡著了一般焕阿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上首启,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天暮屡,我揣著相機(jī)與錄音,去河邊找鬼毅桃。 笑死褒纲,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的钥飞。 我是一名探鬼主播莺掠,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼代承!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起渐扮,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤论悴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后墓律,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體膀估,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年耻讽,在試婚紗的時候發(fā)現(xiàn)自己被綠了察纯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖饼记,靈堂內(nèi)的尸體忽然破棺而出香伴,到底是詐尸還是另有隱情,我是刑警寧澤具则,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布即纲,位于F島的核電站,受9級特大地震影響博肋,放射性物質(zhì)發(fā)生泄漏低斋。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一匪凡、第九天 我趴在偏房一處隱蔽的房頂上張望膊畴。 院中可真熱鬧,春花似錦病游、人聲如沸唇跨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽轻绞。三九已至,卻和暖如春佣耐,著一層夾襖步出監(jiān)牢的瞬間政勃,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工兼砖, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奸远,地道東北人。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓讽挟,卻偏偏與公主長得像懒叛,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子耽梅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容