DrawerLayout打开前的事件

时间:2013-07-20 17:23:19

标签: android navigation-drawer

在我看来,DrawerLayout实施中缺少一件事:在抽屉打开之前触发 的事件。

drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
  public void onDrawerClosed(View view) {
    //...
  }

  public void onBeforeDrawerOpened(View drawerView) {
    //...
  }

  public void onDrawerOpened(View drawerView) {
    //...
  }
};

需要它的原因是设置抽屉项目的状态,如选择,启用和类似的菜单项。把它放到onDrawerOpened()回调有点可行,但在打开后更改菜单外观看起来不太合适。

我的第一个想法是扩展ActionBarDrawerToggle并提供新事件。我应该重新发明轮子:-)还是已经有人这样做了?

4 个答案:

答案 0 :(得分:5)

此链接可以帮助您解决问题。 How to detect that the DrawerLayout started opening?

  

使用onDrawerStateChanged(int newState)回调

     

你需要听STATE_SETTLING个州 - 这个状态是   每当抽屉开始移动(打开或关闭)时报告。所以   一旦你看到这种状态 - 检查抽屉是否现在打开并采取行动   相应地:

Function Bootstrap(S As Range, Z As Range, L As Double) As Double
Dim j As Integer
Dim a As Variant
Dim b As Variant
Dim n As Integer
Dim Q() As Double
Dim sum As Double
Dim P As Double
Dim dt As Double

n = Application.WorksheetFunction.Max(S.Columns.Count, Z.Columns.Count)
a = S.Value
b = Z.Value
dt = 1
sum = 0
ReDim Q(0 To n)
Q(0) = 0

For j = 1 To n - 1
    P = b(1, j) * (L * Q(j - 1)) - (L + dt * a(1, j) * Q(j - 1))
    sum = sum + P
    Q(j) = sum
Next j

Bootstrap = sum
End Function

答案 1 :(得分:2)

当我遇到这个问题并且在任何地方找不到确切的答案时,我在google上发现了这个,所以我决定发布完整的答案。希望它可以节省一些时间。

我找到的最佳解决方案是监控抽屉状态,并使用isDrawerOpen方法确定它是打开还是关闭。务必将重力设置为与打开/关闭抽屉时使用的重力相匹配。

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// ...
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_playlist, R.string.drawer_open, R.string.drawer_close) {
    public void onDrawerStateChanged(int newState) {
        if(newState == DrawerLayout.STATE_DRAGGING){
            if(mDrawerLayout.isDrawerOpen(Gravity.LEFT)){
                //closing

            }else{
                //opening

            }
        }
    }
}

答案 2 :(得分:0)

来自Lenny和Fred的混合人对Kotlin的回答:

@GetMapping("/user/home")
public ModelAndView getUser(
                        @RequestParam(name="userId", required = true) String userId,
                        @RequestParam(name="message", required = false) String message,
                        HttpSession session){
    ModelAndView mv = new ModelAndView();

    User user = (User)session.getAttribute("user");

    mv.addObject("message", message);
    mv.setViewName("user/home/view");
    return mv;
}

答案 3 :(得分:0)

已接受的答案中推荐的setDrawerListener方法现在为deprecated。完成此操作的新方法是使用addDrawerListener方法。

在科特林

  1. 初始化DrawerLayout视图

    val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)

  2. 在您的主要活动中,创建一个内部类,该内部类是DrawerLayout.DrawerListener的子类。 DrawerLayout类实现DrawerListener接口。您可以在onDrawerSlide中跟踪更改的抽屉位置,并确定抽屉是打开还是关闭:

    inner class CustomDrawer : DrawerLayout.DrawerListener{
      override fun onDrawerStateChanged(newState: Int) {
      }
    
    var last: Float = 0f
    
    override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
    
        var opening: Boolean = slideOffset>last
        var closing: Boolean = slideOffset<last
    
        if(opening) {
            Log.i("Drawer","opening")
            Log.d("Last",last.toString())
        } else if(closing) {
            Log.i("Drawer","closing")
            Log.d("Last",last.toString())
        } else {
            Log.i("Drawer","doing nothing")
            Log.d("Last",last.toString())
        }
    }
    
    override fun onDrawerClosed(drawerView: View) {
    
      }
    
    override fun onDrawerOpened(drawerView: View) {
    
      }
    }
    
  3. 将您的自定义DrawerListener添加到抽屉布局(我将其放在onCreate方法中)

    var drawerListener = CustomDrawer()
    drawerLayout.addDrawerListener(drawerListener)