本文介绍了在由Android工作室制作导览活动实施expandablelistview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施 expandablelistview 我的内置导航抽屉式活动。

我想这样的..


  • 首页

  • 类别

    • submenucategory

    • submenucategory2

    • submenucategory3


  • 我的帐户

  • 关于

MainActivity.java

 包com.example.administrator.mosbeau;进口android.app.Activity;
进口android.support.v7.app.ActionBarActivity;
进口android.support.v7.app.ActionBar;
进口android.app.Fragment;
进口android.app.FragmentManager;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.support.v4.widget.DrawerLayout;
公共类MainActivity扩展ActionBarActivity
        实现NavigationDrawerFragment.NavigationDrawerCallbacks {    / **
     *片段管理导航抽屉的行为,相互作用和presentation。
     * /
    私人NavigationDrawerFragment mNavigationDrawerFragment;    / **
     *用于存储最后屏幕标题。对于使用{@link #restoreActionBar()}。
     * /
    私人CharSequence的mTitle;    布尔InternetAvailable = FALSE;
    Seocnd detectconnection;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        detectconnection =新Seocnd(本);
        InternetAvailable = detectconnection.InternetConnecting();
        如果(InternetAvailable){
        }其他{
            NointernetFragment片段=新NointernetFragment();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container,片段)
                    。承诺();
        }        mNavigationDrawerFragment =(NavigationDrawerFragment)
                。getSupportFragmentManager()findFragmentById(R.id.navigation_drawer);
        mTitle =的getTitle();        //设置抽屉。
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout)findViewById(R.id.drawer_layout));
    }    @覆盖
    公共无效onNavigationDrawerItemSelected(INT位置){
        //更新替换片段的主要内容        FragmentManager fragmentManager = getFragmentManager();        // 新的东西
        如果(位置== 0){
            fragmentManager.beginTransaction()
                    .replace(R.id.container,HomeFragment.newInstance())
                    。承诺();
        }
        否则如果(位置== 1){
            fragmentManager.beginTransaction()
                    .replace(R.id.container,CategoryFragment.newInstance())
                    。承诺();
        }
        否则如果(位置== 2){
            fragmentManager.beginTransaction()
                    .replace(R.id.container,AccountFragment.newInstance())
                    。承诺();
        }
        否则如果(位置== 3){
            fragmentManager.beginTransaction()
                    .replace(R.id.container,ReferFragment.newInstance())
                    。承诺();
        }
        否则,如果(位置== 4){
            fragmentManager.beginTransaction()
                    .replace(R.id.container,AboutFragment.newInstance())
                    。承诺();
        }
        否则,如果(位置== 5){
            fragmentManager.beginTransaction()
                    .replace(R.id.container,PolicyFragment.newInstance())
                    。承诺();
        }
        否则,如果(位置== 6){
            fragmentManager.beginTransaction()
                    .replace(R.id.container,TermsFragment.newInstance())
                    。承诺();
        }
        否则,如果(位置== 7){
            fragmentManager.beginTransaction()
                    .replace(R.id.container,ContactusFragment.newInstance())
                    。承诺();
        }
    }    公共无效onSectionAttached(INT编号){
        开关(数){
            情况1:
                mTitle =的getString(R.string.title_home);
                打破;
            案例2:
                mTitle =的getString(R.string.title_category);
                打破;
            案例3:
                mTitle =的getString(R.string.title_login);
                打破;
            情况4:
                mTitle =的getString(R.string.title_register);
                打破;
            情况5:
                mTitle =的getString(R.string.title_myaccount);
                打破;
            情况6:
                mTitle =的getString(R.string.title_referafriend);
                打破;
            案例7:
                mTitle =的getString(R.string.title_about);
                打破;
            案例8:
                mTitle =的getString(R.string.title_privacypocity);
                打破;
            案例9:
                mTitle =的getString(R.string.title_shippingterms);
                打破;
            案例10:
                mTitle =的getString(R.string.title_contactus);
                打破;
        }
    }    @燮pressWarnings(德precation)
    公共无效restoreActionBar(){
        动作条动作条= getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(真);
        actionBar.setTitle(mTitle);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        如果(!mNavigationDrawerFragment.isDrawerOpen()){
            //只显示操作栏中的相关与这个画面项目
            //如果抽屉没有显示。否则,让抽屉
            //决定如何操作栏中显示。
            。getMenuInflater()膨胀(R.menu.main,菜单);
            restoreActionBar();
            返回true;
        }
        返回super.onCreateOptionsMenu(菜单);
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_settings){
            返回true;
        }        返回super.onOptionsItemSelected(项目);
    }    / **
     *包含一个简单视图中的占位符片段。
     * /
    公共静态类PlaceholderFragment扩展片段{
        / **
         *片段参数重新presenting本节号
         *片段。
         * /
        私有静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;        / **
         *返回该片段的一个新实例为给定的部
         *号。
         * /
        公共静态的newInstance PlaceholderFragment(INT sectionNumber){
            PlaceholderFragment片段=新PlaceholderFragment();
            捆绑ARGS =新包();
            args.putInt(ARG_SECTION_NUMBER,sectionNumber);
            fragment.setArguments(参数);
            返回片段;
        }        公共PlaceholderFragment(){
        }        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                                 捆绑savedInstanceState){
            查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,FALSE);
            返回rootView;
        }        @覆盖
        公共无效onAttach(活动活动){
            super.onAttach(活动);
            ((MainActivity)活性).onSectionAttached(
                    。getArguments()调用getInt(ARG_SECTION_NUMBER));
        }    }}

