0

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

4

2 回答 2

1

您需要使用以下方法Activity

覆盖这个 onPrepareOptionsMenu(Menu menu)

@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 中的菜单按钮时设置要显示的菜单。因为您已经使用了 ViewPagerIndicator,您知道要显示哪个页面,并根据该页面设置您的switch条件或if and else if

于 2012-04-05T06:30:45.617 回答
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>

主要的.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 并像我一样添加您的更改。祝你好运 :)

于 2012-04-05T06:15:49.947 回答