我是Android的新手,我开发了一个只有活动的应用程序,现在我想更改它并创建了一个带有碎片的导航抽屉。
问题是我不知道如何将活动改为碎片。
我尝试过的事情:
首先,我创建了一个BlankFragment
并将其调整为我的活动,此片段目前称为BlankFragment,这是布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="games.asee.videogames_asee_project.BlankFragment">
<ListView
android:id="@+id/listTOP"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/background_light" />
</FrameLayout>
这是片段的类(我只修改了onCreateView中的代码):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
JuegosOperaciones crud = JuegosOperaciones.getInstance(getContext());
items = crud.getTOP();
adapter = new JuegoAdapter(getContext(), items);
ListView itemsListView = (ListView) getView().findViewById(R.id.listTOP);
itemsListView.setAdapter(adapter);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
但是我不知道如何在navigationDrawer中调用片段,我试着调用getFragmentManager
,但应用程序没有做任何事情!
} else if (id == R.id.nav_manage) {
// getFragmentManager().beginTransaction().add(R.id.listTOP,new BlankFragment()).commit();
}
如果您需要查看更多代码,请告诉我。
再次感谢你们!
答案 0 :(得分:0)
首先在您的主要活动中初始化导航抽屉项目并进行设置。点击导航项后,替换帧布局上的片段 使用此代码
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fTransaction = manager.beginTransaction();
fTransaction.replace(R.id.frameForPane, my_fragment);
fTransaction.commit();
mainDrawer.closeDrawers();
并使用以下方法设置MainActivity.class:
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();
}
// ...
}
这将帮助您集成打开下一个片段,点击导航项。