当按下 imageView 时,我正在尝试编写一个应用程序来更改图像资源。
我提到了这个链接。
我在我的代码中都使用了setOnTouchListener
和。setOnClickListener
在这两种情况下,我都会得到日志。但是,图像没有改变。我在下面给出了我的代码:
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
for中进行此更改,因此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;
}
即使我在代码中进行了一些更改,TouchClickListener 也不适用于我的图像