[英]Display fragments with Navigation Drawer
我很困惑如何通過單擊導航抽屜中的一項來打開我的不同片段。
在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" />
但這是行不通的...
誰能幫我?
拉斯塔曼
要獲得您想要的內容,請嘗試使用這個看起來有些復雜的代碼,但它更易於使用
這里是詳細的解釋並逐步解決此問題:(似乎很長,但您可以盡快將您的工作關聯起來)
首次創建導航抽屜活動時,我們將查看5個文件:
步驟1:打開app_bar_main.xml ,注釋掉include並添加一個新的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:接下來,您將要添加一些片段為此,請右鍵單擊您的項目,或轉到File –> New,然后從Fragment列表中選擇Fragment(空白)或其他。
步驟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();
第四步:
然后,您需要向您的MainActivity實現的接口添加OnFragmentInteractionListener並實現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都實現接口。
這是在導航抽屜中實現片段加載所需要做的全部工作。 用戶點擊菜單項后,抽屜將順利滑入,新片段將已經開始/完成加載。 這還可以防止您在啟動新活動時看到的任何可能的混亂。
額外要注意的最后一件事是,如果您切換到另一個片段,然后旋轉設備或導致重新進行該活動,則上面的代碼將導致重新加載第一個片段。 一種簡單的處理方法是,將片段塊包裝在onCreate方法中,以檢查一下saveInstanceState是否不為null,如下所示:
protected void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState == null) {
//Fragment load code
}
...
}
試試這個代碼
@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" />
嗯,我找到了答案。 但是我很困惑。 如果我使用:
if (id == R.id.nav_listview) { fragment= new com.thomas.testapp.ListFragment();
有用。 但是,如果我要打開ListFragment,則只需使用包名。 為什么?
請更改您的片段類ListFragment的名稱,因為ListFragment已經是android.app 包 android OS中的Fragments的子類
我建議創建更動態的代碼
首先,如果您有用於創建Navigation Drawer的自定義類,請使用偵聽器從您的活動中調用方法,其中有類調用HomeFragment擴展Fragment,所以我們有:
if(condition){
fragment = new HomeFragment();
// if there is Listener
if(mListener != null){
mListener.someMethod
}
// Other Settings And Codes
}
public class MainActivity extends AppCompatActivity {
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...From section above...
// Find our drawer view
nvDrawer = (NavigationView) findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_first_fragment:
fragmentClass = FirstFragment.class;
break;
case R.id.nav_second_fragment:
fragmentClass = SecondFragment.class;
break;
case R.id.nav_third_fragment:
fragmentClass = ThirdFragment.class;
break;
default:
fragmentClass = FirstFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
// ...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.