如何实现导航抽屉片段大师细节抽屉、如何实现、片段、细节

2023-09-06 13:22:32 作者:掐指一算丶此乃大胸之罩

我已经得到本网站的样本抽屉式导航: 的http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

和大师细节在这里: 的http://wptrafficanalyzer.in/blog/itemclick-handler-for-listfragment-in-android/

错误LogCat中oncreateview(inflac ....)视图 不能创建

我,我有尝试,

  //主activiry的活动:

    包in.wptrafficanalyzer.listfragmentitemclick;

进口in.wptrafficanalyzer.listfragmentitemclick.adapter.NavDrawerListAdapter;
进口in.wptrafficanalyzer.listfragmentitemclick.model.NavDrawerItem;

进口的java.util.ArrayList;

进口in.wptrafficanalyzer.listfragmentitemclick.R;

进口android.app.Activity;
进口android.app.Fragment;
进口android.app.FragmentManager;
进口android.app.FragmentTransaction;
进口android.app.ListFragment;
进口android.content.Intent;
进口android.content.res.Configuration;
进口android.content.res.TypedArray;
进口android.os.Bundle;
进口android.support.v4.app.ActionBarDrawerToggle;
进口android.support.v4.widget.DrawerLayout;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.ListView;

公共类MainActivity扩展活动实现CountryListFragment.ListFragmentItemClickListener {



    私人DrawerLayout mDrawerLayout;
    私人的ListView mDrawerList;
    私人ActionBarDrawerToggle mDrawerToggle;

    //导航抽屉称号
    私人的CharSequence mDrawerTitle;

    //用于存储应用程序标题
    私人的CharSequence mTitle;

    //滑动菜单项
    私有String [] navMenuTitles;
    私人TypedArray navMenuIcons;

    私人的ArrayList< NavDrawerItem> navDrawerItems;
    私人NavDrawerListAdapter适配器;

    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        mTitle = mDrawerTitle =的getTitle();

        //负荷滑动菜单项
        navMenuTitles = getResources()getStringArray(R.array.nav_drawer_items)。

        //导航抽屉,从资源的图标
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
        mDrawerList =(ListView控件)findViewById(R.id.list_slidermenu);

        navDrawerItems =新的ArrayList< NavDrawerItem>();

        //添加导航抽屉里的物品数组
        // 家
        navDrawerItems.add(新NavDrawerItem(navMenuTitles [0],navMenuIcons.getResourceId(0,-1)));
        //查找人​​员
        navDrawerItems.add(新NavDrawerItem(navMenuTitles [1],navMenuIcons.getResourceId(1,-1)));
        //照片
        navDrawerItems.add(新NavDrawerItem(navMenuTitles [2],navMenuIcons.getResourceId(2,-1)));
        //社区,这里将添加计数器
        navDrawerItems.add(新NavDrawerItem(navMenuTitles [3],navMenuIcons.getResourceId(3,-1),真,22));
        //网页
        navDrawerItems.add(新NavDrawerItem(navMenuTitles [4],navMenuIcons.getResourceId(4,-1)));
        //热点,我们将在这里添加计数器
        navDrawerItems.add(新NavDrawerItem(navMenuTitles [5],navMenuIcons.getResourceId(5,-1),真正的50+));


        //回收类型数组
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(新SlideMenuClickListener());

        //设置导航抽屉名单适配器
        适配器=新NavDrawerListAdapter(getApplicationContext()
                navDrawerItems);
        mDrawerList.setAdapter(适配器);

        //使操作栏中的应用程序图标,它表现为切换按钮
        getActionBar()setDisplayHomeAsUpEnabled(真)。
        getActionBar()setHomeButtonEnabled(真)。

        mDrawerToggle =新ActionBarDrawerToggle(这一点,mDrawerLayout,
                R.drawable.ic_drawer,//导航菜单切换图标
                R.string.app_name,//导航抽屉打开 - 说明访问
                R.string.app_name //导航抽屉关闭 - 说明访问
        ){
            公共无效onDrawerClosed(查看视图){
                。getActionBar()的setTitle(mTitle);
                //调用prepareOptionsMenu()来显示操作栏图标
                invalidateOptionsMenu();
            }

            公共无效onDrawerOpened(查看drawerView){
                。getActionBar()的setTitle(mDrawerTitle);
                //调用prepareOptionsMenu()隐藏操作栏图标
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        如果(savedInstanceState == NULL){
            //关于第一导航项目首次显示视图
            displayView(0);
        }
    }

    / **
     *滑动菜单项,点击监听器
     * * /
    私有类SlideMenuClickListener工具
            ListView.OnItemClickListener {
        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
                长ID){
            对选定的导航抽屉的项目//显示视图
            displayView(位置);
        }
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //切换导航抽屉选择操作栏中的应用程序图标/标题
        如果(mDrawerToggle.onOptionsItemSelected(项目)){
            返回true;
        }
        //手柄操作栏操作点击
        开关(item.getItemId()){
        案例R.id.action_settings:
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);
        }
    }

    / ***
     *当调用invalidateOptionsMenu()被触发
     * /
    @覆盖
    公共布尔prepareOptionsMenu(功能菜单)在{
        //如果导航抽屉被打开,隐藏行动项目
        布尔drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(drawerOpen!);
        返回super.on prepareOptionsMenu(菜单);
    }

    / **
     * Diplaying片段视图选择导航抽屉列表项
     * * /
    私人无效displayView(INT位置){
        //更新替换片段的主要内容
        ListFragment片段= NULL;
        开关(位置){
        情况下0:
            //片段=新HomeFragment();
            打破;
        情况1:
            片段=新CountryListFragment();
            打破;
        案例2:
            //片段=新PhotosFragment();
            打破;
        案例3:
           //片段=新CommunityFragment();
            打破;
        壳体4:
            //片段=新PagesFragment();
            打破;
        壳体5:
            //片段=新WhatsHotFragment();
            打破;

        默认:
            打破;
        }

        如果(片段!= NULL){
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.country_list_fragment,片段).commit();

            //更新所选项目和标题,然后关上抽屉
            mDrawerList.setItemChecked(位置,真正的);
            mDrawerList.setSelection(位置);
            的setTitle(navMenuTitles [位置]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } 其他 {
            //创建片段错误
            Log.e(MainActivity,创建片段错误);
        }
    }

    @覆盖
    公共无效的setTitle(CharSequence的标题){
        mTitle =称号;
        。getActionBar()的setTitle(mTitle);
    }

    / **
     *当使用ActionBarDrawerToggle,你必须在调用它
     * onPostCreate()和onConfigurationChanged()...
     * /

    @覆盖
    保护无效onPostCreate(包savedInstanceState){
        super.onPostCreate(savedInstanceState);
        //同步发生onRestoreInstanceState后切换状态。
        mDrawerToggle.syncState();
    }

    @覆盖
    公共无效onConfigurationChanged(配置NEWCONFIG){
        super.onConfigurationChanged(NEWCONFIG);
        //传递任何配置更改抽屉toggls
        mDrawerToggle.onConfigurationChanged(NEWCONFIG);
    }
    / **第一次创建活动时调用。 * /


    @覆盖
    公共无效onListFragmentItemClick(INT位置){

        / **获取屏幕*的方向(横向或纵向)/
        。INT方向= getResources()getConfiguration()方向。


        / **风景模式* /
        如果(定向== Configuration.ORIENTATION_LANDSCAPE){
            / **获取片段经理片段相关的操作* /
            FragmentManager fragmentManager = getFragmentManager();

            / **获取fragmenttransaction对象,它可以用来添加,删除或替换一个片段* /
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            / **获取现有详细片段的对象,如果它已经存在。
             *碎片对象检索标签名
             * * /
            片段prevFrag = fragmentManager.findFragmentByTag(in.wptrafficanalyzer.country.details);

            / **如果存在删除现有的详细片段对象* /
            如果(prevFrag!= NULL)
                fragmentTransaction.remove(prevFrag);

            / **实例化片段CountryDetailsFragment * /
            CountryDetailsFragment片段=新CountryDetailsFragment();

            / **创建一个包对象从活动数据(点击的项目的位置)传递给片段* /
            叠B =新包();

            / **数据设置到包对象* /
            b.putInt(位置,位置);

            / **捆绑对象设置为片段* /
            fragment.setArguments(B);

            / **添加片段的片段交易* /
            fragmentTransaction.add(R.id.detail_fragment_container,片段,in.wptrafficanalyzer.country.details);

            / **添加此交易backstack * /
            fragmentTransaction.addToBackStack(空);

            / **。事实上,本次交易* /
            fragmentTransaction.commit();

        }其他{/ **肖像模式或广场模式* /
            / **创建一个Intent对象启动CountryDetailsActivity * /
            意向意图=新的意图(in.wptrafficanalyzer.CountryDetailsActivity);

            / **设置数据(点击的项目的位置),以这个意图* /
            intent.putExtra(位置,位置);

            / **通过将隐式意图启动活动* /
            startActivity(意向);
        }
    }
}
 

