BottomSheetDialog/BottomSheetDialogFragment-使用哪个以及如何使用? [英] BottomSheetDialog/BottomSheetDialogFragment — which to use and how?

查看:292
本文介绍了BottomSheetDialog/BottomSheetDialogFragment-使用哪个以及如何使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Material Design应用.我要实现的功能是某种民意测验.当用户单击列表的元素时,将显示持久性底部工作表对话框,如下所示:

I'm working on a Material design app. One feature I want to implement is some kind of a poll. When a user clicks an element of a list, the persistent bottom sheet dialog, which looks like this should show up:

然后,当用户单击任何按钮时,该对话框应消失,并显示模式底部的对话框,为用户提供有关在开始时单击的列表项的更多信息.看起来像这样:

Then, when user clicks any button, this dialog should go away and the modal bottom sheet dialog should show up, providing a user with more information about the list item which was clicked at the beginning. It looks like this:

即使阅读了有关AppCompat对话框的某些信息,我也找不到有关BottomSheetDialog和BottomSheetDialogFragment以及如何正确使用它们的任何清晰说明.所以,我的问题是:

I can't find any clear explanations about BottomSheetDialog and BottomSheetDialogFragment, and how to use them correctly, even after reading some information about AppCompat dialogs. So, my questions are:

  1. 它们有什么不同之处,我应该分别使用哪种 情况吗?
  2. 如何在活动中获取有关在对话框中按下哪个按钮的数据?
  3. 是否有实现代码或有关使用它们的教程的链接?
  1. In what way are they different and which one should I use for each case?
  2. How to get data in the activity about which button was pressed in the dialog?
  3. Any links to the code of implementations or tutorials about using them?

推荐答案

最后,我找到了解决方案,它可以工作.告诉我我做错了什么.它基本上像本指南中的DialogFragment一样工作,但是我已经做了一些不同的事情.

Finally, I've found the solution and it works. Tell me if I'm doing something wrong. It basically works like DialogFragment from this guide, but I've done it a bit different.

1)它们的区别与DialogFragment和Dialog相同,并且都是模态的.如果您需要持久对话框,请使用 BottomSheetBehaviour (我发现这两个对话框必须在我的应用中是模态的.)

1) Their difference is the same as it of DialogFragment and Dialog, and they both are modal. If you need persistent dialog, use BottomSheetBehaviour instead (I found out that both dialogs had to be modal in my app).

2)我必须先用一些代码回答第三个问题,然后再很容易回答第二个问题.

2) I have to answer the third question with some code first, and then it will be easy to answer the second one.

3)创建一个新的public class,即extends BottomSheetDialogFragment,我将其称为FragmentRandomEventPoll.这里有两件事必须实现.

3) Create a new public class, which extends BottomSheetDialogFragment, I called it FragmentRandomEventPoll. There are two two things which have to be implemented here.

  • 覆盖方法onCreateView.它与Activity中的onCreate方法几乎相同,除了它返回应充气的View:

  • Override method onCreateView. It is nearly the same as onCreate method in Activities, except for that it returns the View it should inflate:

// We will need it later
private static String headerItem;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_random_event_poll, container, false);
    header = (TextView) v.findViewById(R.id.uRnd_fragment_bottom_sheet_poll_header);
    skip = (Button) v.findViewById(R.id.uRnd_fragment_bottom_sheet_button_skip);
    header.setText(...);

    // I implemented View.OnClickListener interface in my class
    skip.setOnClickListener(this);
    return v;
}

  • 静态方法,您可以将必需的数据传递到该方法并获取此类的新实例(可能我可以使用常规的构造函数,因此我将不得不对其进行更多试验). URandomEventListItem是数据模型类.

  • Static method which you can pass necessary data to and get new instance of this class (Probably I could have just used a regular constructor, I'll have to experiment with it a bit more). URandomEventListItem is the data model class.

    public static FragmentRandomEventPoll newInstance(URandomEventListItem item) {
        FragmentRandomEventPoll fragment = new FragmentRandomEventPoll();
        headerItem = item.getHeader();
        return fragment;
    } 
    

  • 2)要获取活动或任何其他地方的输入事件,请定义具有必要方法的接口并为其实例创建setter方法:

    2) To get input events in activity or any other place, define an interface with necessary methods and create setter method for it's instance:

    private PollButtonClickListener listener;
    
    public void setListener(PollButtonClickListener listener) {
        this.listener = listener;
    }
    
    public interface PollButtonClickListener {
        void onAnyButtonClick(Object data)
    }
    

    在要获取数据的位置(在布局中指定了"dialog_event_poll"标签):

    And in the place you want to get your data ("dialog_event_poll" tag was specified in the layout):

    FragmentRandomEventPoll poll = FragmentRandomEventPoll.newInstance(events.get(id));
    poll.setListener(new FragmentRandomEventPoll.PollButtonClickListener() {
            @Override
            public void onAnyButtonClick(Object data) {
                // Do what you want with your data
            }
        });
        poll.show(getSupportFragmentManager(), "dialog_event_poll");
    }
    

    如果有任何不清楚的地方,可以在上找到我的项目文件. Github .

    If there is anything unclear, my project files could be found on Github.

    这篇关于BottomSheetDialog/BottomSheetDialogFragment-使用哪个以及如何使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