1

当我填充我的SimpleAdapter. 在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();
            }
        });         
    }
4

2 回答 2

2

else您的“”子句中,您if(videoExists(videoId))需要设置默认的“下载”按钮图像和颜色过滤器。

这是因为当您滚动列表时会重复使用这些项目,因此具有新设置的按钮将与当前未播放的其他项目的新设置一起重复使用。


示例

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);
    ...
}
于 2013-03-30T01:22:21.243 回答
0

正如@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();
            }
        });         
    }
于 2013-03-30T09:48:37.003 回答