在CountryListFragment为listfragment:

 包in.wptrafficanalyzer.listfragmentitemclick;

进口android.app.Activity;
进口android.app.ListFragment;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.Toast;

公共类CountryListFragment扩展ListFragment {

    / **要显示在ListFragment国家名单* /

    ListFragmentItemClickListener ifaceItemClickListener;

    / **定义回调方法的接口* /
    公共接口ListFragmentItemClickListener {
        / **在ListFragment一个项被点击时,此方法将被调用* /
        无效onListFragmentItemClick(INT位);
    }

    / **回调函数被执行时该片段被连接到一个活动* /
    @覆盖
    公共无效onAttach(活动活动){
        super.onAttach(活动);

        尝试{
            / **此语句确保托管活动实现ListFragmentItemClickListener * /
            ifaceItemClickListener =(ListFragmentItemClickListener)的活动;
        }赶上(例外五){
            Toast.makeText(activity.getBaseContext(),异常,Toast.LENGTH_SHORT).show();
        }
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){

        / **数据源ListFragment * /
        ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(inflater.getContext(),android.R.layout.simple_list_item_1,Country.name);

        / **数据源设置到ListFragment * /
        setListAdapter(适配器);



        返回super.onCreateView(充气,容器,savedInstanceState);
    }

    @覆盖
    公共无效onListItemClick(ListView的L,视图V,INT位置,长的id){

        / **调用该方法istFragmentItemClick的实施托管活动* /
        ifaceItemClickListener.onListFragmentItemClick(位置);

    }

}
 
