[英]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.