最近一个正在学Android的朋友问到了一个ActionBar与Fragment的问题,于是写了一个Demo来简单的说明Fragment与ActionBar的结合。
要实现诸如WEB开发当中的Tab页切换的效果,可以使用ActionBar来结合Fragment使用。实现思路是在Android页面的头领加上一个ActionBar,其中包含多个TAB元素,每一个Tab元素对应一个Fragemnt,为Tab元素加上TabListener监听事件。这样就可以实现WEB当中的TAB切换的效果了。下面是我写的一个Demo:
首先新建Fragment并对应的新建连个xml布局文件
first.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/first"
android:text="@string/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="25sp"
/>
</LinearLayout>
second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/second"
android:text="@string/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="25sp"
/>
</LinearLayout>
对应的两个Fragment
FirstFragment
public class FirstFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.e(Constant.MESS_LOG, "FirstFragment OnCreate");
super.onCreate(savedInstanceState);
}
@Override
public void onStop() {
// TODO Auto-generated method stub
Log.e(Constant.MESS_LOG, "FirstFragment onStop!");
super.onStop();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.e(Constant.MESS_LOG, "FirstFratment OnCreateView");
return inflater.inflate(R.layout.first, container,false);
//super.onCreateView(inflater, container, savedInstanceState);
}
}
SecondFragment
public class SecondFragment extends Fragment{
private Button secondButton;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.e(Constant.MESS_LOG, "sECONDfRAGMENT Oncreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.e(Constant.MESS_LOG, "SecondFragment OnCreateView");
return inflater.inflate(R.layout.second, container, false);
//super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onStop() {
// TODO Auto-generated method stub
Log.e(Constant.MESS_LOG, "Secondfragment Onstop!");
super.onStop();
}
}
最后在主页面启动时为其添加一个ActionBar,并将Tab的监听事件加上
public class MainActivity extends Activity implements TabListener{
private Fragment fragment;
public MainActivity(){
}
public MainActivity(Fragment fragment){
this.fragment = fragment;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab firstTab = actionBar.newTab().setText("First");
Tab secondTab = actionBar.newTab().setText("second");
firstTab.setTabListener(new MainActivity((new FirstFragment())));
secondTab.setTabListener(new MainActivity(new SecondFragment()));
actionBar.addTab(secondTab);
actionBar.addTab(firstTab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.add(R.id.layout, fragment,null);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(fragment);
}
}
最后注意:
MainActivity 实现了TabListener监听事件所以添加了带参数的构造方法,但是不要往了重载一个无参数的构造方法,否则会报错。
有点困了,今天先写到这里,更详细的内容以后再补上。