簡體   English   中英

RecyclerView上的定期更新項目

[英]Periodic update items on a RecyclerView

我有一個帶有Recycler視圖處理卡的活動。 我有一個SwipeRefreshLayout,所以當我滑動它時,會使用新內容更新RecyclerView。 到現在為止還挺好。

但是,我想每隔X秒更新RecyclerView,因此,例如,如果我在打開活動recylcerview的情況下忘了滑動它,那么它也會自行更新。 為此,我想這樣的事情:(我省略了必要的代碼)。

我的主要活動(包含recyclerview)安排了一個作業,如下所示:

private void scheduleJob() {

    ComponentName serviceName = new ComponentName(this, MyJobService.class);
    JobInfo jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setRequiresDeviceIdle(false)
            .setRequiresCharging(false)
            .setPeriodic(3000)
            //.setOverrideDeadline(400) // Remove comment for faster testing.
            .build();

    JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
    int result = scheduler.schedule(jobInfo);

    if (result == JobScheduler.RESULT_SUCCESS) {
        Toast.makeText(this,"Start", Toast.LENGTH_LONG).show();
    } } }

在MyJobService內部,我通過廣播到我的Activity,以使其必須更新RecylcerView的內容。 負責接收廣播的是一個內部類,如下所示:

public static class MyReceiver extends BroadcastReceiver{


    @Override
    public void onReceive(Context context, Intent intent) {

        DataSource dataSource = new DataSource(context);
        items = dataSource.getItems(); // can't do this due to outter class variable
    adapter.refresh(ítems); // can't do this due to outter class variable
        Toast.makeText(context,"event",Toast.LENGTH_SHORT).show();

    }
}

問題出在我的主Activity上,我擁有兩個私有變量,分別稱為items(即ArrayList)和adapter(即recylcerview的適配器),我將項目傳遞給適配器以更新recyclerView內容。內部類我無法訪問外部類變量。 做這樣的事情的正確方法是什么? 我想我太混亂了,我想必須有一種最簡單,更直接的方法來完成我想要的事情。

非常感謝你

好吧,原來我把事情弄得太復雜了。 可以使用像這樣的簡單Handler來完成:

  // Create the Handler object (on the main thread by default)
    Handler handler = new Handler();
    // Define the code block to be executed
    private Runnable runnableCode = new Runnable() {
        @Override
        public void run() {
            // Do something here on the main thread
            Toast.makeText(getApplicationContext(),"Update",Toast.LENGTH_SHORT).show();
            ItemsDS itemsDS = new ItempsDS(getApplicationContext());
            items = itemsDS.getItems();
            adapter.clear();
            adapter.addAll(items);
            handler.postDelayed(runnableCode, 1000);
        }
    };

有兩種方法可以執行此操作:

首先是@akshayBhat提到的,您可以通過從BroadcastReceiver類中刪除static關鍵字來實現:

ArrayList<String> items;
Adapter adapter;
public class MyReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {

        DataSource dataSource = new DataSource(context);
        items = dataSource.getItems(); // can't do this due to outter class variable
        adapter.refresh(ítems); // can't do this due to outter class variable
        Toast.makeText(context, "event", Toast.LENGTH_SHORT).show();

    }
}

其次是將當前活動狀態的引用傳遞給BroadCast接收器,並使用該引用訪問那里的ClassVariables。

ArrayList<String> items;
Adapter adapter;
public static class MyReceiver extends BroadcastReceiver {

    AboutUs mContext;

    @Override
    public void onReceive(Context context, Intent intent) {

        mContext = (AboutUs)context;
        DataSource dataSource = new DataSource(context);
        mContext.items = dataSource.getItems(); // can't do this due to outter class variable
        mContext.adapter.refresh(ítems); // can't do this due to outter class variable
        Toast.makeText(context, "event", Toast.LENGTH_SHORT).show();

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM