setVisibility(View.INVISIBLE)无法在ListView的第一行上运行

时间:2015-01-21 18:50:17

标签: android listview android-listview

创建setVisibility(View.INVISIBLE)时,

ListView无法处理Listview的第一行。但是当我向下滚动并向上滚动时,它工作正常

我的代码:

public class AudioListAdapter extends ArrayAdapter<GalleryItem>{

    private Context context;
    private int layoutResourceId;
    private ArrayList<GalleryItem> data = new ArrayList<GalleryItem>();
    private ArrayList<Boolean> isPlaying = new ArrayList<Boolean>();
    boolean isPlaying1;
    final MediaPlayer mediaPlayer = new MediaPlayer();

    public AudioListAdapter(Context context, int resource, ArrayList<GalleryItem> data) {
        super(context, resource, data);
        this.layoutResourceId = resource;
        this.context = context;
        this.data = data;
        Iterator<GalleryItem> iterator = data.iterator();
        while (iterator.hasNext()) {
            GalleryItem galleryItem = (GalleryItem) iterator.next();
            isPlaying.add(false);
        }
        //notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //return super.getView(position, convertView, parent);
        final ViewHolder holder;
        //View row = convertView;

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(layoutResourceId, parent, false);

            holder = new ViewHolder();

            holder.playImage = (ImageView) convertView.findViewById(R.id.audio_play_image);
            holder.stopImage = (ImageView) convertView.findViewById(R.id.audio_stop_image);
            holder.title = (TextView) convertView.findViewById(R.id.audio_title);
            holder.duration = (TextView) convertView.findViewById(R.id.audio_duration);
            isPlaying.set(position, false);
            //convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
            LogUtils.d("TEST", holder.title.getText().toString());
        }




        final GalleryItem item =  data.get(position);
        System.out.println(item.getDateModified());

        holder.title.setText(item.getTitle());
        holder.title.setSelected(true);
        holder.duration.setText("00:00");
        if (isPlaying.get(position)) {
            holder.playImage.setVisibility(View.INVISIBLE);
            holder.stopImage.setVisibility(View.VISIBLE);
        }else{
            holder.playImage.setVisibility(View.VISIBLE);
            holder.stopImage.setVisibility(View.INVISIBLE);
        }

        OnClickListener audioControlsListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isPlaying.get(position)) {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                    }
                    mediaPlayer.reset();
                    isPlaying.set(position, false);
                    holder.playImage.setVisibility(View.VISIBLE);
                    holder.stopImage.setVisibility(View.INVISIBLE);
                }else{
                    holder.playImage.setVisibility(View.INVISIBLE);
                    holder.stopImage.setVisibility(View.VISIBLE);
                    for(int i=0 ; i< isPlaying.size();i++){
                        isPlaying.set(i, false);
                    }
                    try {
                        mediaPlayer.reset();
                        mediaPlayer.setDataSource(item.getPath());
                        mediaPlayer.prepare();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mediaPlayer.start();
                    isPlaying.set(position, true);
                    notifyDataSetChanged();
                }
            }
        };
        holder.playImage.setOnClickListener(audioControlsListener);
        holder.stopImage.setOnClickListener(audioControlsListener);
        convertView.setTag(holder);
        return convertView;
    }

    static class ViewHolder{
        TextView title;
        TextView duration;
        ImageView playImage;
        ImageView stopImage;

    }
}

1 个答案:

答案 0 :(得分:0)

仍然setVisibility(View.INVISIBLE)无效。但我只使用了一个ImageView,并在需要时替换了图像。这对我有用。