不断更新Listview连续渲染按钮

时间:2015-11-11 21:13:25

标签: android listview

我昨天和今天在互联网上搜索了一个更好的解决方案,但是找不到它。
我尝试在我的getViews中使用计时器,但它们在展开/折叠或滚动后重置 我试图将计时器的时间存储在我的运动员物体中,但仍然发生了RecyclerView问题。

问题:

基本上,我正在尝试创建具有启动/停止/重置按钮的ListViews以及出现在TextView中的计时器。
截至目前,我一直在呼叫notifyDatasetChanged() 问题是,除非我将刷新率降低到500毫秒,否则按钮需要2-3次点击才能工作。

我目前正在运行一个特定于每个运动员的秒表,所以只要我停留在相同的片段上,每个运动员计时器都是特定于每个运动员并且它们完美地运行。
这是因为屏幕快速更新并且每500分钟获得每位运动员的秒表时间 将刷新率降低到500毫秒的问题是我的计时器每隔0.5秒显示一次,然后看起来很糟糕。

使我的按钮更具响应性的任何解决方案。

片段:(刷新列表视图)

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    handler = new Handler();
    handler.post(runnable);

}
Runnable runnable = new Runnable(){
    @Override
    public void run() {
        adapterWhiteMales.notifyDataSetChanged();
        handler.postDelayed(runnable, 250);
    }
};

GetChildView :(我的自定义适配器)

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View view, ViewGroup parentView)
{

    final ViewHolderItem viewHolderItem;
    final listViewParentAthlete parent = parents.get(groupPosition);
    final Athlete athlete = parent.getChildren().get(childPosition);

    int viewType = getGroupType(groupPosition);

        // If child view has never been created, initialize variables
        if (view == null) {

            viewHolderItem = new ViewHolderItem();
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.row_explistview_child_meet_athletes, parentView, false);

            viewHolderItem.childSplitTimes = (TextView) view.findViewById(R.id.txtSplitTime);
            viewHolderItem.btnStartLap = (Button) view.findViewById(R.id.btnStartLap);
            viewHolderItem.btnStop = (Button) view.findViewById(R.id.btnStop);
            viewHolderItem.btnReset = (Button) view.findViewById(R.id.btnReset);
            viewHolderItem.btnSave = (Button) view.findViewById(R.id.btnSave);
            viewHolderItem.txtStopWatch = (TextView) view.findViewById(R.id.txtStopWatch);

            view.setTag(viewHolderItem);
        } else {
            // Use already initialized variables
            viewHolderItem = (ViewHolderItem) view.getTag();
        }

        // If you click the Start/Lap button, it starts the timer and adds splits
        viewHolderItem.btnStartLap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("", "Start Lap Button Clicked!");
                if (athlete.getEvents().get(finalEventPosition).getIsRunning() == false) {
                    Log.e("", "StopWatch Started!");
                    athlete.getEvents().get(finalEventPosition).startStopWatch();
                }
            }
        });
        // If you click the Stop/Reset button, it stops all times and last split. Working
        viewHolderItem.btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("", "Stop Button Clicked!");
                athlete.getEvents().get(finalEventPosition).stopStopWatch();
                athlete.getEvents().get(finalEventPosition).setIsRunning(false);
            }
        });
        viewHolderItem.btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("", "Reset Button Clicked!");
                athlete.getEvents().get(finalEventPosition).resetStopWatch();
                athlete.getEvents().get(finalEventPosition).setIsRunning(false);
            }
        });
        // If you click the Save button, it sends total time and splits to the database.
        viewHolderItem.btnSave.setOnClickListener(new View.OnClickListener()     {
            @Override
            public void onClick(View view) {
                Log.e("", "Save Button Clicked!");

            }
        });

    return view;
}

1 个答案:

答案 0 :(得分:0)

只需使用一些标志即可停止刷新。在你的例子中是这样的。 这段代码应该有效。 (用于在适配器使用接口中为按钮设置OnClickListener)

public bolean isWork = true;

....

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    handler = new Handler();
    handler.post(runnable);

}
Runnable runnable = new Runnable(){
    @Override
    public void run() {
        if (isWork) {
          adapterWhiteMales.notifyDataSetChanged();
          handler.postDelayed(runnable, 250);
        }
    }
};

....

myButton.setOnClickListener (new OnClickListener () {
   void OnClick () {
       isWork = false;
   }

});