有没有办法可以从我的片段访问我在活动中创建的导航栏?我也希望能够使用后退功能。我的活动是在手机上发布的,我的片段是在平板电脑上发布的。
活动类
public class BakerlooHDNActivity extends AppCompatActivity {
private Drawer result = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bakerloo_hdn);
final String actionBarColor = "#B36305";
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
if(getSupportActionBar()!=null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().setTitle(Html.fromHtml("<font color='#FFFFFF'>" + getResources().getString(R.string.hdn) + "</font>"));
getSupportActionBar().setSubtitle(Html.fromHtml("<font color='#FFFFFF'>" + getResources().getString(R.string.zone_3) + "</font>"));
final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
}
// start of navigation drawer
headerResult = new AccountHeaderBuilder()
.withActivity(getActivity())
.withCompactStyle(true)
.withHeaderBackground(R.color.bakerloo)
.withProfileImagesVisible(false)
.withTextColor(Color.parseColor("#FFFFFF"))
.withSelectionListEnabled(false)
.addProfiles(
new ProfileDrawerItem().withName(getString(R.string.hdn)).withEmail(getString(R.string.hello_world))
)
.build();
result = new DrawerBuilder()
.withActivity(getActivity())
.withAccountHeader(headerResult)
.withTranslucentStatusBar(false)
.withActionBarDrawerToggle(false)
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.hello_world).withIdentifier(1).withCheckable(false)
)
.build();
// end of navigation drawer
}
@Override
public void onBackPressed() {
if (result.isDrawerOpen()) {
result.closeDrawer();
} else {
super.onBackPressed();
}
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(new Intent("BACKPRESSED_TAG"));
}
}
片段类
public class FragmentBakerlooHDN extends android.support.v4.app.Fragment {
public FragmentBakerlooHDN() {
// Required empty constructor
}
BroadcastReceiver onNotice = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
result.closeDrawer();
}
};
private AccountHeader headerResult = null;
private Drawer result = null;
private boolean mTwoPane;
@Override
public void onCreate(Bundle savedInstanceState) {
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(onNotice, new IntentFilter("BACKPRESSED_TAG"));
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_bakerloo_hdn, container, false);
super.onCreate(savedInstanceState);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
View v = getView();
super.onActivityCreated(savedInstanceState);
}
}
答案 0 :(得分:0)
我永远不会有片段直接访问抽屉。相反,我会通过Fragment中的回调方法调用Activity,然后访问抽屉。
重新阅读您的问题后,我建议如下:
如果您仍需要与Fragment沟通,请执行以下操作:
.PHONY
在你的片段中添加:
public void onBackPressed() {
if (result.isDrawerOpen()) {
result.closeDrawer();
getSupportFragmentManager().findFragmentByTag("FRAGMENT_TAG").doSomething();
} else {
super.onBackPressed();
}
}
答案 1 :(得分:0)
我的建议基本上是在片段中创建一个公共方法。并且活动调用传递抽屉对象的公共方法。真的很简单。这具有通过引用传递抽屉的效果。
代码建议。 在BakerlooHDNActivity类中:
AccountHeader headerResult;
if (savedInstanceState == null) {
FragmentBakerlooHDN myFragment = new FragmentBakerlooHDN();
myFragment.setDrawer(headerResult);
}
在FragmentBakerlooHDN课程中:
private AccountHeader myDrawer;
public void setDrawer(final AccountHeader drawer) {
this.myDrawer = drawer;
}
注意:
答案 2 :(得分:0)
我的建议是在Activity中使用onBackPressed方法。我更新了关于NavigationDrawer(https://github.com/AlexZhukovich/NavigationDrawerTutorial)的教程的源代码。我希望它对你有所帮助。我在本教程中使用了com.android.support:design中的NavigationDrawer。
@Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(mNavigationView))
mDrawerLayout.closeDrawers();
else
super.onBackPressed();
}
对我而言,它适用于平板电脑和手机。 (Nexus设备)。
我为你创建了带片段的简单活动。此活动显示2个片段,但在单击后,删除第二个片段。
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
getSupportFragmentManager().beginTransaction().add(R.id.container, new DummyFragment(), "TEST").commit();
getSupportFragmentManager().beginTransaction().add(R.id.container2, new DummyFragment(), "TEST2").commit();
}
@Override
public void onBackPressed() {
if (getSupportFragmentManager().findFragmentByTag("TEST2") != null && getSupportFragmentManager().findFragmentByTag("TEST2").isVisible()) {
getSupportFragmentManager().beginTransaction().remove(getSupportFragmentManager().findFragmentByTag("TEST2")).commit();
} else {
super.onBackPressed();
}
}
private class DummyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test, container, false);
return view;
}
}
}