2014-10-28 79 views
0

在我的抽屜式導航欄我用這種方法打開活動: 編輯如何打開使用導航抽屜onClickListener片段()

mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        Fragment fragment = null; 
        @SuppressLint("ResourceAsColor") @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 
Fragment fragment = null; 
      FragmentManager fragmentManager = getFragmentManager(); 
      Bundle bundle = new Bundle(); 
         switch (position) { 

         case 0:  

          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str))); 

          break; 

         case 1:  

          fragment = new TrimFragment(); 
         fragment.setArguments(bundle); 
         fragmentManager.beginTransaction() 
         .replace(R.id.content_frame, fragment).commit(); 

          break; 

          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(strg))); 

          break; 

         case 3:  

          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(strtwit))); 

          break; 
         } 

        } 
       }); 
      } 

但我想要的是開放的,現在一個片段。是否有可能從這個結構中做到這一點?由於

+0

[http://developer.android.com/training/implementing-navigation/nav-drawer.html](http://developer.android.com/training/implementing-navigation/nav-drawer.html) – 2014-10-28 08:54:11

回答

0

添加/替換要在容器,顯示在適當的情況下

0

在開關的情況下,使用此代碼片段。如果您想用導航抽屜列表中單擊的那個替換現有片段,這就是如何操作的。

//通過更換片段

Fragment fragment = null; 
      FragmentManager fragmentManager = getFragmentManager(); 
      Bundle bundle = new Bundle(); 
      switch (position) { 
      case 0: 

       fragment = new Dashboard_Fragment(); 
       break; 
      case 1: 

       fragment = new SearchCustomer_Fragment(); 
       //Request Origin 
       bundle.putString("origin", "searchCustomer"); 
       fragment.setArguments(bundle); 
fragmentManager.beginTransaction() 
      .replace(R.id.frame_container, fragment).commit(); 

       break; 
      case 2: 

       fragment = new STB_Check_Fragment(); 
fragmentManager.beginTransaction() 
      .replace(R.id.frame_container, fragment).commit(); 
       break; 


    } 
+0

好吧......什麼也沒發生。有沒有提交的地方? – 2014-10-28 09:01:06

+0

對不起,請參閱編輯答案。 – Akhil 2014-10-28 09:03:10

+0

mmh,現在試着看我的答案。仍然沒有工作:( – 2014-10-28 09:07:10

0

更新主要內容您可以顯示片段,而不是活動:

看到這個片段。

mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); //onclick listener 

/* The click listner for ListView in the navigation drawer */ 
private class DrawerItemClickListener implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     setCharacter(position); 
    } 
} 

//here a new fragment is created and replaces the old one. 
public void setCharacter(int position){ 
    Fragment fragment = new CharacterFragment(); 
    Bundle args = new Bundle(); 
    args.putInt(CharacterFragment.ARG_CHAR_NUMBER, position); 
    fragment.setArguments(args); 

    FragmentManager fragmentManager = getFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 

    // update selected item and title, then close the drawer 
    mDrawerList.setItemChecked(position, true); 
    setTitle(mCharacterTitles[position]); 
    mDrawerLayout.closeDrawer(mLinearLayout); 
} 

@Override 
public void setTitle(CharSequence title) { 
    // TODO Auto-generated method stub 
    mTitle = title; 
    getActionBar().setTitle(mTitle); 
} 

/** 
* Fragment that appears in the "content_frame", shows a planet 
*/ 
public static class CharacterFragment extends Fragment { 
    public static final String ARG_CHAR_NUMBER = "character_number"; 

    public CharacterFragment() { 
     // Empty constructor required for fragment subclasses 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_character, container, false); 
     int i = getArguments().getInt(ARG_CHAR_NUMBER); 
     String charName = getResources().getStringArray(R.array.characters_array)[i]; 

     TextView charTextView = (TextView) rootView.findViewById(R.id.text); 
     charTextView.setText(charName); 
     getActivity().setTitle(charName); 
     return rootView; 
    } 
} 

希望這會有所幫助。