如何将片段交换进我的应用程序?

时间:2013-06-13 23:56:24

标签: android android-fragments notifications android-framelayout

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

    /** 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'文件吗?如果是这样,我如何根据点击的按钮交换该片段?感谢您的时间和帮助!

EDIT !! 这是遴选方法

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

    }

2 个答案:

答案 0 :(得分: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)并查看您的操作方式有何不同。我认为这个示例是为了下载而不是重构:教程页面并不完全包含您需要的所有代码。

答案 1 :(得分:0)

我修好了!好极了!感谢@bee的启蒙!所有我必须做的,而不是使用getListView(),我做了这样做:if("Stuff".equals(mDrawerList.getItemAtPosition(position))){},这最终工作!