从OnClickListener在Android的ListView的充气行?OnClickListener、Android、ListView

2023-09-12 08:21:22 作者:争气总比生气好

我有一个类( A_Main.java )延长 ArrayAdapter 。我把我的的ListView 使用 A_Main ,因为它是 ListAdapter 。在 A_Main.getView()我膨胀的观点得到在的ListView 小部件的每一行。每一行包含一个的TextView 复选框的ImageButton 。当的ImageButton 点击,我玩的的TextView 相关的歌曲。我不想使用 onItemClickListener()的ListView ,因为它太容易失球了一个滚动和启动玩新的歌曲。

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 在一个新行,我需要取消HILITE的的ImageButton 的当前正在播放的歌曲,并HILITE新的。我想顺便做这将是夸大在视图中的ImageButton的 onClickListener()并在列表中未HILITE每一个按钮,然后,高亮度的其中之一正在播放。我不知道去了解这一点的最好办法。我可以保持一个成员列表中 A_Main 每个的ImageButton ID作为getView()遍历他们与直接引用ID从 onClickListener(),而不会造成内存泄漏?难道这些ID只要消失getView()与他们做了什么?替代方法有什么想法?

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

我希望这会帮助你。

I hope this will help you

试试这个

取自定义选择两种不同的状态图像选择和非选择

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,创建一个全局变量

1.Create a global variable

Imageview previous; 

在自定义适配器,并初始化它在构造函数中,你会得到的含量

in your Custom Adapter and Initialize it in the constructor where you'll get the content

previous=new ImageView(context);

在添加适配器getView()方法,你将可能有一个onclickListener为您的ImageView 像这样

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