九大APP界面导航模式推荐,快来get

在文件夹布局布局主

 >< android.support.v4.widget.DrawerLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent>




<的FrameLayout
    机器人:ID =@ + ID / country_list_fragment
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:名称=in.wptrafficanalyzer.listfragmentitemclick.CountryListFragment
    />


<! - 列表视图显示滑块菜单 - >
<的ListView
    机器人:ID =@ + ID / list_slidermenu
    机器人:layout_width =240dp
    机器人:layout_height =match_parent
    机器人:layout_gravity =开始
    机器人:choiceMode =singleChoice
    机器人:分隔=@色/ list_divider
    机器人:dividerHeight =1DP
    机器人:listSelector =@可绘制/ list_selector
    机器人:背景=@色/ list_background/>
   > < /android.support.v4.widget.DrawerLayout>
 

在文件夹中的布局,用地布局主要

 >< android.support.v4.widget.DrawerLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent>



<的FrameLayout
    机器人:ID =@ + ID / country_list_fragment
    机器人:layout_width =200dp
    机器人:layout_height =WRAP_CONTENT
    机器人:名称=in.wptrafficanalyzer.listfragmentitemclick.CountryListFragment

    />

<的FrameLayout
    机器人:ID =@ + ID / detail_fragment_container
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直
    机器人:layout_gravity =中心
    />


