0

这是我使用NavigationDrawer. 我有一个非常简单的问题。如何在主页面中制作第一页NavigationDrawer?此外,我对格式不太熟悉,因为这是我第一次使用抽屉,所以如果更熟悉的人能告诉我我是否做得正确,我将不胜感激。现在每个页面只显示文本,但最终它会做更多。我的一个问题是如何做到这一点,以便单击抽屉中的页面可以使用RelativeLayout例如打开一个新页面。根据我的理解适配器仅用于视图,我会创建一个全新的活动并startActivity()在我的迭代中调用抽屉点击吗?如果是这样,那效率高吗?这意味着页面加载需要很长时间吗?我的主要活动是:

public class MainActivity extends Activity {
private String[] mPages;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;

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

    mPages = getResources().getStringArray(R.array.page_titles);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPages));
    mTitle = mDrawerTitle = getTitle();
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
        }
    };

    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
}

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

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

@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 (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

/** 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;
    if(position == 0){
        fragment = new OneFragment();

        // Insert the fragment by replacing any existing fragment
        android.app.FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.content_frame, fragment)
                       .commit();
    }
    else if(position == 1){
        fragment = new TwoFragment();

        // Insert the fragment by replacing any existing fragment
        android.app.FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.content_frame, fragment)
                       .commit();
    }
    else if(position == 2){
        fragment = new ThreeFragment();

        // Insert the fragment by replacing any existing fragment
        android.app.FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.content_frame, fragment)
                       .commit();
    }
    else if(position == 3){
        fragment = new FourFragment();

        // Insert the fragment by replacing any existing fragment
        android.app.FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.content_frame, fragment)
                       .commit();
    }

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPages[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

public static class OneFragment extends Fragment{

    public OneFragment(){

    }
    View rootView;
    TextView text;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup contatiner,
            Bundle savedInstanceState){
        rootView = inflater.inflate(R.layout.drawer_layout,
                contatiner, false);
        text = (TextView)rootView.findViewById(R.id.text_view1);
        text.setText("One");
        return rootView;
    }

}

public static class TwoFragment extends Fragment{

    public TwoFragment(){

    }
    View rootView;
    TextView text;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup contatiner,
            Bundle savedInstanceState){
        rootView = inflater.inflate(R.layout.drawer_layout,
                contatiner, false);
        text = (TextView)rootView.findViewById(R.id.text_view1);
        text.setText("Two");
        return rootView;
    }

}

public static class ThreeFragment extends Fragment{

    public ThreeFragment(){

    }
    View rootView;
    TextView text;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup contatiner,
            Bundle savedInstanceState){
        rootView = inflater.inflate(R.layout.drawer_layout,
                contatiner, false);
        text = (TextView)rootView.findViewById(R.id.text_view1);
        text.setText("Three");
        return rootView;
    }

}

public static class FourFragment extends Fragment{

    public FourFragment(){

    }
    View rootView;
    TextView text;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup contatiner,
            Bundle savedInstanceState){
        rootView = inflater.inflate(R.layout.drawer_layout,
                contatiner, false);
        text = (TextView)rootView.findViewById(R.id.text_view1);
        text.setText("Four");
        return rootView;
    }

}
}

对于这个冗长的问题,我深表歉意,但开发人员网站并没有提供太多帮助,我想确保我第一次正确地做到这一点,所以我不必回头太多

4

1 回答 1

0

无论您要显示什么都需要在 DrawerLayout 的子级中。在您的情况下,我认为您会进行片段事务,onCreate()以将您希望首先看到的任何片段放入内容区域。

if (savedInstanceState == null) {
    fragment = new OneFragment();

    // Insert the fragment by replacing any existing fragment
    getFragmentManager().beginTransaction()
            .replace(R.id.content_frame, fragment).commit();

}

您可以保存对这些片段的引用,这样您就不会在每次用户进行选择时创建它们的新实例。您可能还想确保新选择与当前选择不同,否则您将获得不需要的额外事务。

为了打开一个“新页面”,您想要使用startActivity()并显示另一个具有自己布局的活动。一般来说,除非你专门做一些有意义的工作(比如从数据库中加载一堆数据),否则不要关心加载一个 Activity 需要多长时间。

最后,Adapters 专门用于AdapterViews (like ListView) 并且是完全不同的事情。它们与特定的 UI 组件结合使用以生成子视图,以表示潜在的大型数据集,并且出于效率原因可以回收这些数据集。如果您想了解更多信息/清晰度,我建议您观看ListView 的世界。

于 2013-08-13T18:19:30.193 回答