2015-10-16 260 views
0

我很困惑如何通過單擊我的導航抽屜中的一個項目來打開我的不同片段。使用導航抽屜顯示片段

在MainActivity我用下面的代碼:

@SuppressWarnings("StatementWithEmptyBody") 
 
@Override 
 
public boolean onNavigationItemSelected(MenuItem item) { 
 
    Fragment fragment; 
 

 
    int id = item.getItemId(); 
 

 
    if (id == R.id.nav_listview) { 
 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
 
     ft.replace(R.id.fragment, new ListFragment()); 
 
     ft.commit(); 
 

 
    } else if (id == R.id.nav_add_data) { 
 

 
    } else if (id == R.id.nav_settings) { 
 

 
    } else if (id == R.id.nav_legal_information) { 
 

 
    } 
 

 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
 
    drawer.closeDrawer(GravityCompat.START); 
 
    return true; 
 
}

所有我想嘗試打開我的ListFragment首先:

import android.os.Bundle; 
 
import android.support.v4.app.Fragment; 
 
import android.view.LayoutInflater; 
 
import android.view.View; 
 
import android.view.ViewGroup; 
 

 

 
public class ListFragment extends Fragment { 
 

 

 
    @Override 
 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
 

 
     return inflater.inflate(R.layout.fragment_list, container, false); 
 
    } 
 
}

在我的內容main.xml中,我創建了以下片段,在點擊導航抽屜中的特定項目時應該替換它。

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

但它不是工作...

誰能幫助我?

Rastaman

+0

任何錯誤?或者片段沒有被替換? –

+0

不,我用設計工具手動添加我的fragment_list片段。現在我可以在屏幕上看到它,但是如果我點擊導航抽屜中的「列表」項目,我會在片段中間獲得加載循環,但沒有任何反應。 – Rastaman

+0

你有沒有嘗試添加或實施其他片段? – Droidwala

回答

0

請更改您的片段類的名稱ListFragment因爲ListFragment已經在android中的子類的片段。應用 Android操作系統

1

試試這個代碼

@SuppressWarnings("StatementWithEmptyBody") 
 
@Override 
 
public boolean onNavigationItemSelected(MenuItem item) { 
 
    Fragment fragment=null; 
 
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
 
    int id = item.getItemId(); 
 

 
    if (id == R.id.nav_listview) { 
 
     
 
     fragment= new ListFragment(); 
 
    
 

 
    } else if (id == R.id.nav_add_data) { 
 

 
    } else if (id == R.id.nav_settings) { 
 

 
    } else if (id == R.id.nav_legal_information) { 
 

 
    } 
 
ft.replace(R.id.container, fragment); 
 
    ft.addToBackStack(null); 
 
    ft.commit(); 
 
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
 
    drawer.closeDrawer(GravityCompat.START); 
 
    return true; 
 
}

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

+0

錯誤:(116,37)錯誤:變量片段可能未被初始化 – Rastaman

+0

