Android开发学习:切换菜单-点击ImageView菜单进行切换,使用控件ViewPager - Android开发 - 郑德才博客
11
2014
06

Android开发学习:切换菜单-点击ImageView菜单进行切换,使用控件ViewPager

Android开发学习

切换菜单,第二种:点击进行ImageView切换,使用控件ViewPager

新建xml布局文件:activity_two.xml,使用ViewPager来显示内容,代码内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/vPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:background="#ffffff"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />

    <include layout="@layout/bottom" />

</LinearLayout>

新建xml布局文件:bottom.xml,用来显示底部的菜单,代码内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient_bg"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/toolbar_ly1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/toolbar_img1"
                android:layout_width="wrap_content"
                android:layout_height="30dip"
                android:layout_marginTop="3dip"
                android:src="@drawable/selector_toolbar_1" />

            <TextView
                android:id="@+id/toolbar_txt1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lable_1_top_left_text"
                android:textColor="@color/notefontcolor"
                android:textSize="12dip" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/toolbar_ly2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center|center_vertical"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/toolbar_img2"
                android:layout_width="wrap_content"
                android:layout_height="30dip"
                android:layout_marginTop="3dip"
                android:src="@drawable/selector_toolbar_2" />

            <TextView
                android:id="@+id/toolbar_txt2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lable_2_top_left_text"
                android:textColor="@color/notefontcolor"
                android:textSize="12dip" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/toolbar_ly3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/toolbar_img3"
                android:layout_width="wrap_content"
                android:layout_height="30dip"
                android:layout_marginTop="3dip"
                android:src="@drawable/selector_toolbar_3" />

            <TextView
                android:id="@+id/toolbar_txt3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lable_3_top_left_text"
                android:textColor="@color/notefontcolor"
                android:textSize="12dip" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/toolbar_ly4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/toolbar_img4"
                android:layout_width="wrap_content"
                android:layout_height="30dip"
                android:layout_marginTop="3dip"
                android:src="@drawable/selector_toolbar_4" />

            <TextView
                android:id="@+id/toolbar_txt4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lable_4_top_left_text"
                android:textColor="@color/notefontcolor"
                android:textSize="12dip" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/toolbar_ly5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/toolbar_img5"
                android:layout_width="wrap_content"
                android:layout_height="30dip"
                android:layout_marginTop="3dip"
                android:src="@drawable/selector_toolbar_5" />

            <TextView
                android:id="@+id/toolbar_txt5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lable_5_top_left_text"
                android:textColor="@color/notefontcolor"
                android:textSize="12dip" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

使用线性布局LinearLayout,资源文件中的四个按钮使用布局图片文件selector_toolbar_1.xml、selector_toolbar_2.xml、selector_toolbar_3.xml、selector_toolbar_4.xml、selector_toolbar_5.xml、bottom.xml、activitty_two.xml,在此就不一一列出

五个按钮点击后对应五个布局及页面:Tab1Activity.java、Tab2Activity.java、Tab3Activity.java、Tab4Activity.java、Tab5Activity.java、TwoActivity.java;layout_1.xml、layout_2.xml、layout_3.xml、layout_4.xml、layout_5.xml,在此也不一一列出

实现TwoActivity.java代码:

package com.zhengdecai.switchmenu;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.TabActivity.Tab1Activity;
import com.TabActivity.Tab2Activity;
import com.TabActivity.Tab3Activity;
import com.TabActivity.Tab4Activity;
import com.TabActivity.Tab5Activity;

/**
 * 切换菜单:使用ViewPager,包括:Tab1Activity.java、Tab2Activity.java、Tab3Activity.java、Tab4Activity.java、Tab5Activity.java、TwoActivity.java
 * xml文件包括:selector_toolbar_1.xml、selector_toolbar_2.xml、selector_toolbar_3.xml、selector_toolbar_4.xml、selector_toolbar_5.xml、bottom.xml、activitty_two.xml、layout_1.xml、layout_2.xml、layout_3.xml、layout_4.xml、layout_5.xml
 *
 * @author 郑德才
 *
 */
