是否有可能创建一个扩展列表AlertDialog?有可能、创建一个、列表、AlertDialog

2023-09-05 06:17:00 作者:林有味.

在我的应用程序的用户能够选择的文章使用不同的标准进行下载。其中之一是年份和月份。为此,我愿与多年列表的AlertDialog。如果用户再上一年的点击,名单将扩大并显示一月,二月等。

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.

我知道如何使一个可扩展的列表视图的使用SimpleExpandableListAdapter但是这不是我想要的。由于其他条件(如类)也列出AlertDialogs,我想要的东西,是类似于和感觉。

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.

是否有可能实现这种可扩展列表AlertDialog?

Is it possible to accomplish such an expandable list AlertDialog?

解决方案:

这是我结束了基于CommonsWare的解决方案:

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();

唯一的问题剩余:我怎么实现的onClick侦听AlertDialog?通常我会做的setItems()方法,但我没有使用setItems。

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

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    ");
        }
    }
});

解决方案点击问题:

解决方案是非常简单的。因为它是一个可扩展的列表中,项的点击由列表本身捕获,打开的子元素。因此,该事件处理程序没有被调用。

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.

相反,你必须实现OnChildClickListener()是 - 顾名思义 - 倾听孩子的点击

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

推荐答案

使用的setView() AlertDialog.Builder ,传递一个 ExpandableListView 你夸大或Java创建code,并在设置您的适配器。

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