简体   繁体   English

将progressbar放入回收视图android

[英]put progressbar in recycle view android

how to put progressbar in recycleview? 如何将进度条放入recycleview? i have to display progress bar when user play song in list,please help me.currently when user select any song ,user still wait for play the song. 当用户在列表中播放歌曲时,我必须显示进度条,请帮助我。当前当用户选择任何歌曲时,用户仍在等待播放歌曲。 thank you. 谢谢。 here is my code: 这是我的代码:

public class CustomAdapterNew extends RecyclerView.Adapter<CustomAdapterNew.MyViewHolder> {
    private ArrayList<SlockDataModel> dataSet;
    Context context;
    private static LayoutInflater inflater = null;
    public static MediaPlayer mediaPlayer = new MediaPlayer();
    Boolean check = false;


    private static final String TAG = "CustomAdapter";
    int current = -1;

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        ImageView img_play;
        AVLoadingIndicatorView progress;

        public MyViewHolder(View itemView) {
            super(itemView);

            this.title = (TextView) itemView.findViewById(R.id.text_slock);
            this.img_play = (ImageView) itemView.findViewById(R.id.img_play);
            progress = (AVLoadingIndicatorView) itemView.findViewById(R.id.avi);

            itemView.setTag(this);
        }
    }

    public CustomAdapterNew(Context slock, ArrayList<SlockDataModel> data) {
        this.dataSet = data;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.slock_list_items, parent, false);
        final MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
        final View mainView = holder.itemView;
        TextView title = holder.title;
        final ImageView btn_play = holder.img_play;
        title.setText(dataSet.get(listPosition).getValue());
        title.setTag(listPosition);

        if(listPosition != current){
            btn_play.setImageResource(R.mipmap.play_btn);
        } else {
            btn_play.setImageResource(R.mipmap.stop_btn);
        }

        title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SlockNew.progress.setVisibility(View.VISIBLE);
                String str = dataSet.get(listPosition).getSong();
                v.getTag(listPosition);
                View oldSongView = SlockNew.lv.getLayoutManager().findViewByPosition(current);

                if (!check || current != listPosition) {
                    if (current != -1) {
                        mediaPlayer.release();
                        btn_play.setImageResource(R.mipmap.stop_btn);
                        ((ImageView) oldSongView.findViewById(R.id.img_play)).setImageResource(R.mipmap.play_btn);
                    }
                    playsong(str);
                    btn_play.setImageResource(R.mipmap.stop_btn);
                    check = true;
                    current = listPosition;
                    SlockNew.progress.setVisibility(View.INVISIBLE);

                } else {
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                        mediaPlayer.reset();
                        mediaPlayer.stop();
                        mediaPlayer.release();
                    }
                    check = false;
                    Log.i(TAG, "ELSE");
                    btn_play.setImageResource(R.mipmap.play_btn);
                    current = -1;
                    SlockNew.progress.setVisibility(View.INVISIBLE);
                }
            }
            //Tapan
            private void playsong(String str) {
                String url = str;
                mediaPlayer = new MediaPlayer();
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mediaPlayer.setDataSource(url);
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

}

Try this, 尝试这个,

   if(listPosition != current){
        btn_play.setImageResource(R.mipmap.play_btn);
        holder.progress.setVisibility(View.VISIBLE);
    } else {
        btn_play.setImageResource(R.mipmap.stop_btn);
        holder.progress.setVisibility(View.GONE);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM