Java 如何启用反压以仅关闭碎片上的导航抽屉
我已经为我的片段实现了一个导航抽屉,因此我想知道什么是只为片段实现反压功能的最佳方法,以及代码需要放在哪里?我在下面使用了一些反压代码,但它不起作用 活动类Java 如何启用反压以仅关闭碎片上的导航抽屉,java,android,android-fragments,navigation-drawer,onbackpressed,Java,Android,Android Fragments,Navigation Drawer,Onbackpressed,我已经为我的片段实现了一个导航抽屉,因此我想知道什么是只为片段实现反压功能的最佳方法,以及代码需要放在哪里?我在下面使用了一些反压代码,但它不起作用 活动类 public class BakerlooHDNActivity extends AppCompatActivity { //save our header or result private Drawer result = null; private int getFactorColor(int color,
public class BakerlooHDNActivity extends AppCompatActivity {
//save our header or result
private Drawer result = null;
private int getFactorColor(int color, float factor) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= factor;
color = Color.HSVToColor(hsv);
return color;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bakerloo_hdn);
LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("BACKPRESSED_TAG"));
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>"));
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);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
getWindow().setStatusBarColor(getFactorColor(Color.parseColor(actionBarColor), 0.8f));
}
// start of navigation drawer
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withCompactStyle(true)
.withHeaderBackground(R.color.bakerloo)
.withProfileImagesVisible(false)
.withTextColor(Color.parseColor("#FFFFFF"))
.withSelectionListEnabled(false)
.addProfiles(
new ProfileDrawerItem().withName(getString(R.string.hello)).withEmail(getString(R.string.world))
)
.build();
result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withTranslucentStatusBar(false)
.withActionBarDrawerToggle(false)
.withSelectedItem(-1)
.addDrawerItems(
new SectionDrawerItem().withName(R.string.other_lines_nocaps).setDivider(false),
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(context).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) {
// do stuff when back in activity is pressed
headerResult.closeDrawer();
}
};
// Declaring navigation drawer
private AccountHeader headerResult = null;
private Drawer result = null;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
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);
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)
.withSelectedItem(-1)
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.hello_world).withIdentifier(1).withCheckable(false)
)
.build();
super.onCreate(savedInstanceState);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
View v = getView();
super.onActivityCreated(savedInstanceState);
}
}
片段中没有onBackPressed()
。这是活动的方法。您可以做的是,在调用其onBackPressed()
时,让您的活动调用片段方法:
@Override
public void onBackPressed() {
if (!frag.onBackPressed() ) {
super.onBackPressed();
}
}
然后你的片段的onBackPressed()可以做任何你想做的事情,当它做任何事情时返回true,或者false`否则,在这种情况下你可以调用super的一个。你应该知道片段没有onBackPressed()我应该用什么替换frag
?引用要通知onBackPressed()的片段。很可能是您的FragmentHDN
FragmentHDN刚刚尝试过,但它不起作用。请检查最新的屏幕截图,因为片段中没有onBackPressed()
,所以@Override
不正确。您也需要将我的代码添加到活动中。你这样做了吗?首先,停止以图片形式发布文本。真烦人。此外,您似乎还缺乏java基础知识。您编写的内容只适用于静态方法,在这里没有任何用处。您需要使用findFragmentById()
(或.ByTag()
)查找片段,然后对所获得的对象调用方法