2014-07-14 155 views
1

我正在使用導航抽屜(使用Android模板)創建新應用程序。但我想用另一個視圖替換默認的listview。在導航抽屜上添加EditText Android

解決:

我要修改我的.java代碼修改CreateView的方法,以配合我的佈局。感謝所有

問題:

我的類從ActionBarActivity延伸,並實現NavigationDrawerFragment.NavigationDrawerCallbacks

我希望做一些這樣的:

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

好吧,我試圖用這個:

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(不工作版)

<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(工作版本)

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

這是錯誤,我得到:

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 

我想我不得不修改NavigationDrawerFragment類,但我不知道元素我必須改變,添加或刪除。任何人都可以幫助我嗎?

回答

1

編輯:

跟你聊天后,我們看到您的NavigationDrawerClass預計這mthod一個ListView:

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

在此功能的視圖中創建和構建各種佈局項目。您必須更改代碼,以便片段不僅構建listview,而且必須手動構建菜單文件,並且在創建視圖時將該xml文件充氣,而不僅僅是一個List。

抱歉不能提供代碼ATM,時間有點低:-)

從我老的答案,修改前:

我覺得你在你的應用程序丟失的不同點/項目。我看你還是有一個像這樣

tools:context="com.example.myapp" 
在文件中

線。既然你說你使用的是模板,你必須將其調整爲你自己的應用程序名稱等等。不確定你是否這樣做了,我從所提供的代碼中看不到它。

但我看到的是,在另一條線路有:

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

所以一個2是不對的我猜。

您應該重新檢查Android上開發人員部分的教程頁面上的應用程序基礎。

否則你的代碼是不是錯了,你可以簡單地把任何東西在菜單中,這樣

<?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> 
+0

對不起,我想隱藏數據包名稱和foget這一行。 :) – Genaut

+0

呵呵,是的,它發生:-) – Senchay

+0

xml代碼看起來不錯,但我得到的錯誤,因爲我編輯它,並添加「EditText」元素,我想我必須從片段編輯onCreateView或使用另一種結構來實現具有混合視圖的抽屜導航菜單? – Genaut

0

1.確保yourActivity擴展android.support.v4.widget.DrawerLayout 2.inflate異常只是另一個問題的處理程序。 問題是內存不足異常。 3.添加了android supportv4.jar

+0

我的類從ActionBarActivity延伸,並實現NavigationDrawerFragment.NavigationDrawerCallbacks(這來自於Android應用所選擇的navegation類型嚮導) 提到的錯誤,我認爲它是由充氣(它與edittext + listview衝突的列表視圖),因爲如果我刪除edittext視圖,這就像一個魅力(但只有一個列表視圖) – Genaut