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,"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 as Listfragment:
package in.wptrafficanalyzer.Listfragmentitemclick;import androID.app.Activity;import androID.app.ListFragment;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;import androID.Widget.Toast;public class CountryListFragment extends ListFragment{ /** List of countrIEs to be displayed in the ListFragment */ ListFragmentItemClickListener ifaceItemClickListener; /** An interface for defining the callback method */ public interface ListFragmentItemClickListener { /** This method will be invoked when an item in the ListFragment is clicked */ voID onListFragmentItemClick(int position); } /** A callback function,executed when this fragment is attached to an activity */ @OverrIDe public voID onAttach(Activity activity) { super.onAttach(activity); try{ /** This statement ensures that the hosting activity implements ListFragmentItemClickListener */ ifaceItemClickListener = (ListFragmentItemClickListener) activity; }catch(Exception e){ Toast.makeText(activity.getBaseContext(),"Exception",Toast.LENGTH_SHORT).show(); } } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { /** Data source for the ListFragment */ ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(),androID.R.layout.simple_List_item_1,Country.name); /** Setting the data source to the ListFragment */ setlistadapter(adapter); return super.onCreateVIEw(inflater,container,savedInstanceState); } @OverrIDe public voID onListItemClick(ListVIEw l,VIEw v,long ID) { /** Invokes the implementation of the method istFragmentItemClick in the hosting activity */ ifaceItemClickListener.onListFragmentItemClick(position); }}
布局主要在文件夹布局中
><androID.support.v4.Widget.DrawerLayoutxmlns: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.DrawerLayoutxmlns: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>解决方法 我也对此感到困惑.我设置导航抽屉以在我的应用程序的主要部分之间导航.然后我想要将其中一个主要部分设置为Master / Detail.我知道这是可能的,因为这基本上就是Gmail,但在生成Master / Detail Activity时,很难将我已经构建的内容与Eclipse吐出的内容放在一起.
我无法从MainActivity(导航抽屉)中启动ItemListFragment(主/细节),因为ItemListFragment想要附加到ItemListActivity,而不是MainActivity,并且片段不能有两个Activity.
我终于找到了一个实际使用这两个设计思想的教程:
http://blog.evizija.si/android-layout/
我只是按照他们的例子,让我的UI在大约5分钟内按照需要工作.它实际上非常简单.你使Masteractivity看起来更像生成的ItemListActivity(即实现TaskListFragment.Callbacks,复制onItemSelected方法,以及onCreate的一个chuck)你已经完成了!
我希望这有助于解决您的问题!快乐的编码!
更新:基于对改进我的答案的评论中的反馈,阐述所涉及的步骤(来自链接的信息).
(1)让你的抽屉活动和碎片,让我们称之为MainActivity和我们在此不关心的一些片段.就个人而言,我会创建一个空的片段,例如ItemFragement,作为Master / Detail的占位符,同时让Drawer启动并运行.然后,一旦抽屉按照需要工作,解决主/细节并链接它们.
(2)使用IDE向导(我正在运行Eclipse)来创建主/详细信息流的活动和片段.我将通过它们的默认名称来引用它们:ItemListActivity,ItemListFragement,ItemDetailActivity,ItemDetailFragment.
(3)如果您之前使用过Master / Detail,那么您知道大部分逻辑都会进入片段.将Master / Detail与Drawer结合使用时仍然如此.注意,此时我们的MainActivity和Master / Detail流程之间没有任何关联,后者甚至可能无法在UI中访问.
(4)关键概念:要将抽屉与列表连接起来,我们的MainActivity将成为ItemListFragment的托管活动(而不是当前的ItemListActivity).为了完成这项工作,我们只需复制一些由向导FROM ItemListActivity INTO MainActivity创建的Master / Detail魔法.
(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即可.
希望很清楚.如果没有,原始链接可能比我更好地解释.只是浏览一下博客谈论将列表活动添加到抽屉活动的底部.
干杯.
更新:
在被主持人要求这样做之后,我正在标记这个和另一个类似的问题(重复).
那些问题:
https://stackoverflow.com/questions/25403377/combine-navigation-drawer-and-master-detail-layout
Navigation Drawer and master/detail flow
以上是内存溢出为你收集整理的android – 如何使用片段主细节实现导航抽屉全部内容,希望文章能够帮你解决android – 如何使用片段主细节实现导航抽屉所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)