7

我正在使用带有导航抽屉的 Android 开发人员示例制作应用程序。我制作了这些物品,但我不知道如何从每个入伍的物品中打开新的活动。

这是主活动:

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"/>

单击项目不调用其他活动,这是什么错误?

4

6 回答 6

14

而不是您的代码:

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:
}
}
于 2013-06-13T06:50:11.387 回答
1

而不是使用活动,只需使用片段和片段管理器在它们之间进行转换

于 2013-05-29T08:41:42.073 回答
1

像这样使用

 switch (position) {

            case 0:
                fragment = new Free();

                break;
            case 1:
               fragment  = new Manage();

                break;
            case 2:
              fragment = new Paid();

                break;
            case 3:
                fragment = new Categories();

                break;
            case 4:
             logout();

                break;
            default:
                break;
        }
于 2020-01-31T12:51:36.267 回答
0

在清单中,您编写以下代码:

<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);
于 2016-01-07T07:30:56.943 回答
0
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:
}
于 2015-08-21T07:48:19.357 回答
0
 private class DrawerItemClickListener implements ListView.OnItemClickListener
 {
 @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
     {
         SelectItem(position);
     }
  }
于 2020-01-31T12:55:24.143 回答