<! - 列表视图显示滑块菜单 - >
<的ListView
    机器人:ID =@ + ID / list_slidermenu
    机器人:layout_width =240dp
    机器人:layout_height =match_parent
    机器人:layout_gravity =开始
    机器人:choiceMode =singleChoice
    机器人:分隔=@色/ list_divider
    机器人:dividerHeight =1DP
    机器人:listSelector =@可绘制/ list_selector
    机器人:背景=@色/ list_background/>
   >< /android.support.v4.widget.DrawerLayout>
 

解决方案

我被这个迷惑了。我设置的导航抽屉我的应用程序的主要部分之间进行导航。然后,我想那些主要部分之一,成立作为主/从。我知道这是可能的,因为这基本上是Gmail是,但有一个很难把我所已经建立起来什么Eclipse中吐出来生成主/详细活动的时候。

我不能只是推出ItemListFragment(主/从)从MainActivity(导航抽屉),因为ItemListFragment要附加到ItemListActivity,不MainActivity和片段不能有两个活动。

我终于找到它的教程的实际上的使用的两种设计思路在一起: http://blog.evizija.si/android-layout/

我只是以他们为榜样,并得到了我的UI根据需要在5分钟的工作。这其实非常简单。你让MasterActivity看起来有点更像产生ItemListActivity(即实施TaskListFragment.Callbacks,复制在onItemSelected方法,和的onCreate的卡盘),你就大功告成了!

我希望这有助于解决您的问题!快乐编码!

更新:在阐述所涉及的步骤(从链接信息)的基础上,对提高我的答复意见反馈

(1)让你的抽屉活动和片段,让我们给他们打电话MainActivity和一些碎片,我们不关心这里。就个人而言,我会创建一个空的片段,就像ItemFragement例如,是一个占位主/详细信息,而我得到的抽屉和运行。然后,一旦抽屉工作根据需要,解决了主/详细信息和联系起来。

(2)使用IDE向导(我运行的Eclipse),使主/流的活动和片段。我将把他们通过各自的默认名称:ItemListActivity,ItemListFragement,ItemDetailActivity,ItemDetailFragment。

(3)如果您以前使用主/详细信息,你知道大部分的逻辑进入片段。这仍然是真实的,当与抽屉组合主/详细信息。请注意,在这一点上我们的MainActivity和我们的主/详细信息流,而后者甚至可能不是在UI访问之间没有任何联系。

(4)的关键概念::要与名单连接抽屉,我们MainActivity将是对ItemListFragment(而不是当前的ItemListActivity)主办的活动。为了使这项工作,我们只是复制了一些向导从ItemListActivity INTO MainActivity创建主/详细信息的魔法。的

(5)具体做法是:

(5A)MainActivity实现ItemListFragment.Callbacks(或EmployeeListFragment.Callbacks,AlbumListFragment.Callbacks,无论你是上市),并实施onItemSelected方法

 公共类MainActivity扩展活动
                          实现OnItemClickListener,ItemListFragment.Callbacks {
 

(5B)复制从的onCreate在ItemListActivity code部分,并将其粘贴在MainActivity到OnCreate中。该部分:

 如果(findViewById(R.id.item_detail_container)!= NULL){
    //细节容器视图将是$ P $中唯一psent
    //大屏幕布局(RES /值,大和
    // RES /值-sw600dp)。如果这种观点是present,那么
    //活动应在两个窗格模式。
    mTwoPane = TRUE;

    //在双窗格模式,列表中的项目应给予
    //激活状态,触摸时。
    ((ItemListFragment)getFragmentManager()
            .findFragmentById(R.id.item_list))
            .setActivateOnItemClick(真正的);
}
 

(5℃)也复制onItemSelected方法从ItemListActivity并将其粘贴到MainActivity。你会已经有一个onItemSelected方法,如果你对Eclipse的响应,将有步骤5A后所提出的错误添加未实现的方法。如果不这样做,复制在整个方法。 (这一步编辑回答记者提问的意见的)code:

 如果(mTwoPane){
    //在双窗格模式,通过展示本次活动的详细视图
    //添加或替换使用细节片段
    //片段交易。
    捆绑参数=新包();
    arguments.putString(ItemDetailFragment.ARG_ITEM_ID,ID);
    ItemDetailFragment片段=新ItemDetailFragment();
    fragment.setArguments(参数);
    getFragmentManager()的BeginTransaction()
            .replace(R.id.item_detail_container,片段)
            。承诺();

} 其他 {
    //在单窗格模式,只需启动具体活动
    //所选项目的ID。
    意图detailIntent =新的意图(这一点,ItemDetailActivity.class);
    detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID,ID);
    startActivity(detailIntent);
}
 

