单击滑动抽屉处理程序时执行操作

发布于 2024-12-19 01:13:21 字数 184 浏览 4 评论 0原文

在我的应用程序中,我只想在数据不为空时填充滑动绘制。 因此,当用户按下滑动抽屉处理程序时,它应该给出一个对话框“数据为空”,并且滑动抽屉不应打开。

我已经尝试过 Handler.setOnTouchListener() 但发生的情况是在任何调用触摸事件且出现对话框的地方。

有什么建议吗?

In my application I want to fill the sliding draw only when data is not null.
So , when the user presses on the sliding drawer handler it should give a dialog "Data is empty" and sliding drawer should not open.

I have tried Handler.setOnTouchListener() but what happens is anywhere a touch event is called that dialog box appear.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-12-26 01:13:21

您应该看一下方法 setOnDrawerOpenListener

创建您自己的 OnDrawerOpenListener 并使用它检查数据是否为空 - 如果是,则再次关闭抽屉:

private class OnDrawerOpened implements OnDrawerOpenListener {
  @Override
  public void onDrawerOpened() {
    if( data == null )
      mySlidingDrawer.close();
  }
}

您需要对 SlidingDrawer 的引用,并且需要OnDrawerOpened 是一个内部类,因此您可以访问对 SlidingDrawer 的引用。

替代

您还可以通过扩展 SlidingDrawer 创建自己的子类,然后重写 animateOpen()open()方法(您可能也需要重写 animateToggle()toggle()):

public class MySlidingDrawer extends SlidingDrawer {

  public MySlidingDrawer( Context context, AttributeSet attrs ) {
    super( context, attrs );
  }

  @Override
  public void animateOpen() {
    if( data != null )    
      super.animateOpen();
    else
      //Show dialog
  }

  @Override
  public void open() {
    if( data != null )
      super.open();
    else
      //Show dialog
  }
}

如果您这样做,您将需要引用您自己的实现XML 中的 SlidingDrawer

<com.myPackage.MySlidingDrawer>
   ...
</com.myPackage.MySlidingDrawer>

根据获取数据以检查 null 的方式和时间,您可能需要将其传递给其构造函数中的 MySlidingDrawer 或通过添加一个方法您可以设置数据。

You should take a look at the method setOnDrawerOpenListener.

Create your own OnDrawerOpenListener and use it to check if the data is null - if it is, close the drawer again:

private class OnDrawerOpened implements OnDrawerOpenListener {
  @Override
  public void onDrawerOpened() {
    if( data == null )
      mySlidingDrawer.close();
  }
}

You would need a reference to your SlidingDrawer and you would need OnDrawerOpened to be an inner class, so you can access the reference to your SlidingDrawer.

Alternative

You could also create your own subclass by extending SlidingDrawer and then override the animateOpen() and open() methods (you might need to override animateToggle() and toggle() too):

public class MySlidingDrawer extends SlidingDrawer {

  public MySlidingDrawer( Context context, AttributeSet attrs ) {
    super( context, attrs );
  }

  @Override
  public void animateOpen() {
    if( data != null )    
      super.animateOpen();
    else
      //Show dialog
  }

  @Override
  public void open() {
    if( data != null )
      super.open();
    else
      //Show dialog
  }
}

If you do this, you would need to reference your own implementation of the SlidingDrawer in your XML:

<com.myPackage.MySlidingDrawer>
   ...
</com.myPackage.MySlidingDrawer>

Depeding on how and when you get the data to check for null, you might want to either pass it to MySlidingDrawer in its constructor or add a method through which you can set the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文