我正在研究这个应用程序,它的MainActivity中有一个viewPager和一些可滑动的片段。今天我实现了导航抽屉,但我不知道如何控制抽屉的DrawerItemClickListener类显示哪个片段。有没有解决方案,或者我只是朝着错误的方向前进并且必须改变导航抽屉的实现方式?
教程建议使用对我不起作用的getFragmentManager方法。
感谢。
这是DrawerItemClickListener类:
package com.freestylers.druskischool;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.AdapterView;
public class DrawerItemClickListener implements ListView.OnItemClickListener
{
public int FragmentNumber;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long
id) {
selectItem(position);
}
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new StartFragment();
FragmentNumber=0;
break;
case 1:
fragment = new MesFragment();
FragmentNumber=1;
break;
case 2:
fragment = new NaujienosFragment();
FragmentNumber=2;
break;
case 3:
fragment = new InstruktoriaiFragment();
FragmentNumber=3;
break;
case 4:
fragment = new MetodikaFragment();
FragmentNumber=4;
break;
case 5:
fragment = new GalerijaFragment();
FragmentNumber=5;
break;
case 6:
fragment = new InstruktoriaiFragment();
FragmentNumber=6;
break;
case 7:
fragment = new AtsiliepimaiFragment();
FragmentNumber=7;
break;
case 8:
fragment = new DrufunparkFragment();
FragmentNumber=8;
break;
case 9:
FragmentNumber=9;
fragment = new KontaktaiFragment();
break;
default:
break;
}
if (fragment != null) {
//tutorial suggests this code:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
getActionBar().setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
}
这是我的MainActivity.java:
package com.freestylers.druskischool;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
public class MainActivity extends ActionBarActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
ViewPager pager;
FragmentStatePagerAdapter adapter2;
/* Just some random URLs
*
* Each page of our pager will display one URL from this array
* Swiping, to the right will take you to the next page having
* the next URL.
*/
String[] fragments={
"start",
"mes",
"naujienos",
"instruktoriai",
"metodika",
"galerija",
"kainos",
"atsiliepimai",
"drufunpark",
"kontaktai"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[10];
drawerItem[0] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Pradžia");
drawerItem[1] = new ObjectDrawerItem(R.drawable.ic_action_copy, "Mes");
drawerItem[2] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Naujienos");
drawerItem[3] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Instruktoriai");
drawerItem[4] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Metodika");
drawerItem[5] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Galerija");
drawerItem[6] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Kainos");
drawerItem[7] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Atsiliepimai");
drawerItem[8] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Drufunpark");
drawerItem[9] = new ObjectDrawerItem(R.drawable.ic_action_copy,
"Kontaktai");
mNavigationDrawerItemTitles=
getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this,
R.layout.listview_item_row, drawerItem);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
pager=(ViewPager)findViewById(R.id.my_pager);
adapter2=new FragmentStatePagerAdapter(
getSupportFragmentManager()
){
@Override
public int getCount() {
// This makes sure getItem doesn't use a position
// that is out of bounds of our array of fragments
return fragments.length;
}
@Override
public Fragment getItem(int position) {
// Here is where all the magic of the adapter happens
// As you can see, this is really simple.
//(fragments[position]);
if(position==0){
return StartFragment.newInstance();
}
else if(position==1){
return MesFragment.newInstance();
}
else if(position==2){
return NaujienosFragment.newInstance();
}
else if(position==3){
return InstruktoriaiFragment.newInstance();
}
else if(position==4){
return MetodikaFragment.newInstance();
}
else if(position==5){
return GalerijaFragment.newInstance();
}
else if(position==6){
return KainosFragment.newInstance();
}
else if(position==7){
return AtsiliepimaiFragment.newInstance();
}
else if(position==8){
return DrufunparkFragment.newInstance();
}
else if(position==9){
return KontaktaiFragment.newInstance();
}
else{return KontaktaiFragment.newInstance();}
}
};
//Let the pager know which adapter it is supposed to use
pager.setAdapter(adapter2);
}
@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 super.onCreateOptionsMenu(menu);
}
/*@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
itu f (id == R.id.action_start) {
return true;
}
return super.onOptionsItemSelected(item);
}*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle other action bar items...
/* switch (item.getItemId()) {
case R.id.action_settings:
return true;
}*/
return super.onOptionsItemSelected(item);
}
public static void ChooseFragment(Fragment fragment) {
}
}
答案 0 :(得分:0)
我想出了我的错误,它必须在MainActivity.java中,DrawerItemClickListener是一个内部类。 仍然没有找到一种在navDrawer中可滑动片段之间导航的方法,可能是不可能的。