NavigationDrawerFragment.java

 包com.example.administrator.mosbeau;进口android.support.v7.app.ActionBarActivity;
进口android.app.Activity;
进口android.support.v7.app.ActionBar;
进口android.support.v4.app.Fragment;
进口android.support.v7.app.ActionBarDrawerToggle;
进口android.support.v4.view.GravityCompat;
进口android.support.v4.widget.DrawerLayout;
进口android.content.Shared preferences;
进口android.content.res.Configuration;
进口android.os.Bundle;
。进口的Andr​​oid preference preferenceManager。
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
进口android.widget.Toast;/ **
 *用于管理并导航抽屉presentation互动片段。
 *请参阅的< A HREF =htt​​ps://developer.android.com/design/patterns/navigation-drawer.html#Interaction>
 *设计准则< / A>在这里实施的行为的一个完整的解释。
 * /
公共类NavigationDrawerFragment扩展片段{    / **
     *请记住所选项目的位置。
     * /
    私有静态最后弦乐STATE_SELECTED_POSITION =selected_navigation_drawer_position;    / **
     *每设计指南,您应该手动上展示推出的抽屉里,直到用户
     *扩展它。这种共享preference跟踪此。
     * /
    私有静态最后弦乐preF_USER_LEARNED_DRAWER =navigation_drawer_learned;    / **
     *一个指向当前回调实例(活动)。
     * /
    私人NavigationDrawerCallbacks mCallbacks;    / **
     *是关系操作栏中的导航抽屉助手组件。
     * /
    私人ActionBarDrawerToggle mDrawerToggle;    私人DrawerLayout mDrawerLayout;
    私人的ListView mDrawerListView;
    私人查看mFragmentContainerView;    私人INT mCurrentSelectedPosition = 0;
    私人布尔mFromSavedInstanceState;
    私人布尔mUserLearnedDrawer;    公共NavigationDrawerFragment(){
    }    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        //读取在该标志指示用户是否已经证实的认识
        //抽屉里。详情请参阅preF_USER_LEARNED_DRAWER。
        共享preferences SP = preferenceManager.getDefaultShared preferences(getActivity());
        mUserLearnedDrawer = sp.getBoolean(preF_USER_LEARNED_DRAWER,FALSE);        如果(savedInstanceState!= NULL){
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = TRUE;
        }        //选择任一默认项(0)或最后选择的项目。
        选择信息(mCurrentSelectedPosition);
    }    @覆盖
    公共无效onActivityCreated(捆绑savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        //表明该片段想影响的一组操作栏中的行动。
        setHasOptionsMenu(真);
    }    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                             捆绑savedInstanceState){
        mDrawerListView =(ListView控件)inflater.inflate(
                R.layout.fragment_navigation_drawer,集装箱,FALSE);
        mDrawerListView.setOnItemClickListener(新AdapterView.OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
                选择信息(位置);
            }
        });
        mDrawerListView.setAdapter(新ArrayAdapter<串GT;(
                getActionBar()。getThemedContext(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                新的String [] {
                        的getString(R.string.title_home)
                        的getString(R.string.title_category)
                        的getString(R.string.title_myaccount)
                        的getString(R.string.title_referafriend)
                        的getString(R.string.title_about)
                        的getString(R.string.title_privacypocity)
                        的getString(R.string.title_shippingterms)
                        的getString(R.string.title_contactus)
                }));
        mDrawerListView.setItemChecked(mCurrentSelectedPosition,真);
        返回mDrawerListView;
    }    公共布尔isDrawerOpen(){
        返回mDrawerLayout = NULL&放大器;!&安培; mDrawerLayout.isDrawerOpen(mFragmentContainerView);
    }    / **
     *这个片段的用户必须调用这个方法来设置导航抽屉相互作用。
     *
     * @参数fragmentId Android的:在它的活动的布局这个片段的ID。
     * @参数drawerLayout包含此片段的UI的DrawerLayout。
     * /
    公共无效设置(INT fragmentId,DrawerLayout drawerLayout){
        mFragmentContainerView = getActivity()findViewById(fragmentId)。
        mDrawerLayout = drawerLayout;        //抽屉打开时设置覆盖的主要内容自定义阴影
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START);
        //设置抽屉的列表视图与项目,然后点击监听器        动作条动作条= getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(真);
        actionBar.setHomeButtonEnabled(真);        // ActionBarDrawerToggle联系在一起的适当交互
        //抽屉式导航栏和操作栏的程序图标之间。
        mDrawerToggle =新ActionBarDrawerToggle(
                getActivity()/ *主机活动* /
                mDrawerLayout,/ * DrawerLayout对象* /
                R.string.navigation_drawer_open,/ *开放抽屉的描述可访问* /
                R.string.navigation_drawer_close / *关闭抽屉的描述可访问* /
        ){
            @覆盖
            公共无效onDrawerClosed(查看drawerView){
                super.onDrawerClosed(drawerView);
                如果(!isAdded()){
                    返回;
                }                。getActivity()supportInvalidateOptionsMenu();在prepareOptionsMenu //调用()
            }            @覆盖
            公共无效onDrawerOpened(查看drawerView){
                super.onDrawerOpened(drawerView);
                如果(!isAdded()){
                    返回;
                }                如果(!mUserLearnedDrawer){
                    //用户手动打开抽屉;该标志存储prevent自动显示
                    //在未来自动导航的抽屉里。
                    mUserLearnedDrawer = TRUE;
                    共享preferences SP = preferenceManager
                            .getDefaultShared preferences(getActivity());
                    。sp.edit()putBoolean(preF_USER_LEARNED_DRAWER,真实)。适用();
                }                。getActivity()supportInvalidateOptionsMenu();在prepareOptionsMenu //调用()
            }
        };        //如果用户没有学习关于抽屉,打开它给他们介绍的抽屉里,
        //每个抽屉式导航栏的设计指南。
        如果(mUserLearnedDrawer&安培;!&安培;!mFromSavedInstanceState){
            mDrawerLayout.openDrawer(mFragmentContainerView);
        }        //推迟code依赖于previous实例状态的恢复。
        mDrawerLayout.post(新的Runnable(){
            @覆盖
            公共无效的run(){
                mDrawerToggle.syncState();
            }
        });        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }    私人无效选择信息(INT位置){
        mCurrentSelectedPosition =位置;
        如果(mDrawerListView!= NULL){
            mDrawerListView.setItemChecked(位置,真正的);
        }
        如果(mDrawerLayout!= NULL){
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        如果(mCallbacks!= NULL){
            mCallbacks.onNavigationDrawerItemSelected(位置);
        }
    }    @覆盖
    公共无效onAttach(活动活动){
        super.onAttach(活动);
        尝试{
            mCallbacks =(NavigationDrawerCallbacks)活性;
        }赶上(抛出ClassCastException E){
            抛出新ClassCastException异常(活动必须实现NavigationDrawerCallbacks。);
        }
    }    @覆盖
    公共无效onDetach(){
        super.onDetach();
        mCallbacks = NULL;
    }    @覆盖
    公共无效的onSaveInstanceState(捆绑outState){
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SELECTED_POSITION,mCurrentSelectedPosition);
    }    @覆盖
    公共无效onConfigurationChanged(配置NEWCONFIG){
        super.onConfigurationChanged(NEWCONFIG);
        //转发新配置抽屉切换组件。
        mDrawerToggle.onConfigurationChanged(NEWCONFIG);
    }    @覆盖
    公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
        //如果抽屉被打开,显示在操作栏中的应用程序的全球行动。也可以看看
        // showGlobalContextActionBar,控制操作栏的左上角区域。
        如果(mDrawerLayout =空&放大器;!&放大器; isDrawerOpen()){
            inflater.inflate(R.menu.global,菜单);
            showGlobalContextActionBar();
        }
        super.onCreateOptionsMenu(菜单,充气器);
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        如果(mDrawerToggle.onOptionsItemSelected(项目)){
            返回true;
        }        如果(item.getItemId()== R.id.action_example){
            Toast.makeText(getActivity(),示例行动,Toast.LENGTH_SHORT).show();
            返回true;
        }        返回super.onOptionsItemSelected(项目);
    }    / **
     *每导航抽屉式设计的指导方针,更新操作栏,显示全球应用
     *语境,而不仅仅是什么是在当前屏幕。
     * /
    私人无效showGlobalContextActionBar(){
        动作条动作条= getActionBar();
        actionBar.setDisplayShowTitleEnabled(真);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setTitle(R.string.app_name);
    }    私有动作条getActionBar(){
        返回((ActionBarActivity)getActivity())getSupportActionBar();
    }    / **
     *回调接口,使用这个片段的所有活动必须实现。
     * /
    公共静态界面NavigationDrawerCallbacks {
        / **
         *当选择导航抽屉项目时调用。
         * /
        无效onNavigationDrawerItemSelected(INT位置);
    }
}