我添加了: public boolean onNavigationItemSelected(MenuItem item){ 片段片段; FragmentTransaction ft = getSupportFragmentManager()。beginTransaction(); fragment = new ListFragment(); 將標準片段初始化爲ListFragment。但它沒有工作.. 但一般的代碼似乎是正確的。我可以這樣打開我的AddDataFragment。但我沒有得到ListFragment的女巫... – Rastaman

0

MHMM我找到了答案。但我很困惑。 如果我使用:

   if (id == R.id.nav_listview) { 
 
       fragment= new com.thomas.testapp.ListFragment();

它的工作原理。但是我只需要使用包名就可以打開我的ListFragment。爲什麼?

+0

有一個名爲「ListFragment」的官方課程。重命名後,東西工作正常。 – Rastaman

0

我建議創建的代碼更加動態

首先,如果你有自定義的類來創建抽屜式導航,使用監聽器調用從您的活動方法 有類調用HomeFragment延伸片段,所以我們有:

if(condition){ 
    fragment = new HomeFragment(); 
    // if there is Listener 
    if(mListener != null){ 
     mListener.someMethod 
    } 
    // Other Settings And Codes 
} 
2

爲了得到你想要嘗試這個東西,我還舉辦了這個有些複雜的看着代碼,但它更多的是簡單的方法來使用

下面是詳細的解釋,並步行穿過這個問題: (它似乎長,但你可以很容易地涉及儘快你的工作)

當你第一次創建的抽屜式導航活動,有5對文件,我們來看看:

  • MainActivity.java - 這是後面的代碼爲我們的應用程序的一切。
  • activity_main.xml中 - 這是該應用包括NAV抽屜佈局和一個包括用於app_bar_main。
  • app_bar_main.xml - 這是帶有工具欄(位於頂部),浮動操作按鈕(位於右下角)和content_main的包含的佈局。
  • content_main.xml - 這是主要的頁面內容佈局。
  • nav_header_main.xml - 這是爲資產淨值抽屜的頂部的UI。

第1步: 開拓app_bar_main.xml,註釋掉包括並添加一個新的FrameLayout:

<!--<include layout="@layout/content_main" />--> 

<FrameLayout 
    android:id="@+id/Fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginTop="?attr/actionBarSize"/> 

這是FrameLayout裏我們將使用加載我們的片段成。

第2步: 接下來,您要添加一些片段 要做到這一點,右鍵點擊你的項目,或轉到文件 - >新建,並從片段列表中選擇片段(空白)或其他

第3步: 下一步是在應用程序首次啓動時加載一個片段。轉至MainActivity的onCreate方法,並把下面的調用之後setSupportActionBar

Fragment fragment = null; 
Class fragmentClass = null; 
fragmentClass = FragmentOne.class; 
try { 
    fragment = (Fragment) fragmentClass.newInstance(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

FragmentManager fragmentManager = getSupportFragmentManager(); 
fragmentManager.beginTransaction().replace(R.id.Fragment_container, fragment).commit(); 

第4步:

然後,您需要添加OnFragmentInteractionListener到您的MainActivity實現並且還實施onFragmentInteraction方法。像這樣

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener, FragmentOne.OnFragmentInteractionListener ,FragmentTwo.OnFragmentInteractionListener,FragmentThree.OnFragmentInteractionListener { 

步驟5: 最後,在onNavigationItemSelected方法,您可以添加加載不同片段的能力時,菜單項挖掘:

public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 
    Fragment fragment = null; 
    Class fragmentClass = null; 
    if (id == R.id.nav_camera) { 
     fragmentClass = FragmentOne.class; 
    } else if (id == R.id.nav_gallery) { 
     fragmentClass = FragmentTwo.class; 
    } else if (id == R.id.nav_slideshow) { 
     fragmentClass = FragmentThree.class; 
    } else if (id == R.id.nav_manage) { 

    } else if (id == R.id.nav_share) { 

    } else if (id == R.id.nav_send) { 

    } 
    try { 
     fragment = (Fragment) fragmentClass.newInstance(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit(); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

在這裏,我只是裝我已經添加到我的應用程序的兩個片段之一。請注意,因爲我有兩個不同的片段,所以我必須爲FragmentOne.OnFragmentInteractionListener和FragmentTwo.OnFragmentInteractionListener實現接口。

這就是你在導航抽屜中實現片段加載所需要做的。 用戶點擊菜單項時,抽屜會順利滑回,並且新的片段已經開始/完成加載。這還可以防止在啓動新活動時可能看到的任何可能的騷動。

注意EXTRA 最後一件事是,如果你切換到不同的片段,然後旋轉該裝置,或者導致活性的另一娛樂,上述代碼將導致第一片段被重新加載。一個簡單的方法來處理是包裹在onCreate方法的片段塊進行檢查,看是否savedInstanceState不爲空這樣的:

protected void onCreate(Bundle savedInstanceState) { 
    ... 
    if (savedInstanceState == null) { 
     //Fragment load code 
    } 
    ... 
}