我一直在关注这里的教程:这里的代码让我感到困惑:
/** Swaps fragments in the main content view */
private void selectItem(int position) {
// Create a new fragment and specify the planet to show based on position
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
// Highlight the selected item, update the title, and close the drawer
mDrawer.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawer);
现在我的应用程序运行并且看起来很棒,但是当您按下导航抽屉中的按钮时,什么也没有发生。我需要弄清楚如果用户按下一个按钮,它会如何换入一个新的FrameLayout
. 还有另一个问题:我的activity_main.xml
布局中有这个:
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:keepScreenOn="true"
>
</ListView>
</FrameLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
我不完全确定我是否应该拥有包含内容的列表视图(ListView
内部FrameLayout
)。我尝试制作自己的xml
布局,但是当我引用它来充气时,ListView
它不起作用。那么我是否为每个框架设置了一个单独的“xml”文件?如果是这样,我如何根据单击的按钮交换该片段?感谢您的时间和帮助!
编辑!!这是选择性方法
private void selectItem(int position) {
Log.i("MainActivity", "selectItem()" + position); //this gets called successfully
//not sure if this equals() thing is the best way to do this
if("Stuff".equals(getListView().getItemAtPosition(position))){
Log.i("MainActivity", "getItemAtPosition()-Stuff" + position); //this doesn't get called
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}else if("Stuff2".equals(getListView().getItemAtPosition(position))){
Log.i("MainActivity", "getItemAtPosition()-Stuff2" + position); //this also doesn't get called
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
Intent openHelpAvtivity = new Intent("com.schrodingerscat.hh.math.glossary.THEOREMSLIST");
startActivity(openHelpAvtivity);
}