导航抽屉没有关闭

导航抽屉没有关闭

本文介绍了导航抽屉没有关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导航抽屉里我的应用程序没有关闭。我使用的活动,而不是片段。当我点击了列表视图的任何项目,它会打开其他活动,因为它应该,但是当我回去的,出票人仍处于打开状态。我曾试过用 DrawerLayout.closeDrawers(); ,但没有奏效。如何关闭导航抽屉?
这是我的code:

的Java

 公共类MainActivity扩展FragmentActivity {
最终的String []数据= {铝,金卡,锌};

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_1,数据);

    最后DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout);
    最终的ListView navList =(ListView控件)findViewById(R.id.left_drawer);
    navList.setAdapter(适配器);
    navList.setOnItemClickListener(新AdapterView.OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>母公司视图中查看,最终诠释POS,长I​​D){

            开关(POS){
                情况下0:
                    意图I =新的意图(MainActivity.this,Aluminium.class);
                    startActivity(ⅰ);
                    打破;
                情况1:
                    意图I2 =新的意图(MainActivity.this,Gold.class);
                    startActivity(I 2);
                    打破;
                案例2:
                    意图I3 =新的意图(MainActivity.this,Zinc.class);
                    startActivity(I3);
                    打破;
            }
        }
    });
}
}
 

XML

 < android.support.v4.widget.DrawerLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / drawer_layout
机器人:方向=横向
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
<! - 其主要内容视图 - >
<的FrameLayout
    机器人:ID =@ + ID / content_frame
    机器人:layout_width =match_parent
    机器人:后台=#000000
    机器人:layout_height =match_parent>
< /的FrameLayout>

< ListView的机器人:ID =@ + ID / left_drawer
    机器人:layout_width =220DP
    机器人:layout_height =match_parent
    机器人:layout_gravity =开始
    机器人:choiceMode =singleChoice
    机器人:分隔=@机器人:彩色/透明
    机器人:dividerHeight =1DP
    机器人:后台=#000000/>
< /android.support.v4.widget.DrawerLayout>
 

解决方案

在延续别人的答案和@的Chinmay Dabke问题,但抽屉关闭一半,然后停顿了一下,然后完全关闭的意见之一,这里是什么你可以这样做:

先为别人建议,这条线缺失。 drawer.closeDrawer(navList);

和尽可能抽屉的暂停而关闭来讲,你可以做这样的事情。用一个Runnable和这样的处理程序:

  mRunnable = =新的Runnable(){
    @覆盖
    公共无效的run(){
         //说
         选择信息(POS); //实现您的交换情况下的逻辑在这个FUNC
    }
}
 

然后在 onDrawerClosed overrided方法

  @覆盖
公共无效onDrawerClosed(查看视图){

如果(mRunnable!= NULL){
    mHandler.post(mRunnable);
    mRunnable = NULL;
}
}
 

希望这有助于!

我会建议你使用的片段导航抽屉,解决这个问题的抽屉没有正常关闭的,我发现这篇文章非常有用的(使用的片段)。 http://www.michenux.net/android-asynctask -in片段的最佳pratices-725.html

The navigation drawer in my app is not closing. I am using activities instead of fragments. When i click on any item in the listview, it opens other activities as it should but when i go back, the drawer is still open. I have tried using DrawerLayout.closeDrawers(); but it did not work. How do I close the navigation drawer?
Here is my code:

Java

public class MainActivity extends FragmentActivity {
final String[] data ={"Aluminium","Gold","Zinc"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    final ListView navList = (ListView) findViewById(R.id.left_drawer);
    navList.setAdapter(adapter);
    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){

            switch (pos){
                case 0:
                    Intent i = new Intent(MainActivity.this,Aluminium.class);
                    startActivity(i);
                    break;
                case 1:
                    Intent i2 = new Intent(MainActivity.this,Gold.class);
                    startActivity(i2);
                    break;
                case 2:
                    Intent i3 = new Intent(MainActivity.this,Zinc.class);
                    startActivity(i3);
                    break;
            }
        }
    });
}
}

XML

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent" >
</FrameLayout>

<ListView android:id="@+id/left_drawer"
    android:layout_width="220dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="1dp"
    android:background="#000000"/>
</android.support.v4.widget.DrawerLayout>
解决方案

In continuation to others answers and @ Chinmay Dabke question of 'but the drawer closes half then pauses and then closes fully' in one of the comments, here is what you could do:

first as others suggested,this line is missing. drawer.closeDrawer(navList);

And as far as the pausing of drawer is concerned while closing, you could do something like this.use a Runnable and a Handler like this:

mRunnable = = new Runnable() {
    @Override
    public void run() {
         //say
         selectItem(pos); //Implement your switch case logic in this func
    }
}

and then in the onDrawerClosed overrided method

@Override
public void onDrawerClosed(View view) {

if (mRunnable != null) {
    mHandler.post(mRunnable);
    mRunnable = null;
}
}

Hope this helps!

I would suggest you to use fragments for navigation drawer and to solve this issue of drawer not closing properly, I found this article very useful (using fragments). http://www.michenux.net/android-asynctask-in-fragment-best-pratices-725.html

这篇关于导航抽屉没有关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 17:59