1、布局界面:

                                                            

2、tab选项卡布局

<?xml version="1.0" encoding="utf-8"?>                    
3、主activity的代码:

public class TestActivity extends TabActivity  {protected TabHost tabHost;private static final String TAB_PAGE1="page1";private static final String TAB_PAGE2="page2";private static final String TAB_PAGE3="page3";private static final String TAB_PAGE4="page4";private int tab_last_position;//通过TabWidget可以获取选项卡的布局private TabWidget mTabWidget; private static final int SWIPE_MIN_DISTANCE = 120;private static final int SWIPE_MAX_OFF_PATH = 250;private static final int SWIPE_THRESHOLD_VELOCITY = 200;private GestureDetector gestureDetector;View.OnTouchListener gestureListener; private static int maxTabIndex = 3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.apply_main);                tabHost = getTabHost();               TabSpec tabSpec = tabHost.newTabSpec(TAB_PAGE1);        tabSpec.setIndicator(createTabView("音乐"));        Intent intent = new Intent(this, GovernmentActivity.class);        tabSpec.setContent(intent);        tabHost.addTab(tabSpec);        tabSpec = tabHost.newTabSpec(TAB_PAGE2);        intent = new Intent(this, ClassifyActivity.class);        tabSpec.setContent(intent);        tabSpec.setIndicator(createTabView("电影"));        tabHost.addTab(tabSpec);        tabSpec = tabHost.newTabSpec(TAB_PAGE3);        tabSpec.setIndicator(createTabView("电视剧"));        intent = new Intent(this, PaihangActivity.class);        tabSpec.setContent(intent);        tabHost.addTab(tabSpec);        //        tabSpec = tabHost.newTabSpec(TAB_PAGE4);        tabSpec.setIndicator(createTabView("书籍"));        intent = new Intent(this, FirstTestActivity.class);        tabSpec.setContent(intent);        tabHost.addTab(tabSpec);        //默认使其选中        tabHost.setCurrentTab(0);        tab_last_position=0;        View view =  tabHost.getCurrentTabView();ImageView imageView = (ImageView) view.findViewById(R.id.tab_line);TextView mTextView = (TextView) view.findViewById(R.id.tab_name);imageView.setVisibility(View.VISIBLE);mTextView.setTextColor(Color.GREEN);mTabWidget = tabHost.getTabWidget();         tabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {ImageView imageView = null;Logger.getLogger().i("------>" + tab_last_position);//获取到视图,可以对其进行样式的改变View lastView = mTabWidget.getChildAt(tab_last_position);imageView = (ImageView) lastView.findViewById(R.id.tab_line);TextView mTextView = (TextView) lastView.findViewById(R.id.tab_name);if (imageView!=null) {imageView.setVisibility(View.GONE);mTextView.setTextColor(getResources().getColor(R.color.white));}View view =  tabHost.getCurrentTabView();imageView = (ImageView) view.findViewById(R.id.tab_line);mTextView = (TextView) view.findViewById(R.id.tab_name);imageView.setVisibility(View.VISIBLE);mTextView.setTextColor(Color.GREEN);mTabWidget = tabHost.getTabWidget();tab_last_position = tabHost.getCurrentTab(); }});                        //滑动事件        gestureDetector = new GestureDetector(new TabGestureDetector());  //        gestureListener = new View.OnTouchListener() {  //            public boolean onTouch(View v, MotionEvent event) {  //                if (gestureDetector.onTouchEvent(event)) {  //                    return true;  //                }  //                return false;  //            }  //        };              }        // 创建tab标签    protected View createTabView(String name) {        View tabView = getLayoutInflater().inflate(R.layout.tab_background, null);        TextView textView = (TextView) tabView.findViewById(R.id.tab_name);// 找到textview控件        textView.setText(name);        return tabView;    }            // 左右滑动刚好页面也有滑动效果      private class TabGestureDetector extends SimpleOnGestureListener {          @Override          public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,                  float velocityY) {                         try {                  if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)                      return false;                  // right to left swipe                  int currentView = tabHost.getCurrentTab();                                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE                          && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {                      Log.i("test", "right" +currentView);                      if (currentView SWIPE_MIN_DISTANCE                          && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {                      Log.i("test", "left" +currentView);                      if (currentView > 0) {                       currentView--;                       tabHost.setCurrentTab(currentView);                      }                                    }              } catch (Exception e) {              e.printStackTrace();            }              return false;          }      }        @Override      public boolean dispatchTouchEvent(MotionEvent event) {          if (gestureDetector.onTouchEvent(event)) {          Log.i("test", "---------------dispatch ... ");              event.setAction(MotionEvent.ACTION_CANCEL);          }          return super.dispatchTouchEvent(event);      }       }



更多相关文章

  1. android学习记录
  2. Android(安卓)高仿知乎日报 (上)
  3. Android之Hello WebView
  4. android控件之spinner (下拉列表)
  5. android 设置控件 圆角
  6. android 状态选择器
  7. 超级简单的Google VR SDK播放VR视频
  8. onTouch事件传递机制
  9. Android(安卓)AlertDialog有EditText无法弹出输入法的解决

随机推荐

  1. Android APK瘦身之Android Studio Lint (
  2. android SQLite数据库2
  3. Android与H5互调
  4. android 中遥控器键值的添加和修改
  5. Android中运用Pull解析器读取XML文件
  6. Android RIL源码分析(2)
  7. Android 3D引擎之CatCake----编译hello_c
  8. Andriod 开发之微信分享接口
  9. Android中的Intent详解
  10. 【Android】Android背景选择器selector用