ViewPagerIndicator为每个页面提供不同的菜单?

时间:2012-04-05 06:03:32

标签: android android-viewpager android-menu

我正在使用ViewPagerIndicator在单个活动中显示不同的页面。是否可以通过按每个不同页面上的菜单按钮显示不同的菜单?例如,第1页会显示“添加”和“删除”菜单项,而第2页会显示“编辑”和“设置”吗?这些是愚蠢的例子,但是当活动被实例化时,菜单似乎被创建,所以当我点击MENU按钮时,菜单已经被创建,所以无论当前显示哪个页面,我都会得到相同的菜单项。在ViewPagerIndicator中更改页面时是否可以触发onCreateOptionsMenu()?

2 个答案:

答案 0 :(得分:1)

您需要在Activity

中使用以下方法

覆盖此onPrepareOptionsMenu(菜单菜单)

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
     //In this you can Change Menu from different xml  
   // MenuItem item = menu.findItem(R.id.refresh);
   // if (item != null) {
    // item.setVisible (shouldIShowThisItem)
   // }
//May be you can set Visibility to set for Item to show or Hide

      menu.clear();
    if(menu_one){
         MenuInflater inflater = getMenuInflater();
         inflater.inflate(R.menu.game_menu, menu);
    }else{
        MenuInflater inflater = getMenuInflater();
         inflater.inflate(R.menu.my_menu, menu);
    }

    return super.onPrepareOptionsMenu(menu);

}

在此方法中,您可以设置当用户在android.as中按菜单按钮时要显示的菜单您已使用ViewPagerIndicator,您知道显示哪个页面,并根据您可以使switch条件或if and else if

答案 1 :(得分:0)

我做了一个类似的例子。对我来说,我需要一个页面指示器(表示页面的数量),例如,如果我有4页,我将获得菜单中的圆形图像。当我在第3页时,第三页将是黄色。您可以通过添加必要的按钮来执行相同操作。

这是一个简单的例子: //首先是主要活动

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class AwesomePagerActivity extends Activity {

    private ViewPager awesomePager;
    private static int NUM_AWESOME_VIEWS = 4;
    private Context cxt;
    private Pager awesomeAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        cxt = this;

        awesomeAdapter = new Pager(NUM_AWESOME_VIEWS);
        awesomeAdapter.setContext(cxt);
        awesomeAdapter.setListeImages(liste_images);
        awesomePager = (ViewPager) findViewById(R.id.awesomepager);
        awesomePager.setAdapter(awesomeAdapter);
    }

    private Integer[] liste_images = {
            R.drawable.tuto_step01, R.drawable.tuto_step02,
            R.drawable.tuto_step03, R.drawable.tuto_step04};


}

比寻呼机适配器:

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Pager extends PagerAdapter{

    private static int NUM_AWESOME_VIEWS;
    private Context cxt;
    Integer[] liste_images;

    public Pager(int i){
        NUM_AWESOME_VIEWS = i;
    }
    public void setContext(Context cxt){
        this.cxt=cxt;

    }

    public void setListeImages(Integer[] liste_images){
        this.liste_images=liste_images;
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((View) view);
    }

    @Override
    public void finishUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return NUM_AWESOME_VIEWS;
    }

    @Override
    public Object instantiateItem(View collection, int position) {

        LayoutInflater  inflater = (LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.layout_pager_ind, null);

        ImageView tv = (ImageView) layout.findViewById(R.id.pager);

        tv.setImageResource(liste_images[position]);

        LinearLayout mPagesController = (LinearLayout) layout.findViewById(R.id.pages_controller);

        int count = getCount();

        for (int i = 0; i < count; i++)
        {
            if (i == position)
            {
                ((ImageView) mPagesController.getChildAt(i)).setImageResource(R.drawable.btn_vflipper_pressed);
            }
            else
            {
                ((ImageView) mPagesController.getChildAt(i)).setImageResource(R.drawable.btn_vflipper_normal);
            }
        }



        ((ViewPager) collection).addView(layout,0);

        return layout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==((View)object);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

}

layout_pager_indicator.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="fill_parent"
    android:orientation="vertical"
    android:gravity="center" >

<ImageView
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/pager"/>

 <LinearLayout
        android:id="@+id/pages_controller"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/btn_vflipper_pressed">
        </ImageView>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/btn_vflipper_normal">
        </ImageView>   
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/btn_vflipper_normal">
        </ImageView>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/btn_vflipper_normal">
        </ImageView>    
    </LinearLayout> 


</LinearLayout>

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/awesomepager"/>


</LinearLayout>

您可以下载项目AwesomePagerIndicator添加您所做的更改。 祝你好运:)