我正在尝试在Android上创建一个应用程序,其中ViewPager填充了2个片段作为开始,而NavigationDrawer则具有侧面菜单并且能够在所有片段之间导航。 但是,当OnClick事件完成时,为NavigationDrawer重载的方法“OnClickListener”不响应。我已经尝试更改抽屉项目和列表的XML参数,但不会显示任何更改。 如果有人有几分钟的时间来检查,非常感谢
MainActivity.java
public class MainActivity extends FragmentActivity {
private String[] drawerItemsList;
private ListView myDrawer;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout drawerLayout;
private ViewPager pager;
private FragmentManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.viewPager);
manager = getSupportFragmentManager();
pager.setAdapter(new MyPagerAdapter(manager));
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerItemsList = getResources().getStringArray(R.array.items);
myDrawer = (ListView) findViewById(R.id.my_drawer);
myDrawer.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_item, drawerItemsList));
myDrawer.setOnItemClickListener(new MyDrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_launcher, R.string.open, R.string.close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle("Appli");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.menu);
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(mDrawerToggle);
}
private class MyDrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int pos, long id) {
displayView(pos);
}
private void displayView(int position) {
pager.setCurrentItem(position);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml中
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/my_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#333"
android:divider="#AAA"
android:dividerHeight="1dp"/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="10dp" />
</android.support.v4.widget.DrawerLayout>
最后一个文件
MyPagerAdapter.java
package com.example.test;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(android.support.v4.app.FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return ArtistFragment.newInstance();
case 1: return AlbumFragment.newInstance();
default: return ArtistFragment.newInstance();
}
}
@Override
public int getCount() {
return 3;
}
}
答案 0 :(得分:0)
请从开发者指南中的示例应用中查看导航抽屉菜单实现。 http://developer.android.com/training/implementing-navigation/index.html