我尝试使用DrawerLayout实现基本活动,以便将其扩展到其他活动。
我在Eclipse向导中使用了默认的Drawer应用程序以及这些问题的帮助:Q1,Q2,出于某种原因,我在启动应用程序时遇到了膨胀异常。
public class BaseActivity extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in
* {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
private DrawerLayout mDrawerLayout;
private FrameLayout mFrameLayout;
@Override
public void setContentView(int layoutResID) {
mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(
R.layout.activity_base, null);
mFrameLayout = (FrameLayout) mDrawerLayout.findViewById(R.id.container);
getLayoutInflater().inflate(layoutResID, mFrameLayout, true); /* Exception Here */
super.setContentView(mDrawerLayout);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, mDrawerLayout);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
}
@Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (position) {
case 0:
fragmentManager.beginTransaction()
.replace(R.id.container, new AllCategoriesFragment()).commit();
break;
case 1:
fragmentManager.beginTransaction()
.replace(R.id.container, new AllRecipesFragment()).commit();
break;
}
}
activity_base.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yonivy.cookpad.app.BaseActivity" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/navigation_drawer"
android:name="com.yonivy.cookpad.app.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
AllCategoriesFragment.java
public class AllCategoriesFragment extends Fragment {
private ArrayList<Category> allCategories;
private CategoriesAdapter cAdapter;
private ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_all_categories, container, false);
lv = (ListView) view.findViewById(R.id.mList);
displayAllCategories();
return view;
}
示例扩展类
public class RecipeActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe);
// ...
}