我有一个关于在Android几秒钟后清除recyclerview的问题。我尝试在线程中执行此操作,但对我不起作用。我用了这段代码
public void cleardbView()
{
final Thread countdown = new Thread() {
@Override
public void run() {
try {
Thread.sleep(15000);
runOnUiThread(new Runnable() {
@Override
public void run() {
recyclerView = (RecyclerView) findViewById(R.id.dbView);
arrayList.clear();
layoutManager = new LinearLayoutManager(MainActivity.this);
return;
}
});
} catch (InterruptedException e) {
}
return;
}
};
countdown.start();
}
我在添加数据的部分中使用此代码。该方法在所有条件下都太大了,因此我只在这里放置伪代码(我试图尽可能地简化它:D)
if(conditions)
{
insertData();
}
if(inserted)
{
cleardbView();
}
答案 0 :(得分:1)
清除列表arrayList.clear();
后需要使用yourAdapter.notifyDataSetChanged();
Adapter.notifyDataSetChanged();
尝试一下
public void cleardbView()
{
final Thread countdown = new Thread() {
@Override
public void run() {
try {
Thread.sleep(15000);
runOnUiThread(new Runnable() {
@Override
public void run() {
recyclerView = (RecyclerView) findViewById(R.id.dbView);
arrayList.clear();
layoutManager = new LinearLayoutManager(MainActivity.this);
yourAdapter.notifyDataSetChanged();
return;
}
});
} catch (InterruptedException e) {
}
return;
}
};
countdown.start();
}
答案 1 :(得分:1)
这是对问题的更好且更短的实现,并且处理程序是后台线程,可让您与UI进行通信// //在此处查看有关线程和处理程序Handler vs AsyncTask vs Thread
的更多说明Handler handler = new Handler();
int delay = 2000; //milliseconds
handler.postDelayed(new Runnable(){
public void run(){
RecyclerViewAdapterDataList.clear();
RecyclerViewAdapter.notifyDataSetChanged();
}
}, delay);