gpt4 book ai didi

java - 如何使用抽屉导航嵌套 fragment ?

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:50 25 4
gpt4 key购买 nike

过去几个小时我一直在尝试搜索SO,但除了尝试创建解决方法之外我找不到解决方案。我正在尝试为我的应用程序创建一个抽屉导航,但要做到这一点,我最终不得不尝试将我的基本 Activity 更改为 fragment 。这是结果:

public class ToolReaderActivity extends Fragment implements ToolListFragment.OnToolSelectedListener, CompatActionBarNavListener, OnClickListener {

boolean mIsDualPane = false;

Context c;

ToolListFragment mToolListFragment;
InfoFragment mInfoFragment;
Fragment mContent;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
c = getActivity();
}

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

View rootView = inflater.inflate(R.layout.onepane_with_bar, container, false);

// find our fragments
mToolListFragment = (ToolListFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.toolList);
mInfoFragment = (InfoFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.toolInfo);

FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.toolList, mToolListFragment).commit();

FragmentTransaction transaction2 = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.toolInfo, mInfoFragment).commit();

mToolListFragment.setContext(c);
mToolListFragment.setOnToolSelectedListener(this);

View infoView = getView().findViewById(R.id.toolInfo);
mIsDualPane = infoView != null && infoView.getVisibility() == View.VISIBLE;

int catIndex = savedInstanceState == null ? 0 : savedInstanceState.getInt("catIndex", 0);
setUpActionBar(mIsDualPane, catIndex);

mToolListFragment.setSelectable(mIsDualPane);
restoreSelection(savedInstanceState);

if (savedInstanceState != null) {
//Restore the fragment's instance
mContent = getActivity().getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
}

return rootView;
}

...
...
...
}

预期的输出是一个带有抽屉导航的 Activity ,因此是一个 fragment 。该 fragment 包含此主要 Activity ToolReaderActivity(之所以如此命名,是因为它以前是一个 Activity ),其中有 1-2 个 fragment 。它是一个 ListFragment,其中包含一个显示所选项目信息的 fragment (如果 mDualPane 为 true,则作为另一个 fragment ,否则作为单独的 Activity 启动)。

我想不出一种方法来让它工作,我已经厌倦了使用 onCreate、onCreateView、onActivityCreated 更改事情完成的顺序,但我不断收到的错误是 mToolsListFragment 在 mToolsListFragment.setContext(c) 处为 null;

最佳答案

您可以使用下面的代码简单地创建NavigationDrawer

public class MainActivity extends AppCompatActivity
{
private NavigationView mNavigationView;
private DrawerLayout mDrawerLayout;
private Toolbar toolbar;
private ActionBarDrawerToggle mDrawerToggle;
private FragmentActivity fragment;
private FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDrawerNavigation();
fragmentManager = getFragmentManager();
}

public void initDrawerNavigation()
{
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
{

@Override
public boolean onNavigationItemSelected(MenuItem item)
{
item.setChecked(true);

switch (item.getItemId())
{
case R.id.st_menu:
fragment = new FirstFragment();
break;

case R.id.nd_menu:
fragment = new Second Fragment();
break;
}

fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();

mDrawerLayout.closeDrawers();
return true;
}
});

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open,
R.string.drawer_close)
{
// Called when a drawer has settled in a completely closed state.
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
}

// Called when a drawer has settled in a completely open state.
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
}
};

mDrawerLayout.addDrawerListener(mDrawerToggle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}

@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){

switch(item.getItemId())
{
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
return super.onOptionsItemSelected(item);
}
}

这里有activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<include
layout="@layout/your_layout" />

<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
app:menu="@menu/navigation_items" />
</android.support.v4.widget.DrawerLayout>

</LinearLayout>

your_layout 可以是您想要的任何内容。现在您定义您的 FirstFragment 类,如下所示:

public class FirstFragment extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.target_layout, container, false);

return view;
}
}

您也可以引用这个问题Navigation Drawer to switch between activities如果您想在 Activity 而不是 Fragments 之间切换。希望这会有所帮助。

关于java - 如何使用抽屉导航嵌套 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37977033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com