是否可以创建一个可扩展的AlertDialog列表?
在我的应用程序中,用户可以使用不同的标准选择要下载的文章。其中之一是年份和月份。为此,我想要一个包含年份列表的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
AlertDialog.Builder
上使用setView()
,传入您在 Java 代码中扩充或创建并已设置适配器的ExpandableListView
。Use
setView()
onAlertDialog.Builder
, passing in anExpandableListView
that you inflate or create in Java code and have set your adapter on.好吧,要使用列表视图,您必须扩展适当的列表活动,在您的情况下为 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
您甚至可以在该特定 Activity 的 Manifest 文件中创建
android:theme="Theme.Dialog"
。you can even make
android:theme="Theme.Dialog"
in the Manifest file on that particular Activity.