我想在 RecyclerView 上制作实时变化的数据

i want to make realtime changing data On RecyclerView

我正在尝试在我的 RecyclerView 中每 1 秒刷新一次,但我不知道如何使用我的 API 进行刷新,当我想要的 api 发生某些变化时在我的 RecylerView 中进行更改而无需滑动器,或者我想让滑动器每 1 秒滑动一次..所以这是我的代码:

public class InformationNow extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
    private RequestQueue requestQueue;
    private SwipeRefreshLayout refresh;
    private ArrayList<TrashCanModel> trashCanModels = new ArrayList<>();
    private JsonArrayRequest arrayRequest;
    private Dialog dialog;
    private RecyclerView recyclerView;
    private  InformationNowAdapter informationNowAdapter ;
    private String url = "http://192.168.1.34:9090/TrashCanData/lastrow";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information_now);
        refresh = (SwipeRefreshLayout) findViewById(R.id.Swiper);
        recyclerView = (RecyclerView) findViewById(R.id.Now);

        dialog = new Dialog(this);


        refresh.setOnRefreshListener(this);
        refresh.post(new Runnable() {
            @Override
            public void run() {
                trashCanModels.clear();
                getData();
            }
        });


    }

    private void getData() {
        refresh.setRefreshing(true);

        arrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                JSONObject jsonObject = null;
                for (int i = 0; i < response.length(); i++) {
                    try {
                        //JSONArray jsonArray = response.getJSONArray("users");
                        jsonObject = response.getJSONObject(i);
                        TrashCanModel cat = new TrashCanModel();
                        cat.setDistance(jsonObject.getInt("distance"));
                        cat.setTemperature(jsonObject.getInt("temperature"));
                        cat.setHumidity(jsonObject.getInt("humidity"));
                        Log.e("*********", cat.getDistance().toString());
                        Log.e("*********", cat.getTemperature().toString());
                        Log.e("*********", cat.getHumidity().toString());

                        trashCanModels.add(cat);
                    } catch (JSONException e) {
                        e.printStackTrace();

                    }
                }
                adapterPush(trashCanModels);
                refresh.setRefreshing(false);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        requestQueue = Volley.newRequestQueue(InformationNow.this);
        requestQueue.add(arrayRequest);
    }

    private void adapterPush(ArrayList<TrashCanModel> trashCanModels) {

        informationNowAdapter = new InformationNowAdapter(this, trashCanModels);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(informationNowAdapter);

    }
 
    @Override
    public void onRefresh() {
        trashCanModels.clear();
        getData();

    }

}

和我的适配器:

public class InformationNowAdapter extends RecyclerView.Adapter<InformationNowAdapter.MyViewHolder>{


    private Context context ;
    private ArrayList<TrashCanModel> trashCanModels;
    private String url = "";
//


    public InformationNowAdapter (Context context , ArrayList<TrashCanModel> trashCanModels) {
        this.context = context;
        this.trashCanModels=trashCanModels;
    }




    @NonNull
    @Override
    public InformationNowAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view ;

        LayoutInflater layoutInflater = LayoutInflater.from(context);
        view = layoutInflater.inflate(R.layout.trashcan_list , parent , false);
        return new InformationNowAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InformationNowAdapter.MyViewHolder holder, int position) {
        //holder.id.setText("#"+String.valueOf(position+1));
        holder.distance.setText(trashCanModels.get(position).getDistance().toString());
        holder.temperature.setText(trashCanModels.get(position).getTemperature().toString());
        holder.humidity.setText(trashCanModels.get(position).getHumidity().toString());

    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView  distance , temperature , humidity ;


        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            distance = (TextView) itemView.findViewById(R.id.distance);
            temperature = (TextView) itemView.findViewById(R.id.temperature);
            humidity = (TextView) itemView.findViewById(R.id.humidity);
//
        }
    }

}

那么我怎样才能每 1 秒刷新一次数据呢!

您可以使用定时器来做到这一点

Timer repeatTask = new Timer();
        repeatTask.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                trashCanModels.clear();
                getData();
            }
        }, 0, 1000);

想停止时调用repeatTask.cancel() 但我认为你应该在 api 更改或使用 firebase 或 socket.io 时使用推送通知