小吃吧的支持库不包括OnDismissListener()?不包括、吃吧、OnDismissListener

2023-09-04 04:49:04 作者:久戦帝王

我想实现包含在最新的设计支持图书馆新小吃吧,但它提供的方式似乎违反直觉的我,而我认为许多人,使用。

I'd like to implement the new Snackbar included in the latest Design Support Library, but the way it is offered seems counter-intuitive for my, and I assume many others', use.

当用户执行的重要举措,我希望让他们通过小吃吧撤销了,但似乎没有办法检测时,它被驳回做的动作。这是有道理的我做了以下方式:

When the user does an important action, I want to allow them to undo it via the Snackbar, but there seems to be no way to detect when it is dismissed to do the action. It makes sense to me to do it the following way:

在用户执行操作。 显示小吃吧和更新的UI好象动作已经完成(例如,它似乎数据发送到数据库中,但实际上还没有)。 如果用户pressed撤销,还原了用户界面的变化。如果不是这样,当小吃吧被驳回,它就会发送数据。

但是,因为我看不到任何accessable OnDismissListener,我会因此有:

But because I don't see any accessable OnDismissListener, I would therefore have to:

在用户执行操作。 立即发送信息到数据库和更新用户界面。 如果用户presses撤销,再派调用数据库删除刚刚添加的数据,然后恢复用户界面的变化。

我真的想避免使两者调用数据库,并只发送一个应用程序时知道它是安全的(用户避免了pressing撤消)。我注意到有一个第三方库通过事件监听一些实现这一点,但我真的很想坚持到谷歌库。

I would really like to avoid having to make the two calls to the database, and just send one when the app knows that it's safe (the user has avoided pressing "undo"). I notice there is some implementation of this in a third-party library via an EventListener, but I'd really like to stick to the Google library.

推荐答案

现在它does

Snackbar.make(getView(), "Hi there!", Snackbar.LENGTH_LONG).setCallback( new Snackbar.Callback() {
                @Override
                public void onDismissed(Snackbar snackbar, int event) {
                    switch(event) {
                        case Snackbar.Callback.DISMISS_EVENT_ACTION:
                            Toast.makeText(getActivity(), "Clicked the action", Toast.LENGTH_LONG).show();
                            break;
                        case Snackbar.Callback.DISMISS_EVENT_TIMEOUT:
                            Toast.makeText(getActivity(), "Time out", Toast.LENGTH_LONG).show();
                            break;
                    }
                }

                @Override
                public void onShown(Snackbar snackbar) {
                    Toast.makeText(getActivity(), "This is my annoying step-brother", Toast.LENGTH_LONG).show();
                }
            }).setAction("Go away!", new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            }).show();