码迷,mamicode.com
首页 > 其他好文 > 详细

day4.26总结_Fragment

时间:2016-05-13 04:29:06      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

一、Fragment对象

(1)Fragment 是什么

1)Activity中的模块化对象。

2)Android3.0中的一个新特性。

2Fragment 应用场合(when,why)

1)适配各种屏幕尺寸.

2)对界面中的UI内容进行模块化.

例如:

1)radiogroup+fragment+viewpager 实现底部菜单

2)actionBar(tab)+fragment+viewpager 实现tab导航

3).....

例子1:屏幕尺寸的切换显示

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

 

DisplayMetrics outMetrics=new DisplayMetrics();   获取屏幕的长宽的像素

getWindowManager().getDefaultDisplay().getMetrics(outMetrics);

int width=outMetrics.widthPixels;

int height=outMetrics.heightPixels;

 

//要借助此对象管理fragment对象

FragmentManager fm=getFragmentManager();

FragmentTransaction ft=fm.beginTransaction();

//使用不同的fragment

if(height>width){

ft.replace(R.id.layoutId, new Fragment01(),"tag");

}else{

ft.replace(R.id.layoutId, new Fragment02(),"tag");

}

//可选:将fragment添加到回退栈

//ft.addToBackStack(null);

ft.commit();

}

@Override

protected void onResume() {

super.onResume();

//根据tag找到对应的fragment

Fragment f=getFragmentManager().findFragmentByTag("tag");

Log.i("TAG", "f="+f);

}

}

 

3Fragment 对象的应用

1)类的编写(直接或间接继承fragment,无需在清单配置文件注册)

2)对象的构建(直接执行new的动作)

3)将对象添加到activity(静态方式,动态方式)

4)fragment对象与activity的交互(生命周期方法)

a)fragment: getActivity()

b)activity: findFragmentById,findFragmentByTag

注意:fragment对象的生命周期受activity对象

生命周期的影响。

例子2actionBarFragment的应用

public class MainActivity extends Activity implements TabListener {

private Fragment frg01,frg02;

private String[] tags={"A","B"};

ActionBar bar;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

bar=getActionBar();

bar.setDisplayShowHomeEnabled(false);

bar.setDisplayShowTitleEnabled(false);

//设置为tab导航

bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

//添加tab

bar.addTab(bar.newTab().setText("tab01").setTabListener(this));

bar.addTab(bar.newTab().setText("tab02").setTabListener(this));

 

if(savedInstanceState!=null){

int pos=savedInstanceState.getInt("posKey");

Fragment frg=getFragmentManager().findFragmentByTag(tags[pos]);

if(frg!=null&&!frg.isDetached()){

FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();

beginTransaction.detach(frg);

beginTransaction.commit();

}

bar.setSelectedNavigationItem(pos);

}

}

 

@Override

protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

//获得当前选中的tab的位置

int pos=bar.getSelectedNavigationIndex();

outState.putInt("posKey", pos);

}

@Override

public void onTabSelected(Tab tab, FragmentTransaction ft) {

int pos=tab.getPosition();

if(pos==0){

if(frg01==null){

frg01=new Fragment01();

ft.add(R.id.layout,frg01,tags[pos]);

}else{

ft.attach(frg01);

}

}else if(pos==1){

if(frg02==null){

frg02=new Fragment02();

ft.add(R.id.layout,frg02,tags[pos]);

}else{

ft.attach(frg02);

}

}

}

@Override

public void onTabUnselected(Tab tab, FragmentTransaction ft) {

int pos=tab.getPosition();

if(pos==0){

ft.detach(frg01);

}else if(pos==1){

ft.detach(frg02);

}

}

@Override

public void onTabReselected(Tab tab, FragmentTransaction ft) {

}

}

例子3ViewPagerFragment的应用

public class MainActivity extends FragmentActivity implements OnCheckedChangeListener, OnPageChangeListener {

private ViewPager vPager;

private RadioGroup rGroup;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

rGroup=(RadioGroup)

findViewById(R.id.radioGroup1);

rGroup.setOnCheckedChangeListener(this);

setViewPager();

}

private void setViewPager(){

vPager = (ViewPager) findViewById(R.id.viewPagerId);

vPager.setAdapter(new FrgAdapter(getSupportFragmentManager()));

vPager.setOnPageChangeListener(this);

}

class FrgAdapter extends FragmentPagerAdapter{//android.support.v4

 

public FrgAdapter(FragmentManager fm) {

super(fm);

}

        /**此方法中要返回frgment对象*/

@Override

public Fragment getItem(int positon) {

if(positon==0){

return new Fragment01();

}else if(positon==1){

return new Fragment02();

}return null;

}

@Override

public int getCount() {

return 2;

}

}

/**点击某个RadioButton会执行此方法*/

@Override

public void onCheckedChanged(RadioGroup group,int checkedId) {

//设置viewPager当前的item(fragment)

vPager.setCurrentItem(group.indexOfChild(group.findViewById(checkedId)));

}

@Override

public void onPageScrollStateChanged(int arg0) {

}

@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override

public void onPageSelected(int position) {

//选中执行的radioButton

rGroup.check(rGroup.getChildAt(position).getId());

}

}

 

4其它Fragment对象

1)ListFragment

2)DialogFragment

3)FragmentTabHost

4)......

day4.26总结_Fragment

标签:

原文地址:http://blog.csdn.net/gsd1602_yingmu/article/details/51336182

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!