我想在应用程序的主屏幕中初始显示一些文本视图和图像(当应用程序打开时),并且当用户单击项目表单导航抽屉时需要替换此内容。 这个应用程序应该支持Android 2.2,所以我也使用了动作栏sherlock。
以下是我使用的主要活动的布局。
<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" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView
android:id="@+id/drawer"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:divider="#666"
android:background="#333"
android:dividerHeight="1dp"
android:paddingLeft="15sp"
android:paddingRight="15sp"/>
</android.support.v4.widget.DrawerLayout>
基本上我需要在Frame布局中添加元素。
我正在使用以下代码替换内容并添加新片段。
public class MainActivity extends SherlockFragmentActivity {
private String[] drawerListViewItems;
private ListView drawerListView;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
drawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// just styling option add shadow the right edge of the drawer
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getSupportMenuInflater().inflate(R.menu.main, menu);
// return true;
// }
@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 onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
if(drawerLayout.isDrawerOpen(drawerListView)){
drawerLayout.closeDrawer(drawerListView);
}else{
drawerLayout.openDrawer(drawerListView);
}
}
return false;
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
//Toast.makeText(MainActivity.this, ((TextView)view).getText(), Toast.LENGTH_SHORT).show();
//drawerLayout.closeDrawer(drawerListView);
selectItem(position);
}
}
// private void selectFragment(int position){
//
//// Intent intent = new Intent(MainActivity.this,Second.class);
//// startActivity(intent);
//// drawerLayout.closeDrawer(drawerListView);
// Fragment newFragment = new Second();
// android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
// switch(position){
// case 0:
// sfa = new Second();
// break;
// }
// fm.beginTransaction()
// .replace(R.id.action_settings, sfa)
// .commit();
//
// drawerListView.setItemChecked(position, true);
// drawerLayout.closeDrawer(drawerListView);
// }
private void selectItem(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Locate Position
switch (position) {
case 1:
Fragment f = new Second();
ft.replace(R.id.content_frame, f);
Toast.makeText(getApplicationContext(), "Yahoooo!", Toast.LENGTH_SHORT).show();
break;
case 2:
Fragment f2 = new Third();
ft.replace(R.id.content_frame, f2);
Toast.makeText(getApplicationContext(), "Two", Toast.LENGTH_SHORT).show();
break;
}
ft.commit();
drawerListView.setItemChecked(position, true);
setTitle("Title");
drawerLayout.closeDrawer(drawerListView);
}
}
动作抽屉成功运作。我想在主要内容区域添加项目,并在用户从导航栏中选择项目时用新片段替换它们。怎么做?
提前谢谢。
答案 0 :(得分:2)
在onCreate
添加以下内容
if (savedInstanceState == null) {
selectItem(0);
}
最初显示片段f
,当用户选择列表中的项目时,用新的片段替换片段f
。