在listview中滚动过去后才显示的图标?

时间:2015-06-29 22:28:38

标签: android listview icons adapter getview

在我的getView方法中,我根据交换机案例中的几个不同布局设置图标和文本(您将在下面看到)。但是,在我的“Breakpoint”和“Talkset”布局中,talk_break_icon仅在我滚过它然后向后滚动到它之后出现..为什么会这样?它在我的视图持有者中被引用为breakpoint_icon。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    holder = null;
    int type = getItemViewType(position);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listview_cell, null);
        holder = new ViewHolder();
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

     switch (type) {

            case STREAM_LAYOUT:

                holder.boombox = (ImageView) convertView.findViewById(R.id.boombox);
                ViewGroup.LayoutParams boomboxParams = holder.boombox.getLayoutParams();

                boomboxParams.width = ((5*parent.getWidth())/7)-6;
                holder.boombox.setLayoutParams(boomboxParams);

                final ImageView playButton = (ImageView) convertView.findViewById(R.id.play_stop_button);
                playButton.setImageResource(R.drawable.play_button_final);


                if(holder.boombox.getMeasuredHeight() != 0){
                    ViewGroup.LayoutParams playParams = playButton.getLayoutParams();
                    playParams.height = holder.boombox.getMeasuredHeight()-10;

                    playButton.setLayoutParams(playParams);
                }

                playButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(playFlag == false) {
                            mPlayer.changeStates();
                            playButton.setImageResource(R.drawable.stop_button);
                            if(holder.boombox.getMeasuredHeight() != 0){
                                ViewGroup.LayoutParams playParams = playButton.getLayoutParams();
                                playParams.height = holder.boombox.getMeasuredHeight()-10;

                                playButton.setLayoutParams(playParams);
                            }
                            playFlag = true;
                        } else{
                            mPlayer.changeStates();
                            playButton.setImageResource(R.drawable.play_button_final);
                            if(holder.boombox.getMeasuredHeight() != 0){
                                ViewGroup.LayoutParams playParams = playButton.getLayoutParams();
                                playParams.height = holder.boombox.getMeasuredHeight()-10;

                                playButton.setLayoutParams(playParams);
                            }
                            playFlag = false;
                        }
                    }
                });
                holder.boombox.setVisibility(View.VISIBLE);
                playButton.setVisibility(View.VISIBLE);

                convertView.findViewById(R.id.song).setVisibility(View.GONE);
                convertView.findViewById(R.id.artist).setVisibility(View.GONE);
                convertView.findViewById(R.id.bottom_layout).setVisibility(View.GONE);
                convertView.findViewById(R.id.table).setVisibility(View.GONE);

                convertView.setTag(holder);
                break;


            case TALKSET_LAYOUT:
                convertView.findViewById(R.id.boombox).setVisibility(View.GONE);
                convertView.findViewById(R.id.play_stop_button).setVisibility(View.GONE);

                convertView.findViewById(R.id.cell_image).setVisibility(View.GONE);
                convertView.findViewById(R.id.divider).setVisibility(View.GONE);
                convertView.findViewById(R.id.playcut).setVisibility(View.GONE);
                convertView.findViewById(R.id.table).setVisibility(View.GONE);

                holder.talkset = (TextView) convertView.findViewById(R.id.talk_break_text);
                holder.talkset.setText(" TALKSET");


                holder.breakpoint_icon = (ImageView) convertView.findViewById(R.id.talk_break_image);
                holder.breakpoint_icon.setImageResource(R.drawable.talkset);
                holder.breakpoint_icon.setVisibility(View.VISIBLE);


                ViewGroup.LayoutParams iconParams = ((ImageView) convertView.findViewById(R.id.talk_break_image)).getLayoutParams();
                iconParams.height = holder.talkset.getHeight()+7;
                iconParams.width = holder.talkset.getHeight()+7;
                convertView.findViewById(R.id.talk_break_image).setLayoutParams(iconParams);

                convertView.setTag(holder);
                break;

            case BREAKPOINT_LAYOUT:

                convertView.findViewById(R.id.boombox).setVisibility(View.GONE);
                convertView.findViewById(R.id.play_stop_button).setVisibility(View.GONE);
                convertView.findViewById(R.id.cell_image).setVisibility(View.GONE);
                convertView.findViewById(R.id.divider).setVisibility(View.GONE);
                convertView.findViewById(R.id.artist).setVisibility(View.GONE);
                convertView.findViewById(R.id.table).setVisibility(View.GONE);

                holder.breakpoint_icon = (ImageView) convertView.findViewById(R.id.talk_break_image);

                holder.breakpoint = (TextView) convertView.findViewById(R.id.talk_break_text);
                holder.breakpoint.setText(" BREAKPOINT");

                ViewGroup.LayoutParams iParams = holder.breakpoint_icon.getLayoutParams();
                iParams.height = holder.breakpoint.getHeight()+7;
                iParams.width = holder.breakpoint.getHeight()+7;
                holder.breakpoint_icon.setLayoutParams(iParams);

                long timeStamp = Long.parseLong(oslist.get(position).get("hour"))/1000;
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(timeStamp * 1000L);
                int hour = calendar.get(Calendar.HOUR);

                if(hour > 12){
                    hour = hour-12;
                }

                switch (hour) {
                    case 1:
                        holder.breakpoint_icon.setImageResource(R.drawable.one_breakpoint);
                        break;
                    case 2:
                        holder.breakpoint_icon.setImageResource(R.drawable.two_breakpoint);
                        break;
                    case 3:
                        holder.breakpoint_icon.setImageResource(R.drawable.three_breakpoint);
                        break;
                    case 4:
                        holder.breakpoint_icon.setImageResource(R.drawable.four_breakpoint);
                        break;
                    case 5:
                        holder.breakpoint_icon.setImageResource(R.drawable.five_breakpoint);
                        break;
                    case 6:
                        holder.breakpoint_icon.setImageResource(R.drawable.six_breakpoint);
                        break;
                    case 7:
                        holder.breakpoint_icon.setImageResource(R.drawable.seven_breakpoint);
                        break;
                    case 8:
                        holder.breakpoint_icon.setImageResource(R.drawable.eight_breakpoint);
                        break;
                    case 9:
                        holder.breakpoint_icon.setImageResource(R.drawable.nine_breakpoint);
                        break;
                    case 10:
                        holder.breakpoint_icon.setImageResource(R.drawable.ten_breakpoint);
                        break;
                    case 11:
                        holder.breakpoint_icon.setImageResource(R.drawable.eleven_breakpoint);
                        break;
                    case 12:
                        holder.breakpoint_icon.setImageResource(R.drawable.twelve_breakpoint);
                        break;
                }

                holder.breakpoint_icon.setVisibility(View.VISIBLE);

                convertView.setTag(holder);

                break;

            case PLAYCUT_LAYOUT: //Playcut

                convertView.findViewById(R.id.boombox).setVisibility(View.GONE);
                convertView.findViewById(R.id.play_stop_button).setVisibility(View.GONE);
                holder.iconTable = (TableLayout) convertView.findViewById(R.id.table);
                holder.iconTable.setVisibility(View.GONE);

                convertView.findViewById(R.id.playcut).setVisibility(View.VISIBLE);

                holder.cell_image = (ImageView) convertView.findViewById(R.id.cell_image);

                try {
                    if(oslist.get(position).get("albumArtUrl")!=null) {
                        Picasso
                                .with(context)
                                .load(oslist.get(position).get("albumArtUrl"))
                                .placeholder(R.drawable.no_album_art)
                                .error(R.drawable.no_album_art).into(holder.cell_image);
                    }else{
                        Picasso
                                .with(context)
                                .load(oslist.get(position).get("artistArtUrl"))
                                .placeholder(R.drawable.no_album_art)
                                .error(R.drawable.no_album_art).into(holder.cell_image);
                    }

                } catch (IllegalArgumentException e) {
                    holder.cell_image.setImageResource(R.drawable.no_album_art);
                }

                holder.song = (TextView) convertView.findViewById(R.id.song);
                holder.artist = (TextView) convertView.findViewById(R.id.artist);
                holder.song.setVisibility(View.VISIBLE);
                holder.artist.setVisibility(View.VISIBLE);

                holder.song.setText(oslist.get(position).get("songTitle"));
                holder.artist.setText(oslist.get(position).get("artistName"));

                convertView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        updateView(position, null);
                    }
                });

                convertView.setTag(holder);

                break;
            case NULL_LAYOUT:
                convertView.findViewById(R.id.boombox).setVisibility(View.GONE);
                convertView.findViewById(R.id.play_stop_button).setVisibility(View.GONE);
                holder.iconTable = (TableLayout) convertView.findViewById(R.id.table);
                holder.iconTable.setVisibility(View.GONE);

                convertView.setTag(holder);
                break;

        }

    return convertView;

}

