把进度条放在循环视图android中

时间:2017-03-22 06:29:47

标签: java android

如何将进度条放在recycleview中? 当用户在列表中播放歌曲时我必须显示进度条,请帮助我。当用户选择任何一首歌时,用户仍然等待播放歌曲。 谢谢。 这是我的代码:

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();
    }

}

1 个答案:

答案 0 :(得分:1)

试试这个,

   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);
    }