public class TwoActivity extends Activity {
 private ViewPager mPager;// 页卡内容
 private List<View> listViews; // Tab页面列表

 private ImageView Img1, Img2, Img3, Img4, Img5;
 private TextView Txt1, Txt2, Txt3, Txt4, Txt5;
 private LinearLayout Ly1, Ly2, Ly3, Ly4, Ly5;

 Context context = null;
 LocalActivityManager manager = null;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_two);

  context = TwoActivity.this;
  manager = new LocalActivityManager(this, true);
  manager.dispatchCreate(savedInstanceState);

  InitBottomMenu();

  InitViewPager();
 }

 /**
  * 初始化ViewPager
  */
 private void InitViewPager() {
  mPager = (ViewPager) findViewById(R.id.vPager);
  listViews = new ArrayList<View>();
  LayoutInflater mInflater = getLayoutInflater();

  /*
   * listViews.add(mInflater.inflate(R.layout.layout_1, null));
   * listViews.add(mInflater.inflate(R.layout.layout_2, null));
   * listViews.add(mInflater.inflate(R.layout.layout_3, null));
   * listViews.add(mInflater.inflate(R.layout.layout_4, null));
   * listViews.add(mInflater.inflate(R.layout.layout_5, null));
   */

  /* 使用这种方法,直接添加Activity */
  Intent intent = new Intent(context, Tab1Activity.class);
  listViews.add(getView("A", intent));
  intent = new Intent(context, Tab2Activity.class);
  listViews.add(getView("B", intent));
  intent = new Intent(context, Tab3Activity.class);
  listViews.add(getView("C", intent));
  intent = new Intent(context, Tab4Activity.class);
  listViews.add(getView("D", intent));
  intent = new Intent(context, Tab5Activity.class);
  listViews.add(getView("E", intent));

  mPager.setAdapter(new MyPagerAdapter(listViews));
  mPager.setOnPageChangeListener(new MyOnPageChangeListener());
  mPager.setCurrentItem(0);

  Img1.setImageDrawable(getResources().getDrawable(R.drawable.current_1));
  Txt1.setTextColor(TwoActivity.this.getResources().getColor(
    R.color.white));
 }

 /**
  * 通过activity获取视图
  *
  * @param id
  * @param intent
  * @return
  */
 private View getView(String id, Intent intent) {
  return manager.startActivity(id, intent).getDecorView();
 }

 /**
  * ViewPager适配器
  */
 public class MyPagerAdapter extends PagerAdapter {
  public List<View> mListViews;

  public MyPagerAdapter(List<View> mListViews) {
   this.mListViews = mListViews;
  }

  @Override
  public void destroyItem(View arg0, int arg1, Object arg2) {
   ((ViewPager) arg0).removeView(mListViews.get(arg1));
  }

  @Override
  public void finishUpdate(View arg0) {
  }

  @Override
  public int getCount() {
   return mListViews.size();
  }

  @Override
  public Object instantiateItem(View arg0, int arg1) {

   switch (arg1) {
   case 0:
    // 读取Tab1Activity中的按钮添加事件,集合listViews.add(mInflater.inflate(R.layout.layout_1,
    // null));,查找里面的控件:Button
    // BtnLxs=(Button)mListViews.get(arg1).findViewById(R.id.layout_bottom);
    break;
   case 1:
    break;
   case 2:
    break;
   case 3:
    break;
   case 4:
    break;
   default:
    break;
   }
   ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
   return mListViews.get(arg1);
  }

  @Override
  public boolean isViewFromObject(View arg0, Object arg1) {
   return arg0 == (arg1);
  }

  @Override
  public void restoreState(Parcelable arg0, ClassLoader arg1) {
  }

  @Override
  public Parcelable saveState() {
   return null;
  }

  @Override
  public void startUpdate(View arg0) {
  }
 }

 /**
  * 页卡切换监听
  */
 private class MyOnPageChangeListener implements OnPageChangeListener {
  @Override
  public void onPageSelected(int arg0) {
   Animation animation = null;
   switch (arg0) {
   case 0:
    InitBottomMenu();
    Img1.setImageDrawable(getResources().getDrawable(
      R.drawable.current_1));
    Txt1.setTextColor(TwoActivity.this.getResources().getColor(
      R.color.white));
    break;
   case 1:
    InitBottomMenu();
    Img2.setImageDrawable(getResources().getDrawable(
      R.drawable.current_2));
    Txt2.setTextColor(TwoActivity.this.getResources().getColor(
      R.color.white));
    break;
   case 2:
    InitBottomMenu();
    Img3.setImageDrawable(getResources().getDrawable(
      R.drawable.current_3));
    Txt3.setTextColor(TwoActivity.this.getResources().getColor(
      R.color.white));
    break;
   case 3:
    InitBottomMenu();
    Img4.setImageDrawable(getResources().getDrawable(
      R.drawable.current_4));
    Txt4.setTextColor(TwoActivity.this.getResources().getColor(
      R.color.white));
    break;
   case 4:
    InitBottomMenu();
    Img5.setImageDrawable(getResources().getDrawable(
      R.drawable.current_5));
    Txt5.setTextColor(TwoActivity.this.getResources().getColor(
      R.color.white));
    break;
   }
  }

  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) {
  }

  @Override
  public void onPageScrollStateChanged(int arg0) {
  }
 }

 public void InitBottomMenu() {
  Ly1 = (LinearLayout) findViewById(R.id.toolbar_ly1);
  Ly2 = (LinearLayout) findViewById(R.id.toolbar_ly2);
  Ly3 = (LinearLayout) findViewById(R.id.toolbar_ly3);
  Ly4 = (LinearLayout) findViewById(R.id.toolbar_ly4);
  Ly5 = (LinearLayout) findViewById(R.id.toolbar_ly5);

  Img1 = (ImageView) findViewById(R.id.toolbar_img1);
  Img2 = (ImageView) findViewById(R.id.toolbar_img2);
  Img3 = (ImageView) findViewById(R.id.toolbar_img3);
  Img4 = (ImageView) findViewById(R.id.toolbar_img4);
  Img5 = (ImageView) findViewById(R.id.toolbar_img5);

  Txt1 = (TextView) findViewById(R.id.toolbar_txt1);
  Txt2 = (TextView) findViewById(R.id.toolbar_txt2);
  Txt3 = (TextView) findViewById(R.id.toolbar_txt3);
  Txt4 = (TextView) findViewById(R.id.toolbar_txt4);
  Txt5 = (TextView) findViewById(R.id.toolbar_txt5);

  Ly1.setOnClickListener(new LinearLayout.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    mPager.setCurrentItem(0);
   }
  });

  Ly2.setOnClickListener(new LinearLayout.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    mPager.setCurrentItem(1);
   }
  });

  Ly3.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    mPager.setCurrentItem(2);
   }
  });

  Ly4.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    mPager.setCurrentItem(3);
   }
  });

  Ly5.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    mPager.setCurrentItem(4);
   }
  });

  Img1.setImageDrawable(getResources().getDrawable(R.drawable.back_1));
  Img2.setImageDrawable(getResources().getDrawable(R.drawable.back_2));
  Img3.setImageDrawable(getResources().getDrawable(R.drawable.back_3));
  Img4.setImageDrawable(getResources().getDrawable(R.drawable.back_4));
  Img5.setImageDrawable(getResources().getDrawable(R.drawable.back_5));

  Txt1.setTextColor(this.getResources().getColor(R.color.notefontcolor));
  Txt2.setTextColor(this.getResources().getColor(R.color.notefontcolor));
  Txt3.setTextColor(this.getResources().getColor(R.color.notefontcolor));
  Txt4.setTextColor(this.getResources().getColor(R.color.notefontcolor));
  Txt5.setTextColor(this.getResources().getColor(R.color.notefontcolor));
 }
}

实现效果:

« 上一篇下一篇 »

评论列表:

1.旗袍女装  2014/6/16 16:53:50 回复该留言
实现效果:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。