(6)然后在最后一个步骤是有MainActivity(抽屉)开放ItemListFragment。如果你已经有一个占位符片段启动(在步骤1中一样ItemFragement建议),这与ItemListFragment取代ItemFragment在onNavigationDrawerItemSelected方法只是早晚的问题。

希望是十分明显的。如果不是这样,原来的链接有可能做得更好解释比我做到了。只是走马观花到博主谈加入列表活动,其活动的抽屉的底部。

欢呼声。

更新:

被要求由版主,我该标记,另一个类似的问题(一式两份),这样做之后。

这些问题: combine抽屉式导航栏和主从布局 Navigation抽屉和主/流

i have get the sample navigation drawer from this site : http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

and the master detail from here : http://wptrafficanalyzer.in/blog/itemclick-handler-for-listfragment-in-android/

the error LogCat oncreateview(inflac....) the view can not be created

me i have try that

    //the main activiry as Activity:

    package in.wptrafficanalyzer.listfragmentitemclick;

import in.wptrafficanalyzer.listfragmentitemclick.adapter.NavDrawerListAdapter;
import in.wptrafficanalyzer.listfragmentitemclick.model.NavDrawerItem;

import java.util.ArrayList;

import in.wptrafficanalyzer.listfragmentitemclick.R;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class MainActivity extends Activity implements CountryListFragment.ListFragmentItemClickListener {



    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    // nav drawer title
    private CharSequence mDrawerTitle;

    // used to store app title
    private CharSequence mTitle;

    // slide menu items
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    private ArrayList<NavDrawerItem> navDrawerItems;
    private NavDrawerListAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTitle = mDrawerTitle = getTitle();

        // load slide menu items
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

        // nav drawer icons from resources
        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

        navDrawerItems = new ArrayList<NavDrawerItem>();

        // adding nav drawer items to array
        // Home
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
        // Find People
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
        // Photos
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
        // Communities, Will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
        // Pages
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
        // What's hot, We  will add a counter here
        navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));


        // Recycle the typed array
        navMenuIcons.recycle();

        mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

        // setting the nav drawer list adapter
        adapter = new NavDrawerListAdapter(getApplicationContext(),
                navDrawerItems);
        mDrawerList.setAdapter(adapter);

        // enabling action bar app icon and behaving it as toggle button
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, //nav menu toggle icon
                R.string.app_name, // nav drawer open - description for accessibility
                R.string.app_name // nav drawer close - description for accessibility
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            // on first time display view for first nav item
            displayView(0);
        }
    }

    /**
     * Slide menu item click listener
     * */
    private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // display view for selected nav drawer item
            displayView(position);
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // toggle nav drawer on selecting action bar app icon/title
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action bar actions click
        switch (item.getItemId()) {
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    /***
     * Called when invalidateOptionsMenu() is triggered
     */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // if nav drawer is opened, hide the action items
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    /**
     * Diplaying fragment view for selected nav drawer list item
     * */
    private void displayView(int position) {
        // update the main content by replacing fragments
        ListFragment fragment = null;
        switch (position) {
        case 0:
            //fragment = new HomeFragment();
            break;
        case 1:
            fragment = new CountryListFragment();
            break;
        case 2:
            //fragment = new PhotosFragment();
            break;
        case 3:
           // fragment = new CommunityFragment();
            break;
        case 4:
            //fragment = new PagesFragment();
            break;
        case 5:
            //fragment = new WhatsHotFragment();
            break;

        default:
            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.country_list_fragment, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }

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

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @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);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
    /** Called when the activity is first created. */


    @Override
    public void onListFragmentItemClick(int position) {

        /** Getting the orientation ( Landscape or Portrait ) of the screen */
        int orientation = getResources().getConfiguration().orientation;


        /** Landscape Mode */
        if(orientation == Configuration.ORIENTATION_LANDSCAPE ){
            /** Getting the fragment manager for fragment related operations */
            FragmentManager fragmentManager = getFragmentManager();

            /** Getting the fragmenttransaction object, which can be used to add, remove or replace a fragment */
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            /** Getting the existing detailed fragment object, if it already exists. 
             *  The fragment object is retrieved by its tag name 
             * */
            Fragment prevFrag = fragmentManager.findFragmentByTag("in.wptrafficanalyzer.country.details");

            /** Remove the existing detailed fragment object if it exists */
            if(prevFrag!=null)
                fragmentTransaction.remove(prevFrag);           

            /** Instantiating the fragment CountryDetailsFragment */
            CountryDetailsFragment fragment = new CountryDetailsFragment();

            /** Creating a bundle object to pass the data(the clicked item's position) from the activity to the fragment */ 
            Bundle b = new Bundle();

            /** Setting the data to the bundle object */
            b.putInt("position", position);

            /** Setting the bundle object to the fragment */
            fragment.setArguments(b);           

            /** Adding the fragment to the fragment transaction */
            fragmentTransaction.add(R.id.detail_fragment_container, fragment,"in.wptrafficanalyzer.country.details");

            /** Adding this transaction to backstack */
            fragmentTransaction.addToBackStack(null);

            /** Making this transaction in effect */
            fragmentTransaction.commit();

        }else{          /** Portrait Mode or Square mode */
            /** Creating an intent object to start the CountryDetailsActivity */
            Intent intent = new Intent("in.wptrafficanalyzer.CountryDetailsActivity");

            /** Setting data ( the clicked item's position ) to this intent */
            intent.putExtra("position", position);

            /** Starting the activity by passing the implicit intent */
            startActivity(intent);          
        }
    }
}

