是否可以创建一个可扩展的AlertDialog列表?

发布于 2024-11-19 03:20:57 字数 1439 浏览 2 评论 0原文

在我的应用程序中,用户可以使用不同的标准选择要下载的文章。其中之一是年份和月份。为此,我想要一个包含年份列表的 AlertDialog。如果用户单击年份,列表将展开并显示一月、二月等。

我知道如何使用 SimpleExpandableListAdapter 制作可扩展的列表视图,但这不是我想要的。由于其他标准(例如类别)也列出了 AlertDialogs,因此我想要外观和感觉相似的东西。

是否可以完成这样一个可扩展的AlertDialog列表?

解决方案

这是我根据CommonsWare的解决方案最终得到的结果:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select something");

ExpandableListView myList = new ExpandableListView(this);
MyExpandableListAdapter myAdapter = new MyExpandableListAdapter();
myList.setAdapter(myAdapter);

builder.setView(myList);
AlertDialog dialog = builder.create();
dialog.show();

剩下的唯一问题是:如何实现AlertDialog的onClick侦听器?通常我会在 setItems() 方法中执行此操作,但不使用 setItems。

我在 myList.setAdapter() 之后添加了 myList.setOnItemClickListener 但它被忽略。当我点击一个项目时没有任何反应:

myList.setOnItemClickListener(new ListView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int i, long l) {
        try {
            Toast.makeText(ExpandableList1.this, "You clicked me", Toast.LENGTH_LONG).show();
        }
        catch(Exception e) {
            System.out.println("something wrong here    ");
        }
    }
});

点击问题的解决方案:

解决方案非常简单。由于它是一个可扩展列表,因此列表本身会捕获项目单击以打开子元素。因此,事件处理程序永远不会被调用。

相反,您必须实现 OnChildClickListener(),顾名思义,它监听子级点击!

In my app the users are able to select articles to download using different criteria. One of them is year and month. For this I would like an AlertDialog with a list of years. If the user then clicks on a year, the list will expand and show january, february etc.

I know how to make an expandable listview using a SimpleExpandableListAdapter but that is not what I want. Since the other criteria (eg. category) are also list AlertDialogs, I want something that is similar in look and feel.

Is it possible to accomplish such an expandable list AlertDialog?

SOLUTION

This is what I ended up with based on CommonsWare's solution:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select something");

ExpandableListView myList = new ExpandableListView(this);
MyExpandableListAdapter myAdapter = new MyExpandableListAdapter();
myList.setAdapter(myAdapter);

builder.setView(myList);
AlertDialog dialog = builder.create();
dialog.show();

Only problem remaining: how do I implement the onClick listener for the AlertDialog? Normally I would do it in the setItems() method, but am not using setItems.

I added myList.setOnItemClickListener after myList.setAdapter() but it is ignored. Nothing happens when I click an item:

myList.setOnItemClickListener(new ListView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int i, long l) {
        try {
            Toast.makeText(ExpandableList1.this, "You clicked me", Toast.LENGTH_LONG).show();
        }
        catch(Exception e) {
            System.out.println("something wrong here    ");
        }
    }
});

Solution to click problem:

The solution was quite simple. Since it is an expandable list, item clicks are captured by the list itself to open the child elements. Thus, the event handler is never called.

Instead you have to implement OnChildClickListener() that - as the name suggests - listens to child clicks!

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

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

发布评论

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

评论(3

夏有森光若流苏 2024-11-26 03:20:57

AlertDialog.Builder 上使用 setView(),传入您在 Java 代码中扩充或创建并已设置适配器的 ExpandableListView

Use setView() on AlertDialog.Builder, passing in an ExpandableListView that you inflate or create in Java code and have set your adapter on.

书信已泛黄 2024-11-26 03:20:57

好吧,要使用列表视图,您必须扩展适当的列表活动,在您的情况下为 ExpandableListActivity。您将无法找到要扩展的 ExpandableListDialog。

我想您也许能够在调用对话框的活动中实现它,并将列表视图作为参考传递给对话框,然后手动将其添加到对话框中的布局中。我不确定这是否有效,但值得一试:D

Well, to use the listviews you have to extend the appropriate list-activity, in your case ExpandableListActivity. You wont be able to find a ExpandableListDialog to extend.

I suppose you might be able to implement it in the activity which calls the dialog, and pass on the listview to the dialog as a reference, and manually add it to your layout in the dialog. Im not sure if this will work, but its worth a shot :D

笑叹一世浮沉 2024-11-26 03:20:57

您甚至可以在该特定 Activity 的 Manifest 文件中创建 android:theme="Theme.Dialog"

you can even make android:theme="Theme.Dialog" in the Manifest file on that particular Activity.

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