android的Tabs导航

来源:互联网 发布:网络歌手伤感歌曲大全 编辑:程序博客网 时间:2024/06/13 05:15

学了好几天的tabs导航,终于是把这个demo弄出来了。

写Tabs导航只需写三个东西就行。

1.TabListener.JAVA 这个是点击tab的监听,继承ActionBar.TabListener接口,写个构造函数,再重写他的方法就可以了。

2.artistsFragment和albumsFragment,这两个是你页面要展示的内容,有几个tab写几个。

3.在MainActivity中的onCreate中实现Tab,只要

ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Fragment artistsFragment = new ArtistsFragment();
actionBar.addTab(actionBar.newTab().setText("artists").setTabListener(new MyTabListener(artistsFragment)));
Fragment albumsFragment = new AlbumsFragment();
actionBar.addTab(actionBar.newTab().setText("albums").setTabListener(new MyTabListener(albumsFragment)));

自己写这几行代码就行。

1.MyTabListener.java

package com.example.he;import android.annotation.SuppressLint;import android.app.ActionBar;import android.app.ActionBar.Tab;import android.app.Fragment;import android.app.FragmentTransaction;@SuppressLint("NewApi")public class MyTabListener implements ActionBar.TabListener{private Fragment mFragment;public MyTabListener(Fragment fragment){mFragment = fragment;}@Overridepublic void onTabReselected(Tab tab, FragmentTransaction ft) {// TODO Auto-generated method stub}@Overridepublic void onTabSelected(Tab tab, FragmentTransaction ft) {// TODO Auto-generated method stubft.add(android.R.id.content,mFragment,null);}@Overridepublic void onTabUnselected(Tab tab, FragmentTransaction ft) {// TODO Auto-generated method stubft.remove(mFragment);}}

2.ArtistsFragment.java和AlbumsFragment.java

package com.example.he;import android.annotation.SuppressLint;import android.app.Fragment;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.LinearLayout;import android.widget.TextView;@SuppressLint("NewApi")public class ArtistsFragment extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){TextView textView = new TextView(getActivity());textView.setText("artists");textView.setGravity(Gravity.CENTER_HORIZONTAL);LinearLayout layout = new LinearLayout(getActivity());LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);layout.addView(textView,params);return layout;}}

package com.example.he;import android.annotation.SuppressLint;import android.app.Fragment;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.ViewGroup.LayoutParams;import android.widget.LinearLayout;import android.widget.TextView;@SuppressLint("NewApi")public class AlbumsFragment extends Fragment{public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){TextView textView = new TextView(getActivity());textView.setText("albums");textView.setGravity(Gravity.CENTER_HORIZONTAL);LinearLayout layout = new LinearLayout(getActivity());LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);layout.addView(textView,params);return layout;}}

3.MainActivity.java

package com.example.he;import android.support.v7.app.ActionBarActivity;import android.annotation.SuppressLint;import android.app.ActionBar;import android.app.Fragment;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;public class MainActivity extends ActionBarActivity {@SuppressLint("NewApi")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ActionBar actionBar = getActionBar();actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);Fragment artistsFragment = new ArtistsFragment();actionBar.addTab(actionBar.newTab().setText("artists").setTabListener(new MyTabListener(artistsFragment)));Fragment albumsFragment = new AlbumsFragment();actionBar.addTab(actionBar.newTab().setText("albums").setTabListener(new MyTabListener(albumsFragment)));}@Overridepublic 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;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}


0 0
原创粉丝点击