android-navigation 抽屉在活动更改时未打开

Posted

技术标签:

【中文标题】android-navigation 抽屉在活动更改时未打开【英文标题】:android-navigation drawer not open on activity change 【发布时间】:2014-11-17 05:57:10 【问题描述】:

这很奇怪,我花了最后 5 个小时在上面,没有任何效果。问题是这样的,当我从活动 a 转到活动 b,然后按返回按钮并返回活动 a 时,导航抽屉没有打开。 这是代码:

public abstract class DrawerActivity extends ActionBarActivity 
static LinearLayout fullLayout;
static FrameLayout actContent;
static DrawerLayout mDrawerLayout;
static ListView mDrawerList;
static ActionBarDrawerToggle mDrawerToggle;
static LinearLayout mDrawer;
List<HashMap<String, String>> mList;
SimpleAdapter mAdapter;
final private String COUNTRY = "country";
final private String FLAG = "flag";
final private String COUNT = "count";
@Override
public void setContentView(final int layoutResID) 
    fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.act_layout, null); // Your base layout here
    actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);
    getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame
    super.setContentView(fullLayout);
        makelist();
    
        mTitle = (String) getTitle();

        // Getting a reference to the drawer listview
        mDrawerList = (ListView) findViewById(R.id.drawer_list);

        // Getting a reference to the sidebar drawer ( Title + ListView )
        mDrawer = (LinearLayout) findViewById(R.id.drawer);

        // Each row in the list stores country name, count and flag
        mList = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < mCountries.length; i++) 
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put(COUNTRY, mCountries[i]);
            hm.put(FLAG, Integer.toString(mFlags[i]));
            mList.add(hm);
        
        String[] from =  FLAG, COUNTRY ;
        int[] to =  R.id.flag, R.id.country ;
        mAdapter = new SimpleAdapter(this, mList, R.layout.drawer_layout, from,to);

        // Getting reference to DrawerLayout
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_navigation_drawer, R.string.drawer_open,R.string.drawer_close) 

            /** Called when drawer is closed */
            public void onDrawerClosed(View view) 
                highlightSelectedCountry();
                supportInvalidateOptionsMenu();
            

            /** Called when a drawer is opened */
            public void onDrawerOpened(View drawerView) 
                supportInvalidateOptionsMenu();
            

            @Override
            public boolean onOptionsItemSelected(MenuItem item) 
                if (item != null && item.getItemId() == android.R.id.home) 
                    if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) 
                        mDrawerLayout.closeDrawer(Gravity.RIGHT);
                        Log.v("this","close");
                     else 
                        mDrawerLayout.openDrawer(Gravity.RIGHT);
                    
                
                return false;
            


        ;

        // Setting event listener for the drawer
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerList.setAdapter(mAdapter);
    
    public static boolean open() 
        if (mDrawerLayout.isDrawerOpen(mDrawer)) 
             mDrawerLayout.closeDrawer(Gravity.RIGHT);
             return true;
          else 
             mDrawerLayout.openDrawer(Gravity.RIGHT);
             return true;
         
    

这是 MainActivity 类:

public class MainActivity extends DrawerActivity implements OnClickListener  

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

    //suppose this is the imageview on the actionbar
        ImageView img=(ImageView)dialogg.findViewById(R.id.img);
        dateup.img(new OnClickListener() 
            @Override
            public void onClick(View v) 
                  DrawerActivity.open();                
            
        );

我要注意一件事,mainActivity 扩展了 DrawerActivity 类。

你能帮我解决这个问题吗?

谢谢你

【问题讨论】:

尝试在onRestart() of ActivityA 中打开抽屉 它可能有效,但我不希望每次有人来活动时都打开它 我试过你说的,当我回到活动a时,它会立即打开和关闭。我删除了 mainactivity 中的所有代码,但同样的情况发生了 将你的 onOptionsItemSelected 方法从 ActionBarDrawerToggle 移到活动类中 您不应该对 UI 组件使用静态属性。尝试使它们成为标准实例属性。 【参考方案1】:

这个

尝试使用 Abstract 类中的方法 open() 并且您使用“mDrawerLayout”,我认为此时它必须为 null

public void onClick(View v) 
     DrawerActivity.open();                

也许你会使用

public void onClick(View v) 
     this.open();                

然后你将使用你当前Activity的open-Method

而且你必须在Activity中实现onClickListener

错误消息会很高兴获得有关错误的更多信息

【讨论】:

感谢回复,我调用 DrawerActivity.open();在 mainAcitivty 上,我确定不是 this.open();没有错误,就是打不开菜单

以上是关于android-navigation 抽屉在活动更改时未打开的主要内容,如果未能解决你的问题,请参考以下文章

导航抽屉切换图标

所有活动中的Android导航抽屉[重复]

在导航抽屉上调用不是片段活动的活动

多个活动的导航抽屉,无需选择导航抽屉活动

想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉

如何在导航抽屉中添加活动