0

我一直在关注这里的教程:这里的代码让我感到困惑:

    /** 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); 

    }
4

2 回答 2

0

我修好了它!耶!感谢@bee 的启发!我所要做的,而不是使用 getListView(),而是这样做了:if("Stuff".equals(mDrawerList.getItemAtPosition(position))){}这终于奏效了!

于 2013-06-15T04:45:26.697 回答
0

您是否尝试过使用调试器单步执行它?selectItem() 是否被调用?如果没有,请确保正确设置 OnClickListener。

您不需要为每个帧使用单独的 XML 文件。(你所说的帧是什么意思?片段?我假设是这样。)重点是您可以创建一个新片段并使用 setArguments() 来设置它显示的内容。列表视图应使用以下代码填充其数据,如下所示:

    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPlanetTitles));

尝试下载完整的示例代码(http://developer.android.com/shareables/training/NavigationDrawer.zip),看看你在做什么不同。我认为这个例子是用来下载的,而不是重建的:教程页面并没有完全包含你需要的所有代码。

于 2013-06-14T02:20:52.073 回答