1 个答案:

答案 0 :(得分:0)

您需要添加notifyDataSetChanged();

试试这个:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

holder = null;
int type = getItemViewType(position);

if (convertView == null) {
    convertView = mInflater.inflate(R.layout.listview_cell, null);
    holder = new ViewHolder();
} else {
    holder = (ViewHolder) convertView.getTag();
}

 switch (type) {

        case STREAM_LAYOUT:

            holder.boombox = (ImageView) convertView.findViewById(R.id.boombox);
            ViewGroup.LayoutParams boomboxParams = holder.boombox.getLayoutParams();

            boomboxParams.width = ((5*parent.getWidth())/7)-6;
            holder.boombox.setLayoutParams(boomboxParams);

            final ImageView playButton = (ImageView) convertView.findViewById(R.id.play_stop_button);
            playButton.setImageResource(R.drawable.play_button_final);
            notifyDataSetChanged();


            if(holder.boombox.getMeasuredHeight() != 0){
                ViewGroup.LayoutParams playParams = playButton.getLayoutParams();
                playParams.height = holder.boombox.getMeasuredHeight()-10;

                playButton.setLayoutParams(playParams);
            }

            playButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(playFlag == false) {
                        mPlayer.changeStates();
                        playButton.setImageResource(R.drawable.stop_button);
                        notifyDataSetChanged();
                        if(holder.boombox.getMeasuredHeight() != 0){
                            ViewGroup.LayoutParams playParams = playButton.getLayoutParams();
                            playParams.height = holder.boombox.getMeasuredHeight()-10;

                            playButton.setLayoutParams(playParams);
                        }
                        playFlag = true;
                    } else{
                        mPlayer.changeStates();
                        playButton.setImageResource(R.drawable.play_button_final);
                        notifyDataSetChanged();
                        if(holder.boombox.getMeasuredHeight() != 0){
                            ViewGroup.LayoutParams playParams = playButton.getLayoutParams();
                            playParams.height = holder.boombox.getMeasuredHeight()-10;

                            playButton.setLayoutParams(playParams);
                        }
                        playFlag = false;
                    }
                }
            });
            holder.boombox.setVisibility(View.VISIBLE);
            playButton.setVisibility(View.VISIBLE);

            convertView.findViewById(R.id.song).setVisibility(View.GONE);
            convertView.findViewById(R.id.artist).setVisibility(View.GONE);
            convertView.findViewById(R.id.bottom_layout).setVisibility(View.GONE);
            convertView.findViewById(R.id.table).setVisibility(View.GONE);

            convertView.setTag(holder);
            break;


        case TALKSET_LAYOUT:
            convertView.findViewById(R.id.boombox).setVisibility(View.GONE);
            convertView.findViewById(R.id.play_stop_button).setVisibility(View.GONE);

            convertView.findViewById(R.id.cell_image).setVisibility(View.GONE);
            convertView.findViewById(R.id.divider).setVisibility(View.GONE);
            convertView.findViewById(R.id.playcut).setVisibility(View.GONE);
            convertView.findViewById(R.id.table).setVisibility(View.GONE);

            holder.talkset = (TextView) convertView.findViewById(R.id.talk_break_text);
            holder.talkset.setText(" TALKSET");


            holder.breakpoint_icon = (ImageView) convertView.findViewById(R.id.talk_break_image);
            holder.breakpoint_icon.setImageResource(R.drawable.talkset);
            notifyDataSetChanged();
            holder.breakpoint_icon.setVisibility(View.VISIBLE);


            ViewGroup.LayoutParams iconParams = ((ImageView) convertView.findViewById(R.id.talk_break_image)).getLayoutParams();
            iconParams.height = holder.talkset.getHeight()+7;
            iconParams.width = holder.talkset.getHeight()+7;
            convertView.findViewById(R.id.talk_break_image).setLayoutParams(iconParams);

            convertView.setTag(holder);
            break;

        case BREAKPOINT_LAYOUT:

            convertView.findViewById(R.id.boombox).setVisibility(View.GONE);
            convertView.findViewById(R.id.play_stop_button).setVisibility(View.GONE);
            convertView.findViewById(R.id.cell_image).setVisibility(View.GONE);
            convertView.findViewById(R.id.divider).setVisibility(View.GONE);
            convertView.findViewById(R.id.artist).setVisibility(View.GONE);
            convertView.findViewById(R.id.table).setVisibility(View.GONE);

            holder.breakpoint_icon = (ImageView) convertView.findViewById(R.id.talk_break_image);

            holder.breakpoint = (TextView) convertView.findViewById(R.id.talk_break_text);
            holder.breakpoint.setText(" BREAKPOINT");

            ViewGroup.LayoutParams iParams = holder.breakpoint_icon.getLayoutParams();
            iParams.height = holder.breakpoint.getHeight()+7;
            iParams.width = holder.breakpoint.getHeight()+7;
            holder.breakpoint_icon.setLayoutParams(iParams);

            long timeStamp = Long.parseLong(oslist.get(position).get("hour"))/1000;
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(timeStamp * 1000L);
            int hour = calendar.get(Calendar.HOUR);

            if(hour > 12){
                hour = hour-12;
            }

            switch (hour) {
                case 1:
                    holder.breakpoint_icon.setImageResource(R.drawable.one_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 2:
                    holder.breakpoint_icon.setImageResource(R.drawable.two_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 3:
                    holder.breakpoint_icon.setImageResource(R.drawable.three_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 4:
                    holder.breakpoint_icon.setImageResource(R.drawable.four_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 5:
                    holder.breakpoint_icon.setImageResource(R.drawable.five_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 6:
                    holder.breakpoint_icon.setImageResource(R.drawable.six_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 7:
                    holder.breakpoint_icon.setImageResource(R.drawable.seven_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 8:
                    holder.breakpoint_icon.setImageResource(R.drawable.eight_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 9:
                    holder.breakpoint_icon.setImageResource(R.drawable.nine_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 10:
                    holder.breakpoint_icon.setImageResource(R.drawable.ten_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 11:
                    holder.breakpoint_icon.setImageResource(R.drawable.eleven_breakpoint);
                    notifyDataSetChanged();
                    break;
                case 12:
                    holder.breakpoint_icon.setImageResource(R.drawable.twelve_breakpoint);
                    notifyDataSetChanged();
                    break;
            }

            holder.breakpoint_icon.setVisibility(View.VISIBLE);

            convertView.setTag(holder);

            break;

        case PLAYCUT_LAYOUT: //Playcut

            convertView.findViewById(R.id.boombox).setVisibility(View.GONE);
            convertView.findViewById(R.id.play_stop_button).setVisibility(View.GONE);
            holder.iconTable = (TableLayout) convertView.findViewById(R.id.table);
            holder.iconTable.setVisibility(View.GONE);

            convertView.findViewById(R.id.playcut).setVisibility(View.VISIBLE);

            holder.cell_image = (ImageView) convertView.findViewById(R.id.cell_image);

            try {
                if(oslist.get(position).get("albumArtUrl")!=null) {
                    Picasso
                            .with(context)
                            .load(oslist.get(position).get("albumArtUrl"))
                            .placeholder(R.drawable.no_album_art)
                            .error(R.drawable.no_album_art).into(holder.cell_image);
                }else{
                    Picasso
                            .with(context)
                            .load(oslist.get(position).get("artistArtUrl"))
                            .placeholder(R.drawable.no_album_art)
                            .error(R.drawable.no_album_art).into(holder.cell_image);
                }

            } catch (IllegalArgumentException e) {
                holder.cell_image.setImageResource(R.drawable.no_album_art);
                notifyDataSetChanged();
            }

            holder.song = (TextView) convertView.findViewById(R.id.song);
            holder.artist = (TextView) convertView.findViewById(R.id.artist);
            holder.song.setVisibility(View.VISIBLE);
            holder.artist.setVisibility(View.VISIBLE);

            holder.song.setText(oslist.get(position).get("songTitle"));
            holder.artist.setText(oslist.get(position).get("artistName"));

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    updateView(position, null);
                }
            });

            convertView.setTag(holder);

            break;
        case NULL_LAYOUT:
            convertView.findViewById(R.id.boombox).setVisibility(View.GONE);
            convertView.findViewById(R.id.play_stop_button).setVisibility(View.GONE);
            holder.iconTable = (TableLayout) convertView.findViewById(R.id.table);
            holder.iconTable.setVisibility(View.GONE);

            convertView.setTag(holder);
            break;

    }

return convertView;

}