所以我在教程中找到的代码之后一直在我的导航抽屉。但是我的片段没有切换。笔记本电脑关闭并使用AIDE。谁有人告诉我什么错了?猜猜它是我的setonitemclicklistener。
{
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void addDrawerItems()
{
String[] osArray = { "Hello", "Watch Ads", "TapJoy" };
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
selectView(position);
}
});
}
private void selectView(int position)
{
Fragment fragment = null;
switch (position)
{
case 0:
fragment = new HelloFragment();
break;
case 1 :
fragment = new AdFragment();
break;
case 2:
fragment = new TapJoyFragment();
break;
}
if (fragment == null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
} }
private void setupDrawer()
{
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("How will you earn?");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:2)
你好伙计!
private void selectView(int position)
{
Fragment fragment = null;
switch (position)
{
case 0:
fragment = new HelloFragment();
break;
case 1 :
fragment = new AdFragment();
break;
case 2:
fragment = new TapJoyFragment();
break;
}
**if (fragment == null)**
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
} }
此“if”语句总是 false 如果在开关(位置)之前将片段包装起来,片段如何为null。这就是为什么你的片段交易永远不会成真。
答案 1 :(得分:1)
首先使用FragmentActivity而不是Activity ...
private void selectView(int position) { Fragment fragment = null;
switch (position)
{
case 0:
fragment = new HelloFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame,片段).commit();
break;
case 1 :
fragment = new AdFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame,fragment).commit();
break;
case 2:
fragment = new TapJoyFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame,fragment).commit();
break;
}
答案 2 :(得分:1)
请使用if(fragment!= null)而不是(fragment == null)。