RecyclerView中的计时器:滚动时计时器变为00:00:00

时间:2016-12-27 04:24:29

标签: android timer android-recyclerview chronometer

我关注此123。我有RecyclerView CardView。我使用Chronometer添加了一个计时器。应用程序的工作方式如下:

enter image description here

绿色CardView的第一个条件。当用户选择一个时,颜色变为黄色。

如果00:30:00(HH:MM:SS)中的计时器CardView的颜色再次变为蓝色,我想要make。当计时器在01:00:00(HH:MM:SS)时,CardView的颜色再次变为布朗。

我跟随那位导师(上图)但是当我向下滚动RecyclerView时,计时器(Choronometer)再次变为00:00:00。我不知道哪里错了。

Activity.java

private static final int THIRTYMINUTES = 1800000;
private static final int HOUR = 3600000;

int h,m,s;

...........
...........

@Override
        public void OnRecyclerViewItemBind(final Adapter.ViewHolder holder, final int position) {
            holder.txt_time_occ.setBase(SystemClock.elapsedRealtime());

            try {
                JSONObject currTable = filteredTableList.getJSONObject(position);

                holder.txt_no_table.setText(currTable.getString("tischnr"));
                holder.txt_no_table_occ.setText(currTable.getString("tischnr"));
                holder.txt_guest_name_occ.setText("");
                holder.txt_pax_occ.setText("");
                holder.txt_bill_occ.setText("");

                holder.txt_time_occ.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener(){
                    @Override
                    public void onChronometerTick(Chronometer cArg) {
                        long time = SystemClock.elapsedRealtime() - cArg.getBase();
                        h = (int)(time /3600000);
                        m = (int)(time - h*3600000)/60000;
                        s = (int)(time - h*3600000 - m*60000)/1000 ;
                        String hh = h < 10 ? "0"+h: h+"";
                        String mm = m < 10 ? "0"+m: m+"";
                        String ss = s < 10 ? "0"+s: s+"";
                        cArg.setText(hh+":"+mm+":"+ss);
                        holder.txt_time_occ.setText(String.format("%02d:%02d:%02d", h, m, s));

                        if (time > t1){
                            holder.itemView.setBackgroundResource(R.color.colorBrownTableOcc_3);
                            holder.title_layout.setBackgroundResource(R.color.colorBrownTableOcc_4);
                        }

                        if (time > t2) {
                            holder.itemView.setBackgroundResource(R.color.colorRedTableOcc);
                            holder.title_layout.setBackgroundResource(R.color.colorDeepRedTableOcc);
                        }
                    }
                });


                int queasy33Index = ProgramMethod.getJSONArrayIndex(queasy33List,"number2", currTable.getInt("tischnr"));
                if (queasy33Index >= 0) {
                    holder.txt_guest_name_occ.setText(queasy33List.getJSONObject(queasy33Index).getString("char2"));
                    holder.txt_pax_occ.setText(queasy33List.getJSONObject(queasy33Index).getString("number3"));
                }

                int billIndex = ProgramMethod.getJSONArrayIndex(billList, "tischnr", currTable.getInt("tischnr"));
                if(billIndex >= 0) {
                holder.txt_bill_occ.setText(billList.getJSONObject(billIndex).getString("saldo"));
                }

                if (currTable.has("selected") && currTable.getBoolean("selected")) {
                    holder.itemView.setBackgroundResource(R.color.colorYellowTableOcc_6);
                    holder.title_layout.setBackgroundResource(R.color.colorYellowTableOcc_7);
                    holder.txt_time_occ.setBase(SystemClock.elapsedRealtime());
                    holder.txt_time_occ.start();
                    holder.txt_no_table.setVisibility(View.INVISIBLE);
                    holder.txt_no_table_occ.setVisibility(View.VISIBLE);
                    holder.txt_bill_occ.setVisibility(View.VISIBLE);
                    holder.txt_pax_occ.setVisibility(View.VISIBLE);
                    holder.txt_time_occ.setVisibility(View.VISIBLE);
                    holder.txt_guest_name_occ.setVisibility(View.VISIBLE);
                    holder.cardview_table.setCardElevation(24);

                } else {
                    holder.txt_time_occ.stop();
                    holder.txt_time_occ.setBase(SystemClock.elapsedRealtime());
                    holder.itemView.setBackgroundResource(R.color.colorTableGreen);
                    holder.title_layout.setBackgroundResource(R.color.colorTableGreen);
                    holder.txt_no_table.setVisibility(View.VISIBLE);
                    holder.txt_no_table_occ.setVisibility(View.INVISIBLE);
                    holder.txt_bill_occ.setVisibility(View.INVISIBLE);
                    holder.txt_pax_occ.setVisibility(View.INVISIBLE);
                    holder.txt_time_occ.setVisibility(View.INVISIBLE);
                    holder.txt_guest_name_occ.setVisibility(View.INVISIBLE);
                    holder.cardview_table.setCardElevation(1);
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int OnRecyclerViewItemCount() {
            return filteredTableList.length();
        }
    });
}

Adapter.java

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

public static final int view1 = 0;
public static final int view2 = 1;

private static OnRecyclerViewItemClickedListener recyclerViewItemClickedListener;

public void setOnRecyclerViewClickedListener (OnRecyclerViewItemClickedListener l) {
    recyclerViewItemClickedListener = l;
}

public interface OnRecyclerViewItemClickedListener {
    void OnRecyclerViewItemClicked(int position);
    void OnRecyclerViewItemBind(ViewHolder holder, int position);
    int OnRecyclerViewItemCount();
}

public Adapter() {
    super();
}


@Override
public Adapter.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {



    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.table_item_empty, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    recyclerViewItemClickedListener.OnRecyclerViewItemBind(holder, position);
}

@Override
public int getItemCount() {
    return recyclerViewItemClickedListener.OnRecyclerViewItemCount();
}


public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView txt_no_table;
    public TextView txt_no_table_occ;
    public TextView txt_pax_occ;
    public TextView txt_guest_name_occ;
    public TextView txt_bill_occ;
    public Chronometer txt_time_occ;
    public CardView cardview_table;



    public ViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);

        txt_no_table = (TextView) itemView.findViewById(R.id.txt_no_table_empty);
        txt_no_table_occ = (TextView) itemView.findViewById(R.id.txt_no_table);
        txt_pax_occ = (TextView) itemView.findViewById(R.id.txt_pax);
        txt_guest_name_occ = (TextView)itemView.findViewById(R.id.txt_guestname);
        txt_bill_occ = (TextView) itemView.findViewById(R.id.txt_bill);
        txt_time_occ = (Chronometer) itemView.findViewById(R.id.txt_time);
        cardview_table = (CardView) itemView.findViewById(R.id.table_card_empty);
    }

    @Override
    public void onClick(View itemView) {
        recyclerViewItemClickedListener.OnRecyclerViewItemClicked(getAdapterPosition());
   }
}

任何答案对我都有帮助。先谢谢

0 个答案:

没有答案