更改所有项目后如何保持onClickListener?或如何通过线程更新将onClickListener设置到RecyclerView中的所有项目?

时间:2019-07-08 03:14:58

标签: android multithreading refresh recycler-adapter

我想制作一个recyclerView,它可以使用onClickListener显示实时更改的数据项。

这是我的代码:(RecyclerView的自定义适配器)

    public MyRecyclerAdapter(ArrayList<Procedure> procs){
        this.procedures = procs;


        final Handler handler = new Handler(){
            public void handleMessage(Message msg){
                updateList();
            }
        };

        Timer timer = new Timer(true);
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Message msg = handler.obtainMessage();
                handler.sendMessage(msg);
            }

            @Override
            public boolean cancel() {
                return super.cancel();
            }
        };
        timer.schedule(timerTask, 0, 1);
    }
...
    public void updateList(){
        if(procedures != null && procedures.size()>0){
            notifyDataSetChanged();
        }
    }

updateList()每1毫秒更新一次UI视图元素。效果很好,但是我添加到myRecyclerAdapter的onClickListener无效...

经过几次测试,我认为调用notifyDataSetChanged()可以将原来的onClickListener替换为新的......因为正常的屏幕触摸时间超过1ms,所以当我触摸屏幕时,原来的onClick()消失了。

如何正确地将onClickListener添加到RecyclerView上每隔1毫秒(实时)更新一次的项目?

将更新时间设置为300毫秒〜500毫秒会更好,但是我希望看到更快速的更新,所以请帮助我...我无法在任何地方找到解决方案。

//import things...

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
    public ArrayList<Procedure> procedures;
    public Context mContext;

    public MyRecyclerAdapter(ArrayList<Procedure> procs){
        this.procedures = procs;


        final Handler handler = new Handler(){
            public void handleMessage(Message msg){
                updateList();
            }
        };

        Timer timer = new Timer(true);
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Message msg = handler.obtainMessage();
                handler.sendMessage(msg);
            }

            @Override
            public boolean cancel() {
                return super.cancel();
            }
        };
        timer.schedule(timerTask, 0, 300);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType){
        //skipped
    }


    @Override
    public void onBindViewHolder(final ViewHolder holder, int pos){
        //UI Changes (Which is actually updated)
    }

    public void updateList(){
        if(procedures != null && procedures.size()>0){
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount(){
        return procedures.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        //Elements...
        public ViewHolder(View view){
            super(view);
            //Settings UI elements
            deleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //This listener has to be maintained even after data changed
                    Toast.makeText(mContext, "alert", Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}

0 个答案:

没有答案