the CountryListFragment as listfragment :

package in.wptrafficanalyzer.listfragmentitemclick;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class CountryListFragment extends ListFragment{

    /** List of countries to be displayed in the ListFragment */

    ListFragmentItemClickListener ifaceItemClickListener;   

    /** An interface for defining the callback method */
    public interface ListFragmentItemClickListener {
        /** This method will be invoked when an item in the ListFragment is clicked */
        void onListFragmentItemClick(int position);
    }   

    /** A callback function, executed when this fragment is attached to an activity */  
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try{
            /** This statement ensures that the hosting activity implements ListFragmentItemClickListener */
            ifaceItemClickListener = (ListFragmentItemClickListener) activity;          
        }catch(Exception e){
            Toast.makeText(activity.getBaseContext(), "Exception",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        /** Data source for the ListFragment */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1, Country.name);

        /** Setting the data source to the ListFragment */
        setListAdapter(adapter);    



        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {    

        /** Invokes the implementation of the method istFragmentItemClick          in     the hosting activity */
        ifaceItemClickListener.onListFragmentItemClick(position);

    }

}

layout main in folder layout

><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">




<FrameLayout
    android:id="@+id/country_list_fragment"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:name="in.wptrafficanalyzer.listfragmentitemclick.CountryListFragment"
    />


<!-- Listview to display slider menu -->
<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"       
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>
   > </android.support.v4.widget.DrawerLayout>

layout main in the folder layout-land

><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">



<FrameLayout
    android:id="@+id/country_list_fragment"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:name="in.wptrafficanalyzer.listfragmentitemclick.CountryListFragment"

    />

<FrameLayout
    android:id="@+id/detail_fragment_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="center"
    />


<!-- Listview to display slider menu -->
<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"       
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>
   ></android.support.v4.widget.DrawerLayout>

解决方案

I was confused by this, too. I set up the Navigation Drawer to navigate between major sections of my app. Then I wanted one of those major sections to be set up as Master/Detail. I knew it was possible, since this is basically what Gmail is, but was having a hard time putting what I had already built together with what Eclipse spits out when generating a Master/Detail Activity.

I couldn't just launch the ItemListFragment (Master/Detail) from the MainActivity (Navigation Drawer), because ItemListFragment wants to attach to ItemListActivity, not MainActivity, and a fragment can't have two Activities.

I finally found a tutorial on it that actually used the two design ideas together: http://blog.evizija.si/android-layout/

I just followed their example and got my UI working as desired in about 5 minutes. It's actually incredibly simple. You make MasterActivity look a bit more like the generated ItemListActivity (ie. implement TaskListFragment.Callbacks, copy over the onItemSelected method, and a chuck of onCreate) and you're done!

I hope this helps solve your problem! Happy coding!

UPDATE: Elaborating on the steps involved (info from link), based on feedback in comments on improving my answer.

(1) Make your drawer activity and fragments, lets call them MainActivity and some fragments we don't care about here. Personally, I would create an empty Fragment, like ItemFragement for example, to be a place holder for the Master/Detail while I get the Drawer up and running. Then once the Drawer is working as desired, tackle the Master/Detail and linking them.

(2) Use the IDE wizard (I'm running Eclipse) to make the activities and fragments of master/detail flow. I'll refer to them by their default names: ItemListActivity, ItemListFragement, ItemDetailActivity, ItemDetailFragment.

(3) If you've used Master/Detail before, you know most of your logic goes into the fragment. This is still true when combining Master/Detail with Drawer. Note, at this point there is no connection between our MainActivity and our Master/Detail flow, and the later may not even be accessible in the UI.

(4) Key Concept: To connect the Drawer with the List, our MainActivity is going to be the hosting Activity for ItemListFragment (rather than the current ItemListActivity). To make this work, we just copy over some of the Master/Detail magic created by the wizard FROM ItemListActivity INTO MainActivity.

(5) Specifically:

(5A) MainActivity implements ItemListFragment.Callbacks (or EmployeeListFragment.Callbacks, AlbumListFragment.Callbacks, whatever you are listing) and implement onItemSelected method

public class MainActivity extends Activity 
                          implements OnItemClickListener, ItemListFragment.Callbacks {

(5B) Copy part of code from onCreate in ItemListActivity and paste it to onCreate in MainActivity. This part:

if (findViewById(R.id.item_detail_container) != null) {
    // The detail container view will be present only in the
    // large-screen layouts (res/values-large and
    // res/values-sw600dp). If this view is present, then the
    // activity should be in two-pane mode.
    mTwoPane = true;

    // In two-pane mode, list items should be given the
    // 'activated' state when touched.
    ((ItemListFragment) getFragmentManager()
            .findFragmentById(R.id.item_list))
            .setActivateOnItemClick(true);
}

(5C) Also copy onItemSelected method from ItemListActivity and paste it into MainActivity. You will already have an onItemSelected method if you told Eclipse to "add unimplemented methods" in response to the error that would have been raised after Step 5A. If you don't, copy over the whole method. (this step edited in response to a question in comments) Code:

if (mTwoPane) {
    // In two-pane mode, show the detail view in this activity by
    // adding or replacing the detail fragment using a
    // fragment transaction.
    Bundle arguments = new Bundle();
    arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
    ItemDetailFragment fragment = new ItemDetailFragment();
    fragment.setArguments(arguments);
    getFragmentManager().beginTransaction()
            .replace(R.id.item_detail_container, fragment)
            .commit();

} else {
    // In single-pane mode, simply start the detail activity
    // for the selected item ID.
    Intent detailIntent = new Intent(this, ItemDetailActivity.class);
    detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
    startActivity(detailIntent);
}

(6) Then the last step is to have MainActivity (the Drawer) open ItemListFragment. If you already have a placeholder Fragment launching (like ItemFragement suggested in Step 1), this is just a matter of replacing ItemFragment with ItemListFragment in the onNavigationDrawerItemSelected method.

Hope that's clear. If not, the original link might do a better job explaining than I did. Just skim to the bottom where the blogger talks about adding the list activity to their drawer activity.

cheers.

UPDATE:

After being ask to do so by a moderator, I'm flagging this and another similar question (as duplicates).

Those questions: combine navigation drawer and master detail layout Navigation Drawer and master/detail flow