Android ActionBar中进行Fragment切换

来源:互联网 发布:淘宝国际转运cn dpd 编辑:程序博客网 时间:2024/05/16 18:58

非常感谢小源求学的博文在ActionBar中进行Fragment之间的切换

博文链接地址:http://www.cnblogs.com/hanyuan/archive/2012/04/10/ActionBar_Fragments.html

博文中对ActionBar与Fragment切换写的很详细,由此受启发写下自己的实践过程。再次感谢小源求学


效果图:



实现思路:

1.新建每个Fragment,并与相对应的Layout联系起来。

2.获得Application的ActionBar,并设置相关操作。

3.声明Tab,添加监听TabListener。

4.添加Tab到ActionBar中。


实现源码:

1.新建Fragment(以DayFragment.java为例,其余不在赘述)

DayFragment.java

package com.example.meetingsystem.fragment;import com.example.meetingsystem.R;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class DayFragment extends Fragment {    @Override    public void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {                return inflater.inflate(R.layout.day_fragment, container, false);    }}


day_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".DayFragment" >    <TextView        android:id="@+id/day_fragment_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="I am Dayfragment" /></RelativeLayout>

2.声明ActionBar
 InquireActivity.java

package com.example.meetingsystem;import com.example.meetingsystem.fragment.DayFragment;import com.example.meetingsystem.fragment.MonthFragment;import com.example.meetingsystem.fragment.YearFragment;import android.app.ActionBar;import android.app.ActionBar.Tab;import android.app.Activity;import android.app.Fragment;import android.app.FragmentTransaction;import android.os.Bundle;import android.view.Menu;public class InquireActivity extends Activity{    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.inquire);        //字符串        String main_day = getResources().getString(R.string.main_day);        String main_month = getResources().getString(R.string.main_month);        String main_year = getResources().getString(R.string.main_year);                //添加ActionBar        ActionBar actionBar = getActionBar();         //设置ActionBar的操作模型        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);         //声明Tab        Tab dayTab = actionBar.newTab().setText(main_day);         Tab monthTab = actionBar.newTab().setText(main_month);         Tab yearTab = actionBar.newTab().setText(main_year);                 //声明Fragment        Fragment dayfragment = new DayFragment();        Fragment monthfragment = new MonthFragment();        Fragment yearfragment = new YearFragment();                //添加监听        dayTab.setTabListener(new MyTabsListener(dayfragment));        monthTab.setTabListener(new MyTabsListener(monthfragment));        yearTab.setTabListener(new MyTabsListener(yearfragment));                //将Tab加入ActionBar中        actionBar.addTab(dayTab);        actionBar.addTab(monthTab);        actionBar.addTab(yearTab);            }    protected class MyTabsListener implements ActionBar.TabListener     {        private Fragment fragment;         public MyTabsListener(Fragment fragment)         {             this.fragment = fragment;         }         @Override        public void onTabSelected(Tab tab, FragmentTransaction ft) {            //添加fragment到layout fragment_context中            ft.add(R.id.fragment_context, fragment,null);        }        @Override        public void onTabReselected(Tab tab, FragmentTransaction ft) {            // TODO Auto-generated method stub        }        @Override        public void onTabUnselected(Tab tab, FragmentTransaction ft) {            // TODO Auto-generated method stub            ft.remove(fragment);        }    }         @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.inquire, menu);        return true;    }}


inquire.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/fragment_context"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".Inquire" ></RelativeLayout>


文笔不好,敬请见谅!博文中有错误或者笔误的地方,请指正!

原创粉丝点击