gpt4 book ai didi

android - OnItemClickListener 抽屉导航

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:16:08 25 4
gpt4 key购买 nike

我正在使用抽屉导航根据 Android 开发人员的示例制作一个应用程序。我制作了这些项目,但我不知道如何从每个登记的项目中打开新的 Activity 。

这是主要 Activity :

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;
}
}
}

还有一个 Drawer 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"/>

点击项目没有调用其他 Activity ,这是什么错误?

最佳答案

代替您的代码:

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:
}
}

关于android - OnItemClickListener 抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16721834/

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