Android ActionBar Tabs学习笔记
openkk
13年前
<p><span style="color:#ff0000;">本例主要实现用Tab切换不同的Fragment,点击View显示or隐藏ActionBar,把ActionBar 设为透明,使界面更加友好,详细代码见资源里的ActionBarTabs。</span></p> <p>ActionBar Tab主要用于Fragment之间的切换,其必须要设置ActionBar.TabListener,详细代码如下ActionBarActivity.java:</p> <pre class="brush:java; toolbar: true; auto-links: false;">import android.app.ActionBar; import android.app.Activity; import android.app.FragmentTransaction; import android.app.ActionBar.Tab; import android.os.Bundle; import android.os.CountDownTimer; import android.view.MotionEvent; import android.view.Window; public class ActionBarActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //使ActionBar变得透明 requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); setContentView(R.layout.main); final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); // remove the activity title to make space for tabs actionBar.setDisplayShowTitleEnabled(false); AFragment aFragment = new AFragment(); actionBar.addTab(actionBar.newTab().setText("Tab-A") .setTabListener(new ListenerA(aFragment))); BFragment bFragment = new BFragment(); actionBar.addTab(actionBar.newTab().setText("Tab-B") .setTabListener(new ListenerB(bFragment))); } //点击显示or隐藏ActionBar public boolean onTouchEvent(MotionEvent event){ ActionBar bar = getActionBar(); switch(event.getAction()){ case MotionEvent.ACTION_UP: if(bar.isShowing()) bar.hide(); else bar.show(); break; default: break; } return true; } private class ListenerA implements ActionBar.TabListener { private AFragment mFragment; // Called to create an instance of the listener when adding a new tab public ListenerA(AFragment fragment) { mFragment = fragment; } public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.add(R.id.fragment, mFragment, null); } public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(mFragment); } public void onTabReselected(Tab tab, FragmentTransaction ft) { // do nothing } } } private class ListenerB implements ActionBar.TabListener { private BFragment mFragment; // Called to create an instance of the listener when adding a new tab public ListenerB(BFragment fragment) { mFragment = fragment; } public void onTabSelected(Tab tab, FragmentTransaction ft) { ft.add(R.id.fragment, mFragment, null); } public void onTabUnselected(Tab tab, FragmentTransaction ft) { ft.remove(mFragment); } public void onTabReselected(Tab tab, FragmentTransaction ft) { // do nothing } } } }</pre> <p></p> 其中涉及到两个Fragment,在前面Fragment的笔记中讲过,这里就不再赘述。类AFragment实现如下,BFragment实现与这类似: <pre class="brush:java; toolbar: true; auto-links: false;">public class AFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.alayout, container, false); } }</pre> <p>文章出处:<a href="/misc/goto?guid=4959499320874291413" rel="nofollow">http://blog.csdn.net/flying_tao/article/details/6615171</a></p>