Android開発者とナビゲーションドロワーの例からアプリを作成しています。アイテムを作成しましたが、参加している各アイテムから新しいアクティビティを開く方法がわかりません。
これはMainActivityです:
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mGalaxyTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mGalaxyTitles = getResources().getStringArray(R.array.Galaxys_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mGalaxyTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch(item.getItemId()) {
case R.id.action_websearch:
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 1:
Intent a = new Intent(MainActivity.this, Page1.class);
startActivity(a);
break;
case 2:
Intent b = new Intent(MainActivity.this, Page2.class);
startActivity(b);
break;
default:
}
}
private void selectItem(int position) {
Fragment fragment = new GalaxyFragment();
Bundle args = new Bundle();
args.putInt(GalaxyFragment.ARG_Galaxy_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mGalaxyTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public static class GalaxyFragment extends Fragment {
public static final String ARG_Galaxy_NUMBER = "Galaxy_number";
public GalaxyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_Galaxy, container, false);
int i = getArguments().getInt(ARG_Galaxy_NUMBER);
String Galaxy = getResources().getStringArray(R.array.Galaxys_array)[i];
int imageId = getResources().getIdentifier(Galaxy.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(Galaxy);
return rootView;
}
}
}
そしてドロワーList.xml
<TextView xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:id="@Android:id/text1"
Android:textIsSelectable="false"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:textAppearance="?android:attr/textAppearanceListItemSmall"
Android:gravity="center_vertical"
Android:paddingLeft="16dp"
Android:paddingRight="16dp"
Android:textColor="#fff"
Android:background="?android:attr/activatedBackgroundIndicator"
Android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
アイテムのクリックは他のアクティビティを呼び出しません、間違いは何ですか?
コードの代わりに:
private void selectItem(int position) {
Fragment fragment = new GalaxyFragment();
Bundle args = new Bundle();
args.putInt(GalaxyFragment.ARG_Galaxy_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mGalaxyTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
このコードを使用してください:
private void selectItem(int position) {
switch(position) {
case 1:
Intent a = new Intent(MainActivity.this, Activity1.class);
startActivity(a);
break;
case 2:
Intent b = new Intent(MainActivity.this, Activity2.class);
startActivity(b);
break;
default:
}
}
そしてこの部分を削除します:(必要ありません)
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 1:
Intent a = new Intent(MainActivity.this, Page1.class);
startActivity(a);
break;
case 2:
Intent b = new Intent(MainActivity.this, Page2.class);
startActivity(b);
break;
default:
}
}
アクティビティを使用する代わりに、フラグメントとフラグメントマネージャを使用してアクティビティ間を移行します
switch(position) {
case 1:
Intent a = new Intent(Book.this, SecondActivity.class);
startActivity(a);
break;
case 2:
Intent b = new Intent(Book.this, ThirdActivity.class);
startActivity(b);
break;
default:
}
マニフェストでは、次のコードを記述します。
<activity
Android:name=".firstactivity"
Android:parentActivityName=".MainActivity">
<meta-data
Android:name="Android.support.PARENT_ACTIVITY"
Android:value=".MainActivity"/>
</activity>
主な活動:
Intent i=new Intent(MainActivity.this,firstactivity.class);
startActivity(i);