欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android利用Fragment实现Tab选项卡效果

程序员文章站 2024-03-06 18:13:38
利用fragment实现tab选项卡效果:  将radiogroup与fragment集合,实现tab选项卡效果,这里面最关键的几个文件: ...

利用fragment实现tab选项卡效果: 

将radiogroup与fragment集合,实现tab选项卡效果,这里面最关键的几个文件: 

1.fragmenttabadapter类:

/**
 *@description: 
 *@author:nate robinson 
 *@since:2015-2-12
 */
public class fragmenttabadapter implements radiogroup.oncheckedchangelistener
{
 private list<fragment> fragments; // 一个tab页面对应一个fragment

 private radiogroup rgs; // 用于切换tab

 private activity activity; // fragment所属的activity

 private int fragmentcontentid; // activity中所要被替换的区域的id

 private int currenttab; // 当前tab页面索引

 private onrgsextracheckedchangedlistener onrgsextracheckedchangedlistener; // 用于让调用者在切换tab时候增加新的功能

 public fragmenttabadapter(activity activity, list<fragment> fragments, int fragmentcontentid, radiogroup rgs)
 {
  this.fragments = fragments;
  this.rgs = rgs;
  this.activity = activity;
  this.fragmentcontentid = fragmentcontentid;

  // 默认显示第一页
  android.app.fragmenttransaction ft = activity.getfragmentmanager().begintransaction();
  ft.add(fragmentcontentid, fragments.get(0));
  ft.commit();

  rgs.setoncheckedchangelistener(this);

 }

 @override
 public void oncheckedchanged(radiogroup radiogroup, int checkedid)
 {
  for(int i = 0; i < rgs.getchildcount(); i++)
  {
   if(rgs.getchildat(i).getid() == (checkedid))
   {
    fragment fragment = fragments.get(i);
    fragmenttransaction ft = activity.getfragmentmanager().begintransaction();
    getcurrentfragment().onpause(); // 暂停当前tab
    // getcurrentfragment().onstop(); // 暂停当前tab

    if(fragment.isadded())
    {
     // fragment.onstart(); // 启动目标tab的onstart()
     fragment.onresume(); // 启动目标tab的onresume()
    }
    else
    {
     ft.add(fragmentcontentid, fragment);
    }
    showtab(i); // 显示目标tab
    ft.commit();

    // 如果设置了切换tab额外功能功能接口
    if(null != onrgsextracheckedchangedlistener)
    {
     onrgsextracheckedchangedlistener.onrgsextracheckedchanged(radiogroup, checkedid, i);
    }

   }
  }

 }

 /**
  * 切换tab
  * @param idx
  */
 private void showtab(int idx)
 {
  for(int i = 0; i < fragments.size(); i++)
  {
   fragment fragment = fragments.get(i);
   fragmenttransaction ft = activity.getfragmentmanager().begintransaction();
   if(idx == i)
   {
    ft.show(fragment);
   }
   else
   {
    ft.hide(fragment);
   }
   ft.commit();
  }
  currenttab = idx; // 更新目标tab为当前tab
 }

 public int getcurrenttab()
 {
  return currenttab;
 }

 public fragment getcurrentfragment()
 {
  return fragments.get(currenttab);
 }

 public onrgsextracheckedchangedlistener getonrgsextracheckedchangedlistener()
 {
  return onrgsextracheckedchangedlistener;
 }

 public void setonrgsextracheckedchangedlistener(onrgsextracheckedchangedlistener onrgsextracheckedchangedlistener)
 {
  this.onrgsextracheckedchangedlistener = onrgsextracheckedchangedlistener;
 }

 /**
  * 切换tab额外功能功能接口
  */
 public interface onrgsextracheckedchangedlistener
 {
  void onrgsextracheckedchanged(radiogroup radiogroup, int checkedid, int index);
 }
} 

2.activity_main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@android:color/white"
 android:orientation="vertical" >

 <linearlayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <framelayout
   android:id="@+id/tab_content"
   android:layout_width="fill_parent"
   android:layout_height="0dp"
   android:layout_weight="1.0"
   android:background="#77557799" />

  <radiogroup
   android:id="@+id/tabs_rg"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   android:orientation="horizontal"
   android:paddingbottom="7dp"
   android:paddingtop="7dp" >

   <radiobutton
    android:id="@+id/tab_rb_a"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
    android:background="@drawable/selector_tab"
    android:button="@null"
    android:checked="true"
    android:drawabletop="@drawable/tablatestalert"
    android:gravity="center"
    android:singleline="true"
    android:text="tab1"
    android:textcolor="#000000"
    android:textsize="13sp" />

   <radiobutton
    android:id="@+id/tab_rb_b"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
    android:background="@drawable/selector_tab"
    android:button="@null"
    android:drawabletop="@drawable/tabsearch"
    android:gravity="center"
    android:singleline="true"
    android:text="tab2"
    android:textcolor="#000000"
    android:textsize="13sp" />

   <radiobutton
    android:id="@+id/tab_rb_c"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
    android:background="@drawable/selector_tab"
    android:button="@null"
    android:drawabletop="@drawable/tabrecommd"
    android:gravity="center"
    android:singleline="true"
    android:text="tab3"
    android:textcolor="#000000"
    android:textsize="13sp" />
  </radiogroup>
 </linearlayout>

