我正在尝试编写一个应用程序来更改图像资源,当按下该imageView时。
我提到this link。
我在代码中使用了setOnTouchListener
和setOnClickListener
。在两者中,我都会得到日志。 BUt,图像没有变化。我在下面给出了我的代码:
ImageView more;
more = (ImageView) row.findViewById(R.id.imageView1);
more.setImageResource(R.drawable.ic_more_1);
setOnTouchListener
是:
more.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d(TAG,"Event type "+event+" "+ event.getAction());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
more.setImageResource(R.drawable.ic_more_2);
break;
case MotionEvent.ACTION_UP:
more.setImageResource(R.drawable.ic_more_1);
break;
}
return false;
}
});
我的setOnClickListener
是:
more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!buttonClicked) {
Log.d(TAG, "on image click..... false" + buttonClicked);
buttonClicked = true;
more.setImageResource(R.drawable.ic_more_2);
}
if (buttonClicked) {
Log.d(TAG, "on image click..... true" + buttonClicked);
buttonClicked = false;
more.setImageResource(R.drawable.ic_more_1);
}
// more.setImageResource(R.drawable.ic_more_2);
// TODO Auto-generated method stub
/* Log.d(TAG, "on image click..... " + pos);
* if(listData.getPosition() == SUBSCRIBE) {
* more.setImageResource(R.drawable.ic_more_2);
* //more.setImageResource(R.drawable.ic_more_1); Log.d(TAG,
* "For Subscribe..................."); intent = new
* Intent(getContext(), EpisodeActivity.class);
* intent.putExtra("URL", strurl);
* getContext().startActivity(intent);
*
* } else { // TODO REWRITE IT FOR ADDING AS A FAVIOURITE ITEMS
* Log.d(TAG, "For Episode..................."); }
*/
}
});
请指出我做了什么错误。
提前谢谢!!!
注意:由于我希望listView
ListItem images
对Custom Adapter
进行此更改,因此我在public class CustomAdapterForSubcribe extends ArrayAdapter<ListData> {
public final String TAG = "CustomAdapterForSubcribe";
Context context;
int layoutResourceId;
boolean buttonClicked = false;
LayoutInflater inflater;
TextView title, desc;
ImageView more, fav, imageRightSubscribe, imageRightEpisode;
Intent intent;
public static int SUBSCRIBE = 1;
public static int EPISODE = 0;
private String strurl = "http://timesofindia.feedsportal.com/c/33039/f/533916/index.rss";
private String imageUrlEpisode = "http://news.bbcimg.co.uk/media/images/66996000/jpg/_66996549_66996528.jpg";
public CustomAdapterForSubcribe(Context context, int layoutResourceId,
List<ListData> objects, int option) {
// TODO Auto-generated constructor stub
super(context, layoutResourceId, objects);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int pos = position;
View row = convertView;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(R.layout.episode_row_view, parent, false);
}
final ListData listData = getItem(position);
title = (TextView) row.findViewById(R.id.title);
desc = (TextView) row.findViewById(R.id.description);
more = (ImageView) row.findViewById(R.id.imageView1);
if (listData.getPosition() == SUBSCRIBE) {
more.setImageResource(R.drawable.ic_more_1);
} else {
more.setImageResource(R.drawable.ic_favourite_1);
}
// more.setImageResource(R.drawable.ic_more_1);
imageRightEpisode = (ImageView) row.findViewById(R.id.image);
imageRightEpisode.setTag(imageUrlEpisode);
new DownloadImagesTask().execute(imageRightEpisode);
more.setTag(new Integer(position));
if (listData.getPosition() == SUBSCRIBE) {
Log.d(TAG, "<<<<.....In option.....>>>>");
desc.setVisibility(View.GONE);
// title.setGravity(Gravity.CENTER_HORIZONTAL);
// title.setGravity(Gravity.CENTER_VERTICAL);
title.setGravity(Gravity.CENTER);
title.setTextSize(16);
title.setTypeface(Typeface.DEFAULT_BOLD);
}
title.setText(listData.getTitle());
desc.setText(listData.getDesc());
// more.setOnTouchListener(new View.OnTouchListener() {
//
// @Override
// public boolean onTouch(View v, MotionEvent event) {
// // TODO Auto-generated method stub
// Log.d(TAG, "Event type " + event + " " + event.getAction());
// if (listData.getPosition() == SUBSCRIBE) {
// switch (event.getAction()) {
//
// case MotionEvent.ACTION_DOWN:
// Log.d(TAG, "SUBSCRIBE...Image press releasing....");
// more.setImageResource(R.drawable.ic_more_1);
// more.invalidate();
// break;
// case MotionEvent.ACTION_UP:
// Log.d(TAG, "SUBSCRIBE... Image pressing....");
// more.setImageResource(R.drawable.ic_more_2);
// break;
// }
// }
//
// return false;
// }
// });
more.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listData.getPosition() == EPISODE) {
if (!buttonClicked) {
Log.d(TAG, "on image click..... false" + pos);
more.setImageResource(R.drawable.ic_favourite_2);
buttonClicked = true;
}
else if (buttonClicked) {
Log.d(TAG, "on image click..... " + buttonClicked);
more.setImageResource(R.drawable.ic_favourite_1);
buttonClicked = false;
}
}
// more.setImageResource(R.drawable.ic_more_2);
// TODO Auto-generated method stub
else if (listData.getPosition() == SUBSCRIBE) {
Log.d(TAG, "on image click..... " + pos);
Log.d(TAG, "For Subscribe...................");
intent = new Intent(getContext(), EpisodeActivity.class);
intent.putExtra("URL", strurl);
getContext().startActivity(intent);
} else {
// TODO REWRITE IT FOR ADDING AS A FAVIOURITE ITEMS
Log.d(TAG, "For Episode...................");
}
}
});
return row;
}
更新
我的适配器:
{{1}}
即使我的代码中有些变化,TouchClickListener也不能用于我的图像
答案 0 :(得分:1)
我使用了Holder
而我更改了if
条件而不是Switch
案例
我的工作代码是:
holder.more.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (listData.getPosition() == SUBSCRIBE) {
if(event.getAction() == MotionEvent.ACTION_UP){
Log.d(TAG,"releasing.....");
holder.fav.setImageResource(R.drawable.ic_more_1);
}
if(event.getAction() == MotionEvent.ACTION_DOWN){
Log.d(TAG,"pressing.....");
holder.fav.setImageResource(R.drawable.ic_more_2);
}
}
return false;
}
});