抽屉布局不关闭背面pressed(取决于支持-V4 LIB) [英] Drawer Layout not closing on back pressed (depending on support-v4 lib)
问题描述
我下面的创建一个抽屉式导航教程(安卓培训)。
我已经下载了示例应用程序,它发挥作用的。我打开抽屉,preSS回来了,它被关闭。
这个问题是,当我更换的lib / Android的支持 - v4.jar
(原为523 KB大小)在更新版本(版本20)我的 Android的SDK /演员/安卓/支持/ V4 / Android的支持 - v4.jar
(大小为741 KB)。更换后,如果我打开抽屉,然后preSS装置的返回按钮的应用程序被关闭的关闭抽屉代替。
我该如何解决呢?我应该保留旧的 Android的支持 - v4.jar 版本或者我应该编程拦截后退按钮需要时关闭抽屉?
更新:我有继续测试问题,这样做,我建AndroidStudio项目。复制后的示例应用程序(SRC,资源和更新清单)运行它:后退按钮关闭即使DrawerLayout是打开的应用程序。 然后,我修改了build.gradle文件改变行:
编译com.android.support:appcompat-v7:20.0.0
与
编译com.android.support:appcompat-v7:19.0.0
和disapeared问题:后退按钮先关闭抽屉,然后关闭应用程序
全部code的活性(链接到Android培训来源$ C $ C):
公共类MainActivity延伸活动{
私人DrawerLayout mDrawerLayout;
私人的ListView mDrawerList;
私人ActionBarDrawerToggle mDrawerToggle;
私人的CharSequence mDrawerTitle;
私人的CharSequence mTitle;
私有String [] mPlanetTitles;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
mTitle = mDrawerTitle =的getTitle();
mPlanetTitles = getResources()getStringArray(R.array.planets_array)。
mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList =(ListView控件)findViewById(R.id.left_drawer);
抽屉打开时//设置自定义影子覆盖的主要内容
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START);
//设置抽屉的列表视图项,然后单击监听器
mDrawerList.setAdapter(新ArrayAdapter<字符串>(这一点,
R.layout.drawer_list_item,mPlanetTitles));
mDrawerList.setOnItemClickListener(新DrawerItemClickListener());
//使动作条的应用程序图标,表现为动作切换导航抽屉
getActionBar()setDisplayHomeAsUpEnabled(真)。
getActionBar()setHomeButtonEnabled(真)。
// ActionBarDrawerToggle联系在一起的适当交互
//滑动抽屉和操作栏中的应用程序图标之间
mDrawerToggle =新ActionBarDrawerToggle(
此时,/ *主机活动* /
mDrawerLayout,/ * DrawerLayout对象* /
R.drawable.ic_drawer,/ *导航抽屉图像,以取代上插入符* /
R.string.drawer_open,/ *开放抽屉说明访问* /
R.string.drawer_close / *关闭抽屉说明访问* /
){
公共无效onDrawerClosed(查看视图){
。getActionBar()的setTitle(mTitle);
// supportInvalidateOptionsMenu(); //创建调用上prepareOptionsMenu()
}
公共无效onDrawerOpened(查看drawerView){
。getActionBar()的setTitle(mDrawerTitle);
// supportInvalidateOptionsMenu(); //创建调用上prepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
如果(savedInstanceState == NULL){
选择信息(0);
}
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.main,菜单);
返回super.onCreateOptionsMenu(菜单);
}
/ *调用当我们调用invalidateOptionsMenu()* /
@覆盖
公共布尔prepareOptionsMenu(功能菜单)在{
//如果NAV抽屉被打开,隐藏操作项相关内容视图
布尔drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(drawerOpen!);
返回super.on prepareOptionsMenu(菜单);
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//操作栏家居/后续行动应打开或关闭抽屉。
// ActionBarDrawerToggle将照顾这。
如果(mDrawerToggle.onOptionsItemSelected(项目)){
返回true;
}
//手柄操作按钮
开关(item.getItemId()){
案例R.id.action_websearch:
//创建意图为这个星球上执行网络搜索
意向意图=新的意图(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,getActionBar()的getTitle());
//捕获事件,有没有活动处理的意图
如果(intent.resolveActivity(getPackageManager())!= NULL){
startActivity(意向);
} 其他 {
Toast.makeText(这一点,R.string.app_not_available,Toast.LENGTH_LONG).show();
}
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}
/ *的点击听者中的ListView控件中的抽屉式导航* /
私有类DrawerItemClickListener实现ListView.OnItemClickListener {
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
选择信息(位置);
}
}
私人无效选择信息(INT位置){
//更新替换片段的主要内容
片段片段=新PlanetFragment();
捆绑的args =新包();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER,位置);
fragment.setArguments(参数);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()代替(R.id.content_frame,片段).commit()。
//更新所选项目和标题,然后关上抽屉
mDrawerList.setItemChecked(位置,真正的);
的setTitle(mPlanetTitles [位置]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@覆盖
公共无效的setTitle(CharSequence的标题){
mTitle =称号;
。getActionBar()的setTitle(mTitle);
}
/ **
*当使用ActionBarDrawerToggle,你必须在调用它
* onPostCreate()和onConfigurationChanged()...
* /
@覆盖
保护无效onPostCreate(包savedInstanceState){
super.onPostCreate(savedInstanceState);
//同步发生onRestoreInstanceState后切换状态。
mDrawerToggle.syncState();
}
@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
super.onConfigurationChanged(NEWCONFIG);
//传递任何配置更改抽屉toggls
mDrawerToggle.onConfigurationChanged(NEWCONFIG);
}
/ **
*片段出现在content_frame,显示一个星球
* /
公共静态类PlanetFragment扩展片段{
公共静态最后弦乐ARG_PLANET_NUMBER =planet_number;
公共PlanetFragment(){
需要片段子类//空构造
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_planet,集装箱,假);
。INT I = getArguments()调用getInt(ARG_PLANET_NUMBER);
。字符串星球= getResources()getStringArray(R.array.planets_array)[我]
INT imageId = getResources()。则getIdentifier(planet.toLowerCase(Locale.getDefault()),
可拉伸,getActivity()getPackageName())。
((ImageView的)rootView.findViewById(R.id.image))setImageResource(imageId)。
getActivity()的setTitle(行星)。
返回rootView;
}
}
}
在setUpNavDrawer
mDrawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
此外
@覆盖
公共无效onBack pressed()
{
如果(mDrawerLayout.isOpen())
mDrawerLayout.close();
其他
super.onBack pressed();
}
I'm following the Creating a Navigation Drawer tutorial (Android Training).
I have downloaded the sample app and it work as intended. I open the drawer, press back and it gets closed.
The problem comes when I replace the lib/android-support-v4.jar
(originally with a size of 523 KB) with the updated version (revision 20) in my Android-SDK/extras/android/support/v4/android-support-v4.jar
(with size 741 KB). After the replacement if I open the drawer and then press the back button of the device the app gets closed instead of closing the drawer.
How can I solve that? Should I keep the old android-support-v4.jar version or should I programmatically intercept the back button closing the drawer when needed?
UPDATE: I've continue testing the problem, to do so I built the project in AndroidStudio. After copy the sample app (src, res and update the manifest) ran it: The back button closes the app even if the DrawerLayout is open. Then I modified the build.gradle file changing the line:
compile 'com.android.support:appcompat-v7:20.0.0'
with
compile 'com.android.support:appcompat-v7:19.0.0'
And the problem disapeared: The back button first closes the drawer and then closes the app
Full code for the activity (Link to the Android Training Source Code):
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
//supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
//supportInvalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_websearch:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
* Fragment that appears in the "content_frame", shows a planet
*/
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
}
In setUpNavDrawer
mDrawerLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
Also
@Override
public void onBackPressed()
{
if (mDrawerLayout.isOpen())
mDrawerLayout.close();
else
super.onBackPressed();
}
这篇关于抽屉布局不关闭背面pressed(取决于支持-V4 LIB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!