RecyclerView.Adapter显示错误的数据

时间:2019-04-03 15:21:29

标签: java android

我有一个RecyclerView,它显示多个CountDownTimers,每行有两个切换工厂。但是,每当我添加新的CountDownTimer时,所有行中的所有内容都会更新,并且onClick事件将在所有行上触发。在右边,每行应仅显示其对应的CountDownTimers。尝试了该站点中的大多数方法,但无济于事。有针对这个的解决方法吗?这是我的第一个Java和Android应用程序,因此我不太习惯使用文档。

为我的适配器完成代码,因为我完全不知道为什么它会变成麻烦:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private Context context;
    public static int size;



    public MyAdapter(Context context, ArrayList<mCountDownTimer> timerlist) {
        this.context = context;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {


        public TextView timername;
        public TextView timerduration;
        public FloatingActionButton playpause;
        public FloatingActionButton stopreset;
        mClickListener listener;



        public interface mClickListener{
            void playpausepress(int position);
            void stopresetpress(int position);
        }
        public MyViewHolder(View v, mClickListener listener) {
            super(v);

            timername = v.findViewById(R.id.timer_name);
            timerduration = v.findViewById(R.id.timer_duration);
            playpause = v.findViewById(R.id.play_pause_button);
            stopreset = v.findViewById(R.id.stop_reset_button);

            this.listener = listener;
            playpause.setOnClickListener(this);
            stopreset.setOnClickListener(this);

            }


        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.play_pause_button:
                    int id = mCountDownTimer.getPlayPauseDel();
                    switch(id){
                        case 0:
                            lstTimers.get(getAdapterPosition()).setPlayPauseDel(1);
                            break;
                        case 1:
                            lstTimers.get(getAdapterPosition()).setPlayPauseDel(0);
                            break;
                        case 2:
                            break;
                    }
                    listener.playpausepress(getAdapterPosition());
                    break;
                case R.id.stop_reset_button:
                    if (mCountDownTimer.IsStopped()){
                        mCountDownTimer.setStopped(false);
                        mCountDownTimer.setPlayPauseDel(0);
                        lstTimers.get(getAdapterPosition()).reset();
                    }
                    else {
                        mCountDownTimer.setPlayPauseDel(2);
                        mCountDownTimer.setStopped(true);

                    }
                    listener.stopresetpress(getAdapterPosition());
                    break;
                default:
                    break;
            }
        }
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }



    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.timer_list, parent, false);
        MyViewHolder vh = new MyViewHolder(v, new MyViewHolder.mClickListener() {
            @Override
            public void playpausepress(int position) {
                if (mCountDownTimer.getPlayPauseDel()==2){
                    lstTimers.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(0,lstTimers.size());
                }

            }

            @Override
            public void stopresetpress(int position) {
                notifyItemChanged(position);
            }
        });

        return vh;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position){

            long remaining = lstTimers.get(holder.getAdapterPosition()).getRemaining() / 1000;
            holder.timername.setText(mCountDownTimer.getId());
            holder.timerduration.setText(String.format("Remaining: %ds", remaining));
            if (mCountDownTimer.getPlayPauseDel() == 0) {
                holder.playpause.setImageResource(R.drawable.outline_pause_white_18);
                holder.playpause.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.pausecolor));
            } else if (mCountDownTimer.getPlayPauseDel() == 1) {
                holder.playpause.setImageResource(R.drawable.outline_play_arrow_white_18dp);
                holder.playpause.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.playcolor));
            } else if (mCountDownTimer.getPlayPauseDel() == 2) {
                holder.playpause.setImageResource(R.drawable.outline_delete_white_18dp);
                holder.playpause.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.colorAccent));
            }


            if (mCountDownTimer.getPlayPauseDel() != 2) {
                holder.stopreset.setImageResource(R.drawable.outline_stop_white_36);
                holder.stopreset.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.stopcolor));
            } else {
                holder.stopreset.setImageResource(R.drawable.outline_restore_white_18);
                holder.stopreset.setBackgroundTintList(ContextCompat.getColorStateList(context, R.color.resetcolor));
            }
    }


    @Override
    public int getItemCount() {
        if (lstTimers != null) {
            return lstTimers.size();
        }
        else{
            return 0;
        }
    }

    public void secondupdater(){
        if (!lstTimers.isEmpty()){
            for(int i = 0; i <lstTimers.size(); i++){
                notifyItemChanged(i);
            }
        }

    }


}

我的ArrayList:


    public static ArrayList<mCountDownTimer> lstTimers = new ArrayList<>();

我的mCountDownTimer类:

public class mCountDownTimer extends CountDownTimer {

    public static int PLAY_PAUSE_DEL;
    public static boolean IS_STOPPED = FALSE;
    public static long remaining;
    public static String id;

    public static String getId() {
        if (id == "" || id == null || id.isEmpty()){
            return "Empty Timer";
        }
        else {
            return id;
        }
    }

    public static void setId(String id) {
        mCountDownTimer.id = id;
    }


    public static int getPlayPauseDel() {
        return PLAY_PAUSE_DEL;
    }

    public static void setPlayPauseDel(int playPauseDel) {
        PLAY_PAUSE_DEL = playPauseDel;
        if (PLAY_PAUSE_DEL == 0){
            new mCountDownTimer(remaining,1000).start();
        }

    }

    public mCountDownTimer(long millisinFuture, long countDownInterval){
        super(millisinFuture,countDownInterval);
        PLAY_PAUSE_DEL = 0;
    }

    public static boolean IsStopped() {
        return IS_STOPPED;
    }

    public static void setStopped(boolean isStopped) {
        IS_STOPPED = isStopped;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        remaining = millisUntilFinished;
        if (PLAY_PAUSE_DEL != 0){
            cancel();
        }
    }

    @Override
    public void onFinish() {
        //Future actions
    }    

    public static long getRemaining() {
        return remaining;
    }

    public void reset(){
        start();
    }


}

0 个答案:

没有答案