activity_main.xml中

 <! - 一个DrawerLayout拟用作使用宽度和高度match_parent消耗全部可用空间的顶级内容视图。 - >
< android.support.v4.widget.DrawerLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s机器人:ID =@ + ID / drawer_layout
    机器人:layout_width =match_parent机器人:layout_height =match_parent
    工具:上下文=MainActivity。>    <! - 为主要内容的视图,该视图下方消耗约占整个
         使用match_parent在两个维度可用空间。 - >
    <的FrameLayout机器人:ID =@ + ID /容器的android:layout_width =match_parent
        机器人:layout_height =match_parent/>    < - 安卓!layout_gravity =启动讲述DrawerLayout治疗
         以此为在左侧的滑动抽屉左到右
         语言和右侧从右到左的语言。
         如果你不反对建设17 API或更高,使用
         机器人:layout_gravity =左代替。 - >
    <! - 抽屉给出DP一个固定的宽度并延伸的全高
         的容器。 - >
    <片段机器人:ID =@ + ID / navigation_drawer
    机器人:layout_width =@扪/ navigation_drawer_width机器人:layout_height =match_parent
    机器人:layout_gravity =开始
    机器人:名字=com.example.administrator.mosbeau.NavigationDrawerFragment
    工具:布局=@布局/ fragment_navigation_drawer/>< /android.support.v4.widget.DrawerLayout>

