Android SimpleAdapter问题更改按钮的背景时,所有项目的背景都会更改
当我填充我的Android SimpleAdapter问题更改按钮的背景时,所有项目的背景都会更改,android,simpleadapter,Android,Simpleadapter,当我填充我的simpledapter时。在getView()方法中,我检查视频是否已经存在。如果它已经存在,我会将按钮的图像更改为“播放”图像。否则,我将保留其“下载”图像。问题是当我滚动列表时,它会将所有按钮更改为“播放”图像。这是我的代码,我做错了什么 public View getView(int position, View convertView, ViewGroup parent) { View row=super.getView(position, convertView,
simpledapter
时。在getView()
方法中,我检查视频是否已经存在。如果它已经存在,我会将按钮的图像更改为“播放”图像。否则,我将保留其“下载”图像。问题是当我滚动列表时,它会将所有按钮更改为“播放”图像。这是我的代码,我做错了什么
public View getView(int position, View convertView, ViewGroup parent) {
View row=super.getView(position, convertView, parent);
TextView videoIdText = (TextView) row.findViewById(R.id.videoId);
Button downloadButton = (Button) row.findViewById(R.id.videoDownload);
final String videoId = videoIdText.getText().toString();
if (videoExists(videoId)) {
downloadButton.setBackgroundResource( R.drawable.ic_play );
Drawable d = downloadButton.getBackground();
d.setColorFilter(Color.parseColor("#00AA00"),Mode.SRC_ATOP);
downloadButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
if (activity !=null){
((FeedActivity)activity).playVideo(getVideoPath(videoId));
}
}
});
}else{
downloadButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
DownloadTask task = new DownloadTask();
task.setVideoId(videoId);
task.execute();
}
});
}
在“
if(videoExists(videoId))
的else
子句中,您需要设置默认的“下载”按钮图像和颜色过滤器
这是因为滚动列表时会重复使用项目,因此具有新设置的按钮将与当前未播放的其他项目的新设置一起重复使用
示例:
if (videoExists(videoId)) {
downloadButton.setBackgroundResource( R.drawable.ic_play );
Drawable d = downloadButton.getBackground();
d.setColorFilter(Color.parseColor("#00AA00"), Mode.SRC_ATOP);
...
} else {
downloadButton.setBackgroundResource( R.drawable.ic_download );
Drawable d = downloadButton.getBackground();
d.setColorFilter(Color.parseColor("<download-color>"), Mode.SRC_ATOP);
...
}
if(videoExists(videoId)){
下载按钮。setBackgroundResource(R.drawable.ic_play);
Drawable d=downloadButton.getBackground();
d、 setColorFilter(Color.parseColor(#00AA00)),Mode.SRC_;
...
}否则{
下载按钮.setBackgroundResource(R.drawable.ic_下载);
Drawable d=downloadButton.getBackground();
d、 setColorFilter(Color.parseColor(“”),Mode.SRC_);
...
}
正如@David Manpearl提到的,我需要设置它的原始图像,但我也需要将按钮存储在标签中
public View getView(int position, View convertView, ViewGroup parent) {
View row=super.getView(position, convertView, parent);
TextView videoIdText = (TextView) row.findViewById(R.id.videoId);
Button downloadButton = (Button) row.findViewById(R.id.videoDownload);
final String videoId = videoIdText.getText().toString();
if ( row.getTag() == null){
row.setTag(downloadButton);
}else{
downloadButton = (Button) row.getTag();
}
if (videoExists(videoId)) {
downloadButton.setBackgroundResource( R.drawable.ic_play );
Drawable d = downloadButton.getBackground();
d.setColorFilter(Color.parseColor("#00AA00"),Mode.SRC_ATOP);
downloadButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
if (activity !=null){
((FeedActivity)activity).playVideo(getVideoPath(videoId));
}
}
});
}else{
downloadButton.setBackgroundResource( R.drawable.ic_download );
downloadButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view) {
DownloadTask task = new DownloadTask();
task.setVideoId(videoId);
task.execute();
}
});
}