</linearlayout>

3.mainactivity类:

/**
 *@description: 
 *@author:nate robinson 
 *@since:2015-2-12
 */
public class mainactivity extends activity
{
 private radiogroup rgs;

 private list<fragment> fragments = new arraylist<fragment>();

 private int index;

 private static boolean isexit = false;

 private static boolean hastask = false;

 private radiobutton button;

 private timer texit = new timer();

 private timertask task = new timertask()
 {
  @override
  public void run()
  {
   isexit = false;
   hastask = true;
  }
 };

 private fragmenttabadapter tabadapter;

 @override
 public void oncreate(bundle savedinstancestate)
 {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  fragments.add(new fragmentone());
  fragments.add(new fragmenttwo());
  fragments.add(new fragmentthree());

  rgs = (radiogroup) findviewbyid(r.id.tabs_rg);

  button = (radiobutton) findviewbyid(r.id.tab_rb_a);

  tabadapter = new fragmenttabadapter(this, fragments, r.id.tab_content, rgs);
  tabadapter.setonrgsextracheckedchangedlistener(new fragmenttabadapter.onrgsextracheckedchangedlistener()
  {
   @override
   public void onrgsextracheckedchanged(radiogroup radiogroup, int checkedid, int index)
   {
    // 利用这个index可以实现,如果不是在主页,按第一遍让它先跳转到第一个fragment
    system.out.println("extra---- " + index + " checked!!! ");
    mainactivity.this.index = index;
   }
  });
 }

 @override
 public boolean onkeydown(int keycode, keyevent event)
 {
  if(keycode == keyevent.keycode_back)
  {
   if(index == 0)
   {
    if(isexit == false)
    {
     isexit = true;
     toast.maketext(mainactivity.this, "再按一次退出程序", toast.length_short).show();
     if(!hastask)
     {
      texit.schedule(task, 1000);
     }
    }
    else
    {
     finish();
     system.exit(0);
    }
   }
   else
   {
    // 如果不是在主页,按第一遍让它先跳转到第一个fragment
    // 利用上面的check方法会导致oncheckedchanged方法被执行多次
    // rgs.check(button.getid());
    button.setchecked(true);
   }

  }
  return false;
 }
}

为了能同时学习fragment生命周期,我在fragment每个生命周期方法中加入了log打印,一个有三个这样的fragment,我只贴出其中一个代码,其余和这个一样:

/**
 *@description: 
 *@author:nate robinson 
 *@since:2015-2-12
 */
public class fragmentone extends basefragment
{

 private view view;
 

 @override
 public void onattach(activity activity)
 {
  super.onattach(activity);
  log.d(tag, "1==>fragmentone=>onattach");
 }

 @override
 public void oncreate(bundle savedinstancestate)
 {
  log.d(tag, "2==>fragmentone==>oncreate");
  super.oncreate(savedinstancestate);
 }

 @override
 public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate)
 {
  log.d(tag, "3==>fragmentone==>oncreateview");
  view = inflater.inflate(r.layout.fragment1, container, false);
  textview tv = (textview) view.findviewbyid(r.id.tv);
  tv.settext("fragmentone");
  return view;
 }

 @override
 public void onactivitycreated(bundle savedinstancestate)
 {
  log.d(tag, "4==>fragmentone==>onactivitycreated");
  super.onactivitycreated(savedinstancestate);
 }

 @override
 public void onstart()
 {
  log.d(tag, "5==>fragmentone==>onstart");
  super.onstart();
 }

 @override
 public void onresume()
 {
  log.d(tag, "6==>fragmentone==>onresume");
  super.onresume();
 }

 @override
 public void onpause()
 {
  log.d(tag, "7==>fragmentone==>onpause");
  super.onpause();
 }

 @override
 public void onstop()
 {
  log.d(tag, "8==>fragmentone==>onstop");
  super.onstop();
 }

 @override
 public void ondestroyview()
 {
  log.d(tag, "9==>fragmentone==>ondestroyview");
  super.ondestroyview();
 }

 @override
 public void ondestroy()
 {
  log.d(tag, "10==>fragmentone==>ondestroy");
  super.ondestroy();
 }

 @override
 public void ondetach()
 {
  log.d(tag, "11==>fragmentone==>ondetach");
  super.ondetach();
 }

 @override
 public void onviewcreated(view view, bundle savedinstancestate)
 {

  log.d(tag, "fragmentone==>onviewcreated");
  super.onviewcreated(view, savedinstancestate);
 }
}

在编写过程中,我为了实现如果不是在主页,按第一遍让它先跳转到第一个fragment这个效果,一开始使用了rgs.check(button.getid());这个方法,结果发现,这样的话oncheckedchanged监听事件会执行两次,后来改为button.setchecked(true);这个方法之后,成功解决了这个问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。