从 Android 中的 OnClickListener 膨胀 ListView 行? [英] Inflate ListView row from OnClickListener in Android?
问题描述
我有一个扩展 ArrayAdapter
的类 (A_Main.java
).我将 ListView
设置为使用 A_Main
,因为它是 ListAdapter
.在 A_Main.getView()
内部,我对视图进行了膨胀以获取每一行的 ListView
小部件.每行包含一个 TextView
、CheckBox
和一个 ImageButton
.单击 ImageButton
时,我播放与 TextView
关联的歌曲.我不想在 ListView
上使用 onItemClickListener()
因为它太容易摸索滚动并开始播放一首新歌.
I have a class (A_Main.java
) extending ArrayAdapter
. I set my ListView
to use A_Main
as it's ListAdapter
. Inside A_Main.getView()
I inflate the view to get at the ListView
widgets for each row. Each row contains a TextView
, CheckBox
and an ImageButton
. When the ImageButton
is clicked, I play the song associated with the TextView
. I don't want to use onItemClickListener()
on the ListView
as it's too easy to fumble up a scroll and start playing a new song.
当我单击新行中的 ImageButton
时,我需要取消隐藏当前播放歌曲的 ImageButton
,并将新的隐藏.我正在考虑这样做的方法是在 ImageButton 的 onClickListener()
中放大视图,并取消隐藏列表中的每个按钮,然后,将正在播放的按钮设为 hi-lite.我不确定解决这个问题的最佳方法.我可以在每个 ImageButton
ID 的 A_Main
中保留一个成员列表,因为 getView() 迭代它们并直接从 onClickListener()
引用 ID 而无需导致内存泄漏?getView()
处理完这些 ID 后,它们会消失吗?对替代方法有什么想法吗?
When I click an ImageButton
in a new row, I need to un-hilite the ImageButton
of the currently playing song, and hilite the new one. I'm thinking the way to do that would be to inflate the view in the ImageButton's onClickListener()
and un-hilite every button in the List, then, hi-lite the one which is playing. I'm not sure the best way to go about this. Can I keep a member list in A_Main
of each ImageButton
ID as getView() iterates over them and reference the ID directly from onClickListener()
without causing memory leaks? Do those IDs disappear as soon as getView()
is done with them? Any thoughts on alternative approaches?
推荐答案
解决方案可能很简单 像这样全局获取布尔数组
Solution is probably simple Take boolean array globally like this
private final boolean[] selectedstates;
并在构造函数中使用列表的大小对其进行初始化
And initialize it with the size of your list in your constructor
selectedstates= new boolean[yourlist.size()];
然后在 onclick 侦听器的外侧设置成这样
And in out side of onclick listener set like this
yourbutton.setSelected(selectedstates[position]);
希望对你有帮助
试试这个
采用带有两个不同状态图像的自定义选择器进行选择和非选择
Take a custom selector with two different state images for selection and non selection
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pause_button"
android:state_selected="true" />
<item android:drawable="@drawable/play_button" />
</selector>
1.创建一个全局变量
Imageview previous;
在您的自定义适配器中并在您将获得内容的构造函数中初始化它
in your Custom Adapter and Initialize it in the constructor where you'll get the content
previous=new ImageView(context);
在您的适配器中添加 getView() 方法,您可能会有一个用于 Imageview 的 onclickListener这样做
Add in your adapter getView() method you will probably have a onclickListener for your Imageview do like this
imagPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView current=((ImageView)v);
current.setSelected(true);
previous.setSelected(false);
previous=current;
}
});
这会奏效,我很有信心,因为我已经在我的应用中使用过它.希望对你有帮助
This will work, I was confident because I have used it in my app. I hope this will help you
这篇关于从 Android 中的 OnClickListener 膨胀 ListView 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!