gpt4 book ai didi

android - 如何在 Android actionbar 中使用 switch-case?

转载 作者:行者123 更新时间:2023-11-29 21:09:06 28 4
gpt4 key购买 nike

我正在使用 viewPager,我希望在操作栏上有一个按案例打开新 Activity 的项目,我的意思是,如果当前图像为 1,则打开 Activity1,如果它是第二个,则它应该打开 Activity2。到现在为止,我已经创建了包含项目的菜单,并打开了新的 Activity ,但我希望通过上面描述的案例打开更多 Activity 。非常感谢!!!

MainActivity.java:

package com.descoper.rom;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
private static int NUM_VIEWS = 46;

private MyPagerAdapter adapter;
private ViewPager pager;
private int[] pics = { R.drawable.casapoporului, R.drawable.transfagarasan,
R.drawable.transalpina, R.drawable.balealac,
R.drawable.barajulsiriu, R.drawable.bisericadesublac,
R.drawable.canionulscari, R.drawable.cascadacailor,
R.drawable.cascadaciucas, R.drawable.castelulbran,
R.drawable.castelulcorvinilor, R.drawable.castelulkaroly,
R.drawable.castelulpeles, R.drawable.castelulsturdza,
R.drawable.castrulroman, R.drawable.cazaneledunarii,
R.drawable.cazinoulconstanta, R.drawable.cetateabastionara,
R.drawable.cetateafagaras, R.drawable.cetateaprejmer,
R.drawable.cheilebicazului, R.drawable.cimitirulsapanta,
R.drawable.colibita, R.drawable.deltadunarii,
R.drawable.epavacostinesti, R.drawable.focurilevii,
R.drawable.hanulancutei, R.drawable.insulaovidiu,
R.drawable.laculalbastru, R.drawable.laculana,
R.drawable.laculbeului, R.drawable.laculbucura,
R.drawable.laculcapra, R.drawable.laculrosu, R.drawable.laculvidra,
R.drawable.laculvulturilor, R.drawable.mocanita,
R.drawable.parculcraiova, R.drawable.parcultimisoara,
R.drawable.pesteraghetarul, R.drawable.pesteraursilor,
R.drawable.piatabrasov, R.drawable.poduldumnezeu,
R.drawable.salinapraid, R.drawable.salinaturda,
R.drawable.sarmisegetuzaregia, R.drawable.sfinxulbucegi,
R.drawable.sibiu, R.drawable.vulcaniinoroiosi };

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

adapter = new MyPagerAdapter();
pager = (ViewPager) findViewById(R.id.myviewpager);
pager.setAdapter(adapter);

PagerTitleStrip pagerTitleStrip = (PagerTitleStrip) findViewById(R.id.titlestrip);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);

return super.onCreateOptionsMenu(menu);
}

private class MyPagerAdapter extends PagerAdapter {

String[] title = { "Casa poporului", "Transfagarasan", "Transalpina",
"Balea Lac", "Barajul Siriu", "Biserica de sub lac",
"Canionul Sapte scari", "Cascada Cailor", "Cascada Ciucas",
"Castelul Bran", "Castelul Corvinilor", "Castelul Karoly",
"Castelul Peles", "Castelul Sturdza", "Castelul Roman",
"Cazanele Dunarii", "Cazinoul din Constanta",
"Cetatea Bastioara", "Cetatea Fagaras",
"Biserica fortificata de la Prejmer", "Cheile Bicazului",
"Cimitirul vesel din Sapanta", "Lacul Colibita",
"Delta Dunarii", "Epava din Costinesti",
"Focurile Vii de la Luger", "Hanul Ancutei", "Insula Ovidiu",
"Lacul Albastru", "Lacul Sfanta Ana", "Lacul ochiul Beiului",
"Lacul Bucura", "Lacul Capra", "Lacul Rosu", "Lacul Vidra",
"Lacul Vulturilor", "Mocanita din Maramures",
"Parcul Nicolae Romanov din Craiove",
"Piata Operei din Timisoara", "Pestera Scarisoara",
"Pestera Ursilor", "Piata Sfatului din Brasov",
"Podul lui Dumnezeu", "Salina Praid", "Salina Turda",
"Cetatea Sarmizegetusa", "Sfinxul din Bucegi",
"Piata mare din Sibiu", "Vulcanii Noroiosi" };

@Override
public CharSequence getPageTitle(int position) {
return title[position];
}

@Override
public int getCount() {
return NUM_VIEWS;
}

/**
* Create the page for the given position. The adapter is responsible
* for adding the view to the container given here, although it only
* must ensure this is done by the time it returns from
* {@link #finishUpdate()}.
*
* @param container
* The containing View in which the page will be shown.
* @param position
* The page position to be instantiated.
* @return Returns an Object representing the new page. This does not
* need to be a View, but can be some other container of the
* page.
*/
@Override
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(MainActivity.this);
view.setImageResource(pics[position]);

view.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Toast.makeText(MainActivity.this, "Page" + pics,
Toast.LENGTH_LONG).show();
}
});

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

return view;
}

/**
* Remove a page for the given position. The adapter is responsible for
* removing the view from its container, although it only must ensure
* this is done by the time it returns from {@link #finishUpdate()}.
*
* @param container
* The containing View from which the page will be removed.
* @param position
* The page position to be removed.
* @param object
* The same object that was returned by
* {@link #instantiateItem(View, int)}.
*/
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((ImageView) view);
}

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

/**
* Called when the a change in the shown pages has been completed. At
* this point you must ensure that all of the pages have actually been
* added or removed from the container as appropriate.
*
* @param container
* The containing View which is displaying this adapter's
* page views.
*/
@Override
public void finishUpdate(View arg0) {
}

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

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

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.gallery:
startActivity(new Intent(MainActivity.this, Galerie.class));
break;
case R.id.ro:
startActivity(new Intent(MainActivity.this, Romania.class));
break;
}
return false;
}

}

最佳答案

第 1 步:声明一个变量“frgmnt_Indicator”,用于指示同一 Activity 中的当前页面第 2 步:每次 viewPager 加载新 fragment 时,将相应的值分配给“frgmnt_Indicator”

第 3 步:onOptionItemSelected 方法,检查“frgmnt_Indicator”中的值并相应地在 viewPager 中加载 fragment

另一种方法

在 onOptionItemSelected 方法中

pager.getCurrentItem() 会给你位置,通过位置你知道加载了哪个图像,通过应用开关盒你可以做你想做的事

关于android - 如何在 Android actionbar 中使用 switch-case?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23516403/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com