fragment_main.xml

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s的android:layout_width =match_parent
    机器人:layout_height =match_parent机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    工具:上下文=$ MainActivity PlaceholderFragment。>    < TextView的机器人:ID =@ + ID / section_label的android:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>< / RelativeLayout的>

fragment_navigation_drawer.xml

 < ListView控件的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s的android:layout_width =match_parent
    机器人:layout_height =match_parent机器人:choiceMode =singleChoice
    机器人:分=@机器人:彩色/透明的机器人:dividerHeight =0dp
    机器人:背景=#FFFFFF工具:上下文=。NavigationDrawerFragment
    机器人:listSelector =#EEEEEE/>


解决方案

您需要做的是同样的方式,你会在任何使用的ListView适配器返回标题行和细节行添加标题。

从这些链接尝试可以帮助你
Try第一然后下面的替代品。




  1. 里面的ListView

I want to implement expandablelistview to my built in navigation drawer activity.

i want like this..

  • Home
  • Category
    • submenucategory
    • submenucategory2
    • submenucategory3
  • My Account
  • About

MainActivity.java

package com.example.administrator.mosbeau;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;


public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

    Boolean InternetAvailable = false;
    Seocnd detectconnection;

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

        detectconnection = new Seocnd(this);
        InternetAvailable = detectconnection.InternetConnecting();
        if (InternetAvailable) {
        } else {
            NointernetFragment fragment = new NointernetFragment();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();
        }

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments

        FragmentManager fragmentManager = getFragmentManager();

        // NEW STUFF
        if(position == 0){
            fragmentManager.beginTransaction()
                    .replace(R.id.container, HomeFragment.newInstance())
                    .commit();
        }
        else if (position == 1){
            fragmentManager.beginTransaction()
                    .replace(R.id.container, CategoryFragment.newInstance())
                    .commit();
        }
        else if (position == 2){
            fragmentManager.beginTransaction()
                    .replace(R.id.container, AccountFragment.newInstance())
                    .commit();
        }
        else if (position == 3){
            fragmentManager.beginTransaction()
                    .replace(R.id.container, ReferFragment.newInstance())
                    .commit();
        }
        else if (position == 4){
            fragmentManager.beginTransaction()
                    .replace(R.id.container, AboutFragment.newInstance())
                    .commit();
        }
        else if (position == 5){
            fragmentManager.beginTransaction()
                    .replace(R.id.container, PolicyFragment.newInstance())
                    .commit();
        }
        else if (position == 6){
            fragmentManager.beginTransaction()
                    .replace(R.id.container, TermsFragment.newInstance())
                    .commit();
        }
        else if (position == 7){
            fragmentManager.beginTransaction()
                    .replace(R.id.container, ContactusFragment.newInstance())
                    .commit();
        }
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_home);
                break;
            case 2:
                mTitle = getString(R.string.title_category);
                break;
            case 3:
                mTitle = getString(R.string.title_login);
                break;
            case 4:
                mTitle = getString(R.string.title_register);
                break;
            case 5:
                mTitle = getString(R.string.title_myaccount);
                break;
            case 6:
                mTitle = getString(R.string.title_referafriend);
                break;
            case 7:
                mTitle = getString(R.string.title_about);
                break;
            case 8:
                mTitle = getString(R.string.title_privacypocity);
                break;
            case 9:
                mTitle = getString(R.string.title_shippingterms);
                break;
            case 10:
                mTitle = getString(R.string.title_contactus);
                break;
        }
    }

    @SuppressWarnings("deprecation")
    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

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

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }

    }

}

