0
我想在Android上做一個應用程序與ViewPager填充2片段作爲開始和NavigationDrawer有一個側面菜單,並能夠瀏覽所有片段之間。 但是OnClick事件完成後,爲NavigationDrawer重載的方法「OnClickListener」沒有響應。我已經嘗試更改抽屜項目和列表的XML參數,但未顯示任何更改。 如有一些有幾分鐘的時間來檢查,非常感謝點擊NavigationDrawer不會改變片段與ViewPager
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;
}
}