可扩展列表视图中的多个倒数计时器 Android 不工作

Multiple Countdown Timer in Expandable Listview Android not working

从上周开始,我尝试在可扩展列表中实现多个倒数计时器 view.I 想减少时间直到 00:00:00,据此我需要更新可扩展列表视图的父元素中的文本.

我的问题是:

1) 滚动可扩展列表视图计时器重新启动
2) 展开和折叠计时器重新启动
3)在子按钮上发生事件时,我需要更新父视图上特定位置的文本

我试图实现的另一件事是从子视图中单击一个按钮,我需要更新父视图中的文本,但我被困在那里。

这是我的适配器,包含父项和子项。

public class Sent_ListAdapter extends BaseExpandableListAdapter {
    private Context _context;
    **private List<SentModel> _listDataHeader;**
    // child data in format of header title, child title
    **private HashMap<SentModel, List<SentModel>> _listDataChild;**
    private TextView txt_summary;
    private TextView datetime_element;
    private LinearLayout ll_arrowtorecord;
    **private String[] ti;**
    **private CountDownTimer cdt_sent;**
    **private HashMap<TextView, CountDownTimer> counters;**
    **private TextView deadline;**

    public Sent_ListAdapter(Context context, List<SentModel> listDataHeader, HashMap<SentModel, List<SentModel>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
        ll_end = new FrameLayout[_listDataHeader.size()];
        ll_closeaceess = new FrameLayout[_listDataHeader.size()];
        ll_whoaccpted = new LinearLayout[_listDataHeader.size()];
        **this.counters = new HashMap<TextView, CountDownTimer>();**
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final SentModel headerTitle = (SentModel) getGroup(groupPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.element_sentlist, null);
        }
        ll_arrowtorecord = (LinearLayout) convertView.findViewById(R.id.ll_arrowtorecord);
        ll_arrowtorecord.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (headerTitle.getFile().trim().contains(".mp4")) {
                    Log.v("", TAG + "==video==" + headerTitle.getFile());
                    Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
                    Uri data = Uri.parse(headerTitle.getFile());
                    intent.setDataAndType(data, "video/mp4");
                    startActivity(intent);
                } else if (headerTitle.getFile().trim().contains(".jpg")) {
                    Log.v("", TAG + "==image==" + headerTitle.getFile());
                    Intent gotorecoedactivity = new Intent(context, RecordedAsset.class);
                    gotorecoedactivity.putExtra("image", headerTitle.getFile());
                    startActivity(gotorecoedactivity);
                    overridePendingTransition(R.anim.right_slide_in, R.anim.left_side_out);
                    finish();
                } else {
                    Toast.makeText(context, "No recorded asset is there.", Toast.LENGTH_SHORT).show();
                }
            }
        });
        txt_summary = (TextView) convertView.findViewById(R.id.txt_summary);
        if (headerTitle.get_desc().equalsIgnoreCase("") || headerTitle.get_desc() == null) {
            txt_summary.setText(" Description");
        } else {
            txt_summary.setText(headerTitle.get_desc());
        }
        datetime_element = (TextView) convertView.findViewById(R.id.datetime_element);
        final TextView tv = datetime_element;
        deadline = (TextView) convertView.findViewById(R.id.deadline);
        **if (headerTitle.getRemaining_completion_time().equalsIgnoreCase("") || headerTitle.getRemaining_completion_time() == null) {
            datetime_element.setText("");
        } 
        else if (headerTitle.getRemaining_completion_time().equalsIgnoreCase("00:00:00"))
        {
            tv.setText("Completion time over");
            deadline.setVisibility(View.GONE);
        } 
        else 
        {
            ti = headerTitle.getRemaining_completion_time().split(":");
            int hrs = Integer.parseInt(ti[0]);
            cdt_sent = counters.get(datetime_element);
            int days = Integer.parseInt(headerTitle.getRemaining_days());
            //  int hours1 = datetime_plus(days, Integer.parseInt(ti[0]));
            int min = Integer.parseInt(ti[1]);
            int sec = Integer.parseInt(ti[2]);
            long d1 = TimeUnit.DAYS.toMillis(days);
            long m1 = TimeUnit.MINUTES.toMillis(min);
            long h1 = TimeUnit.HOURS.toMillis(hrs);
            long s1 = TimeUnit.SECONDS.toMillis(sec);
            final long milliseco = d1 + m1 + h1 + s1;
            if (cdt_sent != null) {
                cdt_sent.cancel();
                cdt_sent = null;
            }
            cdt_sent = new CountDownTimer(milliseco, 1000)
            {
                int days = 0;
                int hours1 = 0;
                private String sDate;
                int min = 0;
                int sec = 0;
                @Override
                public void onTick(long millisUntilFinished)
                {
                    millisUntilFinished -= (days * DateUtils.DAY_IN_MILLIS);
                    if (millisUntilFinished > DateUtils.HOUR_IN_MILLIS)
                    {
                        hours1 = (int) (millisUntilFinished / DateUtils.HOUR_IN_MILLIS);
                    }
                    millisUntilFinished -= (hours1 * DateUtils.HOUR_IN_MILLIS);
                    if (millisUntilFinished > DateUtils.MINUTE_IN_MILLIS) 
                    {
                        min = (int) (millisUntilFinished / DateUtils.MINUTE_IN_MILLIS);
                    }
                    millisUntilFinished -= (min * DateUtils.MINUTE_IN_MILLIS);
                    if (millisUntilFinished > DateUtils.SECOND_IN_MILLIS)
                    {
                        sec = (int) (millisUntilFinished / DateUtils.SECOND_IN_MILLIS);
                    }
                    sDate = " " + String.format("%02d", hours1) + "h " + String.format("%02d", min) + "m " + String.format("%02d", sec) + "s ";
                    _listDataHeader.get(groupPosition).setTime(sDate);
                    String hms = String.format("%02d,%02d:%02d:%02d", TimeUnit.MILLISECONDS.toDays(milliseco),TimeUnit.MILLISECONDS.toHours(milliseco),
                            TimeUnit.MILLISECONDS.toMinutes(milliseco) % TimeUnit.HOURS.toMinutes(1),
                            TimeUnit.MILLISECONDS.toSeconds(milliseco) % TimeUnit.MINUTES.toSeconds(1));
                    //  Log.v("", "Bansi"+"==hms=="+hms);
                    tv.setText(_listDataHeader.get(groupPosition).getTime());
                }
                @Override
                public void onFinish() {
                    tv.setText("Completion time over");
                    deadline.setVisibility(View.GONE);
                }
            };
            counters.put(tv, cdt_sent);
            cdt_sent.start();
        }**
        return convertView;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final SentModel childText = (SentModel) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.element_childsent, null);
        }
        ll_whoaccpted[groupPosition] = (LinearLayout) convertView.findViewById(R.id.ll_whoaccpted);
        ll_end[groupPosition] = (FrameLayout) convertView.findViewById(R.id.ll_end);
        ll_closeaceess[groupPosition] = (FrameLayout) convertView.findViewById(R.id.ll_closeaceess);
        txt_whoaccepted = (TextView) convertView.findViewById(R.id.txt_whoaccepted);
        img_block = (ImageView) convertView.findViewById(R.id.img_block);
        img_end = (ImageView) convertView.findViewById(R.id.img_end);
        txt_end = (TextView) convertView.findViewById(R.id.txt_end);
        txt_closeacess = (TextView) convertView.findViewById(R.id.txt_closeacess);
        ll_whoaccpted[groupPosition].setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showWhoAcceptedpopup(childText.getB_id(), groupPosition + "");
                if (Utils.detectInternetConnection(context)) {
                    new post_WhoAccepted().execute(childText.getB_id(), groupPosition + "");
                } else {
                    progressDialog.showWarningDialog_Click(getString(R.string.no_internet), new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            progressDialog.dialogDismiss();
                            try {
                                Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                                startActivity(callGPSSettingIntent);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        });
        if (childText.getIs_cancel().equalsIgnoreCase("0")) {
            ll_end[groupPosition].setBackgroundColor(Color.parseColor("#CBCBCB"));
            txt_end.setTextColor(Color.parseColor("#7E7E7E"));
            txt_end.setText("You ended the .");
            txt_end.setGravity(Gravity.CENTER);
            img_end.setImageResource(R.drawable.cross_circleclick);
            ll_end[groupPosition].setClickable(false);
        } else {
            ll_end[groupPosition].setBackgroundColor(Color.parseColor("#650030"));
            txt_end.setTextColor(Color.parseColor("#FF0101"));
            txt_end.setText("End ");
            txt_end.setGravity(Gravity.RIGHT);
            img_end.setImageResource(R.drawable.cross_circle);
            ll_end[groupPosition].setClickable(true);
            if (endornot.get(groupPosition) == 0) {
            } else {
                ll_end[groupPosition].setBackgroundColor(Color.parseColor("#CBCBCB"));
                txt_end.setTextColor(Color.parseColor("#7E7E7E"));
                txt_end.setText("You ended the .");
                img_end.setImageResource(R.drawable.cross_circleclick);
                txt_end.setGravity(Gravity.CENTER);
                ll_end[groupPosition].setClickable(false);
            }
            ll_end[groupPosition].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("", TAG + "==childposition==" + groupPosition);
                    if (endornot.get(groupPosition) == 0) {
                        if (Utils.detectInternetConnection(context)) {
                            new post_EndorClose().execute(childText.getB_id(), "1", groupPosition + "");
                        } else {
                            progressDialog.showWarningDialog_Click(getString(R.string.no_internet), new OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    progressDialog.dialogDismiss();
                                    try {
                                        Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                                        startActivity(callGPSSettingIntent);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    } else {
                    }
                }
            });
        }
        if (childText.getIs_close().equalsIgnoreCase("0")) {
            ll_closeaceess[groupPosition].setBackgroundColor(Color.parseColor("#E7E7E7"));
            txt_closeacess.setTextColor(Color.parseColor("#7E7E7E"));
            txt_closeacess.setText("Access to the closed");
            txt_closeacess.setGravity(Gravity.CENTER);
            ll_closeaceess[groupPosition].setClickable(false);
        } else {
            ll_closeaceess[groupPosition].setBackgroundColor(Color.parseColor("#006500"));
            txt_closeacess.setTextColor(Color.parseColor("#043303"));
            txt_closeacess.setText("Close access to ");
            ll_closeaceess[groupPosition].setClickable(true);
            txt_closeacess.setGravity(Gravity.RIGHT);
            if (aceessdeniedornot.get(groupPosition) == 0) {
            } else {
                ll_closeaceess[groupPosition].setBackgroundColor(Color.parseColor("#E7E7E7"));
                txt_closeacess.setTextColor(Color.parseColor("#7E7E7E"));
                txt_closeacess.setText("Access to the closed");
                txt_closeacess.setGravity(Gravity.CENTER);
                ll_closeaceess[groupPosition].setClickable(false);
            }
            ll_closeaceess[groupPosition].setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.v("", TAG + "==childposition==" + groupPosition);
                    if (aceessdeniedornot.get(groupPosition) == 0) {
                        if (Utils.detectInternetConnection(context)) {
                            new post_EndorClose().execute(childText.getB_id(), "2", groupPosition + "");
                        } else {
                            progressDialog.showWarningDialog_Click(getString(R.string.no_internet), new OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    progressDialog.dialogDismiss();
                                    try {
                                        Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                                        startActivity(callGPSSettingIntent);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    } else {
                    }
                }
            });
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

请帮帮我,我仍然被困在这里我正在努力解决它,但我有时间限制。 谢谢!

我得先说这个,你的代码真的很难阅读。

  1. 尝试使用方法将视图创建代码与 getGroupViewgetChildView 上的繁忙代码分开。您也可以在 getChildView 上使用 ViewHolder 模式来保存您的视图,而不是那些数组(ll_whoaccpted,等等...)
  2. TextUtils.isEmpty() 可以帮助您更好地检查您的字符串是空的还是空的

    if (TextUtils.isEmpty(headerTitle.getRemaining_completion_time()))
    
    //instead of
    
    if (headerTitle.getRemaining_completion_time().equalsIgnoreCase("") || headerTitle.getRemaining_completion_time() == null)
    
  3. 使 Sent_ListAdapter 实现 OnClickListener,而不是每次 Android 为您的列表获取视图时都重新创建侦听器(新)。在视图参数上添加条件以检查单击了哪种视图类型。

  4. 在 CountDownTimer 上使用 Date 和 SimpleDataFormat,这些手动字符串格式非常糟糕且不可读

现在进入正题:

on scrolling expandable list view timer restarts

发生这种情况是因为 ListView 重新创建了未显示的 views/groups(调用 getView/getGroup 方法)以重用其他数据的行。

如果你想避免这种情况,你应该在 getView 和 getGroup 之外创建并启动你的计数器(构造函数是个好地方)。

on expand and collapse timer restarts

与第一个问题完全相同。

on event on child button i need to update particular position's text on parentview

我不太明白,所以我猜您想更改主布局(包含列表视图的布局)上的 TextView 的文本。那么您需要将该 TextView 的引用提供给 Sent_ListAdapter

或者,不是将通用上下文传递给 Sent_ListAdapter,而是传递 Activity/Fragment 它自己:

public Sent_ListAdapter(MyActivity activty, List<SentModel> listDataHeader, HashMap<SentModel, List<SentModel>> listChildData) {
            this._myactivity = myactivity;
            this._listDataHeader = listDataHeader;
...
}

// The listener that handles all the events of your Sent_ListAdapter

@Override
public void onClick(View v) {
  if(v.getTag() == "theBoutonThatShouldResetTheCounter") { //or other condition
      myactivite.myTextView.setText("------");
  }
}

编辑


But how will I get position in constructor for every row?

您需要反过来思考:列表视图使用您的数据源来显示行。因此,您需要遍历数据源上的元素(在您的情况下为 listDataHeader)。像 :

private List<CountDownTimer> counters;

public Sent_ListAdapter(Context context, List<SentModel> listDataHeader, HashMap<SentModel, List<SentModel>> listChildData) {
    ...
    counters = new ArrayList()<>;
    for(SentModel model : listDataHeader) {
        long milliseco = model.getRemainingTimeInMs(); //TODO: create something like this or figure out how to get your couters starting times on ms
        CountDownTimer cdt = new MyCountDownTimer(milliseco, 1000);
        cdt.start(); // I don't know when you want to start your counters, if they start at the same time it could be here
    }
}

public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    ...
    datetime_element = (TextView) convertView.findViewById(R.id.datetime_element);
    deadline = (TextView) convertView.findViewById(R.id.deadline);
    ...
    CountDownTimer cdt = counters.get(groupPosition);
    cdt.setTv1 = datetime_element;
    cdt.setTv2 = deadline;
    ...
}

public class MyCountDownTimer extends CountDownTimer {

    public TextView tv1; //TODO: make setters instead of public
    public TextView tv2; //TODO: make setters instead of public

    public MyCountDownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        //Only use this if u have something to do each tick
        SimpleDateFormat df = new SimpleDateFormat(" dd,hh:mm:ss");
        Date timeRemaining = //TODO: figure out how you calculate your remaining time
        if(tv1 != null) {
            tv1.setText(df.format(timeRemaining));
        }
    }

    @Override
    public void onFinish() {
        if(tv1 != null && tv2!= null) {
            tv1.setText("Completion time over");
            tv2.setVisibility(View.GONE);
        }
    }
}