NavigationDrawerFragment.java

package com.example.administrator.mosbeau;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Fragment used for managing interactions for and presentation of a navigation drawer.
 * See the <a href="https://developer.android.com/design/patterns/navigation-drawer.html#Interaction">
 * design guidelines</a> for a complete explanation of the behaviors implemented here.
 */
public class NavigationDrawerFragment extends Fragment {

    /**
     * Remember the position of the selected item.
     */
    private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

    /**
     * Per the design guidelines, you should show the drawer on launch until the user manually
     * expands it. This shared preference tracks this.
     */
    private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";

    /**
     * A pointer to the current callbacks instance (the Activity).
     */
    private NavigationDrawerCallbacks mCallbacks;

    /**
     * Helper component that ties the action bar to the navigation drawer.
     */
    private ActionBarDrawerToggle mDrawerToggle;

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerListView;
    private View mFragmentContainerView;

    private int mCurrentSelectedPosition = 0;
    private boolean mFromSavedInstanceState;
    private boolean mUserLearnedDrawer;

    public NavigationDrawerFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Read in the flag indicating whether or not the user has demonstrated awareness of the
        // drawer. See PREF_USER_LEARNED_DRAWER for details.
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
        mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);

        if (savedInstanceState != null) {
            mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
            mFromSavedInstanceState = true;
        }

        // Select either the default item (0) or the last selected item.
        selectItem(mCurrentSelectedPosition);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // Indicate that this fragment would like to influence the set of actions in the action bar.
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mDrawerListView = (ListView) inflater.inflate(
                R.layout.fragment_navigation_drawer, container, false);
        mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        });
        mDrawerListView.setAdapter(new ArrayAdapter<String>(
                getActionBar().getThemedContext(),
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                new String[]{
                        getString(R.string.title_home),
                        getString(R.string.title_category),
                        getString(R.string.title_myaccount),
                        getString(R.string.title_referafriend),
                        getString(R.string.title_about),
                        getString(R.string.title_privacypocity),
                        getString(R.string.title_shippingterms),
                        getString(R.string.title_contactus),
                }));
        mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
        return mDrawerListView;
    }

    public boolean isDrawerOpen() {
        return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
    }

    /**
     * Users of this fragment must call this method to set up the navigation drawer interactions.
     *
     * @param fragmentId   The android:id of this fragment in its activity's layout.
     * @param drawerLayout The DrawerLayout containing this fragment's UI.
     */
    public void setUp(int fragmentId, DrawerLayout drawerLayout) {
        mFragmentContainerView = getActivity().findViewById(fragmentId);
        mDrawerLayout = drawerLayout;

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the navigation drawer and the action bar app icon.
        mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
                R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (!isAdded()) {
                    return;
                }

                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!isAdded()) {
                    return;
                }

                if (!mUserLearnedDrawer) {
                    // The user manually opened the drawer; store this flag to prevent auto-showing
                    // the navigation drawer automatically in the future.
                    mUserLearnedDrawer = true;
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(getActivity());
                    sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
                }

                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }
        };

        // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
        // per the navigation drawer design guidelines.
        if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
            mDrawerLayout.openDrawer(mFragmentContainerView);
        }

        // Defer code dependent on restoration of previous instance state.
        mDrawerLayout.post(new Runnable() {
            @Override
            public void run() {
                mDrawerToggle.syncState();
            }
        });

        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

    private void selectItem(int position) {
        mCurrentSelectedPosition = position;
        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mCallbacks = (NavigationDrawerCallbacks) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Forward the new configuration the drawer toggle component.
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // If the drawer is open, show the global app actions in the action bar. See also
        // showGlobalContextActionBar, which controls the top-left area of the action bar.
        if (mDrawerLayout != null && isDrawerOpen()) {
            inflater.inflate(R.menu.global, menu);
            showGlobalContextActionBar();
        }
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        if (item.getItemId() == R.id.action_example) {
            Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * Per the navigation drawer design guidelines, updates the action bar to show the global app
     * 'context', rather than just what's in the current screen.
     */
    private void showGlobalContextActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setTitle(R.string.app_name);
    }

    private ActionBar getActionBar() {
        return ((ActionBarActivity) getActivity()).getSupportActionBar();
    }

    /**
     * Callbacks interface that all activities using this fragment must implement.
     */
    public static interface NavigationDrawerCallbacks {
        /**
         * Called when an item in the navigation drawer is selected.
         */
        void onNavigationDrawerItemSelected(int position);
    }
}

activity_main.xml

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- 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.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead. -->
    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.example.administrator.mosbeau.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />



</android.support.v4.widget.DrawerLayout>

fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$PlaceholderFragment">

    <TextView android:id="@+id/section_label" android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

fragment_navigation_drawer.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:choiceMode="singleChoice"
    android:divider="@android:color/transparent" android:dividerHeight="0dp"
    android:background="#ffffff" tools:context=".NavigationDrawerFragment"
    android:listSelector="#eeeeee" />
解决方案

You have to do that the same way as you would add headings in any ListView using Adapter to return heading rows as well as detail rows.

Try from these link may help for youTry first then following alternatives.

  1. Navigation Drawer : add headers in listview
  2. ExpandableListView in navigation drawer
  3. expandable listview inside navigation drawer
  4. How To Make Material Design Navigation Drawer With Header View

这篇关于在由Android工作室制作导览活动实施expandablelistview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 04:48