我的片段中有一个方法(目的是在点击后将Listview图像更改为另一个图像)。我在我的适配器中调用此方法..现在,当我点击Imageview时没有任何反应..当我再次点击它时它工作..(这个Imageview是一个播放和暂停按钮,所以当我点击它回来把它变成播放icon我必须再次点击两次等等......)..
这是我片段中的方法:
@Override
public void imageButtonOnClick(View v, int position) {
playPause=(ImageView)v.findViewById(R.id.playPause);
Product m = productList.get(position);
playPause.setImageResource(m.getPlayPauseId());
playPause.setTag(position);
playPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Product m =(Product) productList.get(pos);
if (paused) {
m.setPlayPauseId(R.drawable.ic_pause);
paused=false;
}else {
m.setPlayPauseId(R.drawable.ic_play);
paused = true;
}
adapter.notifyDataSetChanged();
}
});
}
我在我的适配器中这样称呼它:
holder.playPause=(ImageView)v.findViewById(R.id.playPause);
holder.playPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (callback != null) {
callback.imageButtonOnClick(v, position);
}
}
});
这是我的接口和构造函数,它也在我的适配器中:
public interface PlayPauseClick {
void imageButtonOnClick(View v, int position);
}
private PlayPauseClick callback;
public void setPlayPauseClickListener(PlayPauseClick listener) {
this.callback = listener;
}
我在片段
中实现了这个界面更新
产品类别:
public class Product implements Serializable {
@SerializedName("pid")
public int pid;
@SerializedName("name")
public String name;
@SerializedName("qty")
public int qty;
@SerializedName("price")
public String description;
@SerializedName("image_url")
public String image_url;
@SerializedName("date")
public String date;
private int PlayPauseId;
public int getPlayPauseId() {
return PlayPauseId;
}
public void setPlayPauseId(int playPauseId) {
PlayPauseId = playPauseId;
}
答案 0 :(得分:3)
我认为您不需要发送回Fragment的电话来更改播放/暂停图像然后通知。在适配器中简单地更改它就像
一样holder.playPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Product product =(Product) productList.get(pos);
if (product.getPlayPauseId() == R.drawable.ic_play) {
product.setPlayPauseId(R.drawable.ic_pause);
}else {
product.setPlayPauseId(R.drawable.ic_play);
}
v.setImageResource(product.getPlayPauseId());
}
});
<强>更新强>
在您的情况下,您的所有逻辑代码都在Fragment
而不是Adapter
,所以我认为您可以这样做
适配器
Product product = productList.get(position);
holder.playPause=(ImageView)v.findViewById(R.id.playPause);
holder.playPause.setImageResource(product.getPlayPauseId());
holder.playPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (callback != null) {
// you need to pass the position, don't need pass the View
callback.imageButtonOnClick(position);
}
}
});
片段代码
public void imageButtonOnClick(int position) {
Product product = productList.get(position);
if(your logic){
product.setPlayPauseId(R.drawable.ic_play);
}else{
product.setPlayPauseId(R.drawable.ic_pause);
}
adapter.notifyDataSetChanged();
}