6

我已接到該網站的抽樣導航抽屜:從這裏 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 .. ..)視圖 不能創建

我,我已經嘗試

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

的CountryListFragment爲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); 

    } 

} 

佈局主要在文件夾佈局

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

佈局主要

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

回答

11

我被這個迷惑了。我設置了導航抽屜在我的應用程序的主要部分之間導航。然後,我希望將其中一個主要部分設置爲主/細部。我知道這是可能的,因爲這基本上就是Gmail,但是很難將我已經構建的東西與Eclipse在生成主/細節活動時吐出的內容結合在一起。

我無法從MainActivity(Navigation Drawer)啓動ItemListFragment(Master/Detail),因爲ItemListFragment想要附加到ItemListActivity而不是MainActivity,並且一個片段不能有兩個活動。

我終於找到了一個教程上說實際上使用的兩種設計思路在一起: http://blog.evizija.si/android-layout/

我只是他們爲榜樣,並得到了我的UI根據需要在大約5分鐘的工作。這實際上非常簡單。你讓MasterActivity看起來更像是生成的ItemListActivity(即實現TaskListFragment.Callbacks,通過onItemSelected方法複製,以及onCreate的查詢),你就完成了!

我希望這有助於解決您的問題!快樂的編碼!


UPDATE:在闡述相關的步驟(從鏈接信息)的基礎上,對提高我的答覆意見反饋。 (1)讓你的抽屜活動和片段,讓我們稱之爲MainActivity和一些我們不在乎的片段。就我個人而言,我會創建一個空的片段,例如ItemFragement,以便在啓動並運行抽屜時成爲主/細節的佔位符。然後,一旦抽屜工作正常,解決主/細節並鏈接它們。 (2)使用IDE嚮導(我正在運行Eclipse)製作主/細節流的活動和片段。我將通過它們的默認名稱引用它們:ItemListActivity,ItemListFragement,ItemDetailActivity,ItemDetailFragment。 (3)如果你以前使用過主/細節,你知道你的大部分邏輯進入片段。將主/細節與抽屜相結合時仍然如此。請注意,在這一點上,我們的MainActivity和我們的主/細節流之間沒有任何關聯,並且後者可能甚至不能在UI中訪問。

(4)關鍵概念:抽屜與名單連接,我們的MainActivity將是對ItemListFragment(而不是當前的ItemListActivity)主辦的活動。爲了完成這個工作,我們只需複製一些由嚮導創建的Master/Detail魔術師從ItemListActivity INTO MainActivity。

(5)特別是:

(5A)MainActivity實現ItemListFragment.Callbacks(或EmployeeListFragment.Callbacks,AlbumListFragment.Callbacks,不管你正在上市)和實施onItemSelected方法

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

(5B )在ItemListActivity中複製來自onCreate的部分代碼,並將其粘貼到MainActivity中的onCreate中。這部分:

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)也從ItemListActivity複製onItemSelected方法並將其粘貼到MainActivity中。如果您告訴Eclipse「添加未實現的方法」以響應在步驟5A之後引發的錯誤,那麼您將已經擁有一個onItemSelected方法。如果你不這樣做,請複製整個方法。 (該步驟中響應編輯以在註釋一個問題)代碼:

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)然後,最後一步是具有MainActivity(抽屜)開放ItemListFragment。如果你已經有一個佔位符Fragment啓動了(就像步驟1中建議的ItemFragement),這只是在onNavigationDrawerItemSelected方法中用ItemListFragment替換ItemFragment的問題。

希望這是明確的。如果沒有,原來的鏈接可能比我做得更好。只需瀏覽博客講述將列表活動添加到其抽屜活動的底部即可。

歡呼聲。


UPDATE:

是問一個主持人,我這個標記和另一個相似的問題(一式兩份),這樣做之後。

這些問題:
https://stackoverflow.com/questions/25403377/combine-navigation-drawer-and-master-detail-layout
Navigation Drawer and master/detail flow


+1

這有助於給實際的代碼示例的工作,因爲鏈接到博客,之後一段時間 – aarosil

+0

MainActivity是導航抽屜活動等可能會死,所以它沒有一個onSelectedItem方法。那麼這段代碼去哪裏?它是否進入onNavigationDrawerItemSelected?因爲這看起來不對。 –

+0

@RayKiddy它在onItemSelected。在指定MainActivity實現ItemListFragment.Callbacks(步驟5A)後,您需要添加該方法。我也爲後代的答案添加了這個澄清。 –