添加的EditText上的抽屉式导航栏的Andr​​oid [英] Add EditText on Navigation Drawer Android

查看:161
本文介绍了添加的EditText上的抽屉式导航栏的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im在一个新的应用正在与抽屉式导航(使用Android的模板)。但我想替换另一个视图的默认列表视图。

解决:

我要修改我的.java code修改CreateView的方法,以配合我的布局。感谢所有

问:

我的类从扩展的 ActionBarActivity 和农具的 NavigationDrawerFragment.NavigationDrawerCallbacks

我希望做一些这样的:

  ---------
EDITTEXT
- - - - - (分隔器)
(ListView中有链接)
链接1
链接2
- - - - (分隔器)
(ListView中有链接)
链接1
链接2
...
..

好吧,我这个尝试:

activity_mail.xml

 < 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
    工具:上下文=com.example.myapp>    <的FrameLayout
        机器人:ID =@ + ID /容器
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>    <片段
        机器人:ID =@ + ID / navigation_drawer
        机器人:名字=com.extasis.musichunter.NavigationDrawerFragment
        机器人:layout_width =@扪/ navigation_drawer_width
        机器人:layout_height =match_parent
        机器人:layout_gravity =开始/>< /android.support.v4.widget.DrawerLayout>

fragment_navigation_drawer.xml(不工作的版本)

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的EditText
        机器人:ID =@ + ID / buscador
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        安卓的inputType =TEXT>        < requestFocus的/>
    < /&的EditText GT;    < ListView控件
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:背景=#CCCC
        机器人:choiceMode =singleChoice
        机器人:分=@机器人:彩色/透明
        机器人:dividerHeight =0dp
        工具:上下文=com.example.myapp/>< / LinearLayout中>

fragment_navigation_drawer.xml(工作版本)

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

NavigationDrawerFragment.java

 包com.extasis.musichunter;进口android.support.v7.app.ActionBarActivity;
进口android.app.Activity;
进口android.support.v7.app.ActionBar;
进口android.support.v4.app.Fragment;
进口android.support.v4.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_1,
                android.R.id.text1,
                新的String [] {
                        的getString(R.string.title_section1)
                        的getString(R.string.title_section2)
                        的getString(R.string.title_section3)
                }));
        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.drawable.ic_drawer,/ *导航抽屉图像替换上插入符* /
                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,真).commit();
                }                。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位置);
    }
}

这是我得到的错误:

  18 07-14:00:02.971:E / AndroidRuntime(20518):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.myapp / com。示例。 myapp.MainActivity}:android.view.InflateException:二进制XML文件行#31:错误充气类片段

我想,我必须修改 NavigationDrawerFragment 类,但我不知道,我的元素必须改变,添加或删除。任何人可以帮助我呢?


解决方案

编辑:

和你聊天后,我们看到您的NavigationDrawerClass预计这mthod一个ListView:

  @覆盖
公共查看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_1,
            android.R.id.text1,
            新的String [] {
                    的getString(R.string.title_section1)
                    的getString(R.string.title_section2)
                    的getString(R.string.title_section3)
            }));
    mDrawerListView.setItemChecked(mCurrentSelectedPosition,真);
    返回mDrawerListView;
}

在此功能的视图中创建并构建各种版面项目。你将不得不改变code,这样不仅片段建立一个列表视图,相反,你必须手动建立菜单文件,当创建视图膨胀的XML文件,而不是只有一个列表。

很抱歉不能提供code大气压,时间有点低: - )

旧的答案从我这里,编辑之前:


  

我觉得你在你的应用程序/项目缺少的不同点。我看到,你仍然有这样的行


 工具:上下文=com.example.myapp

在您的文件。既然你说你使用的模板,你必须调整这自己的应用程序名称等不知道你这样做,我不能看到它提供了code。

但我看到的是,在另一条线路有:

 的android:NAME =com.extasis.musichunter.NavigationDrawerFragment

所以,一个2是不对的我猜。

您应该重新检查教程网页应用基础上的Andr​​oid开发者部分。

否则,你的code是不是错了,你可以简单地把任何东西在菜单中,像这样

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直的android:layout_width =match_parent
机器人:layout_height =match_parent><的TextView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=新文本
    机器人:ID =@ + ID / TextView的/><的EditText
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / EDITTEXT/>< ListView控件
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID /的ListView/><的TextView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=新文本
    机器人:ID =@ + ID / textView2/>
< / LinearLayout中>

Im working on a new app with Navigation Drawer (Using Android template). But I want replace the default listview with another views.

SOLVED:

I have to edit my .java code to modify the createView method to match with my layout. Thanks for all

QUESTION:

My class extends from ActionBarActivity and implements NavigationDrawerFragment.NavigationDrawerCallbacks

I want make some like this:

---------
EDITTEXT
--------- (separator)
(ListView with Links)
Link 1
Link 2
--------(separator)
(ListView with Links)
Link 1
Link 2
...
..
.

Ok, I was trying with this:

activity_mail.xml

<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="com.example.myapp" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fragment
        android:id="@+id/navigation_drawer"
        android:name="com.extasis.musichunter.NavigationDrawerFragment"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start" />

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

fragment_navigation_drawer.xml (not working version)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/buscador"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#cccc"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        tools:context="com.example.myapp" />

</LinearLayout>

fragment_navigation_drawer.xml (working version)

<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:background="#cccc"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    tools:context="com.extasis.musichunter.NavigationDrawerFragment" />

NavigationDrawerFragment.java

package com.extasis.musichunter;

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.v4.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_1,
                android.R.id.text1,
                new String[]{
                        getString(R.string.title_section1),
                        getString(R.string.title_section2),
                        getString(R.string.title_section3),
                }));
        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.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
                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).commit();
                }

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

This is the error that I get:

07-14 18:00:02.971: E/AndroidRuntime(20518): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.MainActivity}: android.view.InflateException: Binary XML file line #31: Error inflating class fragment

I suppose that I have to modify the NavigationDrawerFragment class but I dont know that elements I have to change, add or remove. Can anybody help me with this?

解决方案

Edit:

After chatting with you we saw that your NavigationDrawerClass expects a ListView in this mthod:

@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_1,
            android.R.id.text1,
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
            }));
    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
    return mDrawerListView;
}

In this function the View is created and build of the various layout items. You would have to change the code so that the fragment not only build a listview, instead you would have to manually build the menu-file and when create the view inflate that xml file instead of only a List.

Sorry to not provide code atm, the time is running a bit low :-)

Old answer from me, before edit:

I think your missing different points in your app/project. I see that you still have lines like this

tools:context="com.example.myapp"

in your files. Since you said youre using a template you have to adjust this to your own apps name etc. Not sure you did that, i cannot see it from provided code.

But what i see is that in another line there is:

android:name="com.extasis.musichunter.NavigationDrawerFragment"

So one the 2 is not right i guess.

You should re-check App fundamentals on the Tutorial Pages on developer Section on Android.

Otherwise your code is not that wrong, you can simply put anything at the menu, like this

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView2" />


</LinearLayout>

这篇关于添加的EditText上的抽屉式导航栏的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