Android - SearchView的延迟扩展

时间:2014-11-03 21:25:07

标签: android android-actionbar navigation-drawer searchview

我想延迟SearchView的扩展。我使用抽屉布局。如果在抽屉打开时单击搜索项,则我的视图布局会变形(灾难),所以我想在展开搜索视图之前先关闭抽屉。知道怎么做到这一点?

我使用以下代码。我试图拦截menuitem选择的事件,触发抽屉关闭。在DrawerListener中,调用serchMenu.expandActionView();

我希望searchView在抽屉完成关闭后展开,但这两个动作总是同时发生,而在过渡期间,我的视图布局也变得非常扭曲。这就是为什么我推迟了搜索事件的扩展。

public boolean onOptionsItemSelected(MenuItem item) {
    SearchView searchView = (SearchView)  mSearchItem.getActionView();
    if(item.getItemId() == R.id.search){
        mDarwerLayout.closeDrawer(mLeftDrawer);
        mSearchViewShouldExpand = true;

    }
    return true;
}



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawer_layout);
    .......


    mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDarwerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
            0,  
            0 
    ){  

        /** Called when a drawer has settled in a completely closed state. */
        @Override
        public void onDrawerClosed(View drawerView) {

            if(mSearchViewShouldExpand){
                mSearchItem.expandActionView();
                mSearchViewShouldExpand = false;
            }
        }

    };

    // Set the drawer toggle as the DrawerListener
    mDarwerLayout.setDrawerListener(mDrawerToggle);

}

1 个答案:

答案 0 :(得分:0)

我找到了拦截​​扩展事件的方法,它位于MenuItem.OnActionExpandListener中。只需要在这个方法中返回false

 @Override
public boolean onMenuItemActionExpand(MenuItem item) {

    Log.d("*******","onMenuItemActionExpand");

    mDarwerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

    if(mDarwerLayout.isDrawerOpen(mRightDrawer) || mDarwerLayout.isDrawerOpen(mLeftDrawer) ) {
        mDarwerLayout.closeDrawer(mRightDrawer);
        mDarwerLayout.closeDrawer(mLeftDrawer);
        mSearchViewShouldExpand = true;
        return false;
    }

    return true;


}