有没有办法在活动中的Imageview(Listview)上设置onClickListener? 我创建了这个,它在我的适配器中运行得很好但是它在我的活动中不起作用:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
playPause=(ImageView)view.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 =productList.get(pos);
if (paused) {
m.setPlayPauseId(R.drawable.ic_pause);
paused=false;
}else {
m.setPlayPauseId(R.drawable.ic_play);
paused = true;
}
adapter.notifyDataSetChanged();
}
});
}
这是我的产品类:
public class Product implements Serializable {
private int PlayPauseId;
@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;
public int getPlayPauseId() {
return PlayPauseId;
}
public void setPlayPauseId(int playPauseId) {
PlayPauseId = playPauseId;
}
}
适配器类(Fundapter库):
public class FunDapter<T> extends BaseAdapter implements Filterable {
protected List<T> mDataItems;
protected List<T> mOrigDataItems;
protected LongExtractor<T> idExtractor;
protected final Context mContext;
private final int mLayoutResource;
private final BindDictionary<T> mBindDictionary;
private int oddColorRes;
private int evenColorRes;
private FunDapterFilter<T> funDapterFilter;
private Filter mFilter;
public FunDapter(Context context, List<T> dataItems, int layoutResource,
BindDictionary<T> dictionary) {
this(context, dataItems, layoutResource, null, dictionary);
}
public FunDapter(Context context, List<T> dataItems, int layoutResource,
LongExtractor<T> idExtractor, BindDictionary<T> dictionary) {
this.mContext = context;
this.mDataItems = dataItems;
this.mOrigDataItems = dataItems;
this.mLayoutResource = layoutResource;
this.idExtractor = idExtractor;
this.mBindDictionary = dictionary;
}
public void updateData(List<T> dataItems) {
this.mDataItems = dataItems;
this.mOrigDataItems = dataItems;
notifyDataSetChanged();
}
@Override
public int getCount() {
if (mDataItems == null || mBindDictionary == null) return 0;
return mDataItems.size();
}
@Override
public T getItem(int position) {
return mDataItems.get(position);
}
@Override
public boolean hasStableIds() {
if(idExtractor == null) return super.hasStableIds();
else return true;
}
@Override
public long getItemId(int position) {
if(idExtractor == null) return position;
else return idExtractor.getLongValue(getItem(position), position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final GenericViewHolder holder;
if (null == v) {
LayoutInflater vi =
(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(mLayoutResource, null);
holder = new GenericViewHolder();
holder.root = v;
FunDapterUtils.initViews(v, holder, mBindDictionary);
v.setTag(holder);
} else {
holder = (GenericViewHolder) v.getTag();
}
final T item = getItem(position);
showData(item, holder, position);
return v;
}
答案 0 :(得分:0)
答案 1 :(得分:0)
你可以用两种方式做到: 创建一个像这样的界面
public interface ImageClick {
public imageButtonOnClick(View view, int position)
}
在您的活动中实施它,并在适配器getView
中设置imageOnClickListener
,并在onClick
内调用此接口方法,传递您的位置并在其中查看,这样您就可以在你的活动回调中使用位置等。
其次是在你的活动中制作一个方法,就像在界面
中一样public imageButtonOnClick(View view, int position)
在imageView上的适配器onClick方法中,将其称为
((YourActivity)context).imageButtonOnClick(params1, param2);
答案 2 :(得分:0)
要在活动本身的列表视图中单击图像,您必须回调
创建界面
public interface OnButtonClickListener {
public void onButtonClick(int position,View view);
}
在您的actvity类中实现 OnButtonClickListener
你得到一个接口物品
@Override
public void onButtonClick(final int position, View view) {
int id = view.getId();
switch (id) {
case R.id.YOURIMAGEVIEWID:
//implent your process...
}
}
在适配器calss中创建构造函数
Context context;
OnButtonClickListener callback
public AdapterClass(Context context OnButtonClickListener buttonClickListener) {
this.context = context;
callback = buttonClickListener;
}
图片点击适配器类
holder.imageView..setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
callback.onButtonClick(position, v);
}
});
在你的动作班
AdapterClass Adapter = new AdapterClass(youractivity.this, youractivity.this);
为回调添加此类
public class ListCustomClickEvents implements OnClickListener,View.OnTouchListener {
private int position;
private OnCustomClickListener callback;
// Pass in the callback (this'll be the activity) and the row position
public ListCustomClickEvents(OnCustomClickListener callback, int pos) {
position = pos;
this.callback = callback;
}
@Override
public void onClick(View v) {
callback.OnCustomClick(v, position);
}
}