导航抽屉没有显示主要片段

时间:2014-05-10 18:41:51

标签: android fragment navigation-drawer

我刚从developer.android.com复制了Navigation Drawer示例。抽屉有效,我可以用它来改变活动,但问题是我无法看到活动的片段。我的问题是什么? 这是我的代码:

Activity.java:

public class Guide extends ActionBarActivity {

private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;

boolean isPlayersOpen = false;
boolean isMatchOpen = false;
boolean isTournamentOpen = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_guide);

    // get list items from strings.xml
    drawerListViewItems = getResources().getStringArray(R.array.activity_list);
    // get ListView defined in activity_main.xml
    drawerListView = (ListView) findViewById(R.id.left_drawer);

    // Set the adapter for the list view
    drawerListView.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_listview_item, drawerListViewItems));

    // 2. App Icon 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // 2.1 create ActionBarDrawerToggle
    actionBarDrawerToggle = 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 */
            );

    // 2.2 Set actionBarDrawerToggle as the DrawerListener
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    // 2.3 enable and show "up" arrow
    getActionBar().setDisplayHomeAsUpEnabled(true); 

    // just styling option
    drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

    drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    actionBarDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    actionBarDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.guide, menu);
    return true;
}

public void extendPlayer(View view){

    ImageView extendPlayer = (ImageView)findViewById(R.id.players1);
    ImageView collapsePlayer = (ImageView)findViewById(R.id.players2);
    TextView players_text = (TextView)findViewById(R.id.players_guide_text);

    if(isPlayersOpen){

        extendPlayer.setVisibility(View.VISIBLE);
        collapsePlayer.setVisibility(View.GONE);
        players_text.setVisibility(View.GONE);
        isPlayersOpen = false;

    } else {

        extendPlayer.setVisibility(View.GONE);
        collapsePlayer.setVisibility(View.VISIBLE);
        players_text.setVisibility(View.VISIBLE);
        isPlayersOpen = true;

    }

}

public void extendMatch(View view){

    ImageView extendMatch = (ImageView)findViewById(R.id.match1);
    ImageView collapseMatch = (ImageView)findViewById(R.id.match2);
    TextView match_text = (TextView)findViewById(R.id.match_guide_text);

    if(isMatchOpen){

        extendMatch.setVisibility(View.VISIBLE);
        collapseMatch.setVisibility(View.GONE);
        match_text.setVisibility(View.GONE);
        isMatchOpen = false;

    } else {

        extendMatch.setVisibility(View.GONE);
        collapseMatch.setVisibility(View.VISIBLE);
        match_text.setVisibility(View.VISIBLE);
        isMatchOpen = true;

    }

}

public void extendTournament(View view){

    ImageView extendTournament = (ImageView)findViewById(R.id.tournament1);
    ImageView collapseTournament = (ImageView)findViewById(R.id.tournament2);
    TextView tournament_text = (TextView)findViewById(R.id.tournament_guide_text);

    if(isTournamentOpen){

        extendTournament.setVisibility(View.VISIBLE);
        collapseTournament.setVisibility(View.GONE);
        tournament_text.setVisibility(View.GONE);
        isTournamentOpen = false;

    } else {

        extendTournament.setVisibility(View.GONE);
        collapseTournament.setVisibility(View.VISIBLE);
        tournament_text.setVisibility(View.VISIBLE);
        isTournamentOpen = true;

    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        drawerLayout.closeDrawer(drawerListView);

        if(position == 0){

            Intent intent = new Intent(Guide.this, Players.class);
            startActivity(intent);

        }

        if(position == 1){

            Toast.makeText(Guide.this, ((TextView)view).getText(), Toast.LENGTH_SHORT).show();

        }

        if(position == 2){

            Intent intent = new Intent(Guide.this, Tournaments.class);
            startActivity(intent);

        }

        if(position == 3){



        }

        if(position == 4){

            Intent intent = new Intent(Guide.this, Info.class);
            startActivity(intent);

        }

    }
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_guide,
                container, false);
        return rootView;
    }
}

}

ExtendPlayer,ExtendMatch和ExtendTournament是我个人的方法。

Activity.xml:

<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/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="#ddd"/>

我的片段名为fragment_guide.xml

感谢。

1 个答案:

答案 0 :(得分:0)

首先,如果您没有开始选择NavigationDrawer List项目的新活动,则更为可取。您应该做的只是用标识FrameLayout替换contentframe中的现有片段。这样做的好处是,您可以更轻松地将应用程序扩展到平板电脑。

关于您未实例化片段的问题,您的 Activity.xml 中的FrameLayout中没有任何内容。您可能希望在FrameLayout内实例化您的片段。在从NavigationDrawer列表中选择选项后,只需将FrameLayout中的片段替换为相应的片段。

使用FragmentTransaction来实现这一目标。