0
我正確使用RecyclerView嗎?無論何時更新數據,儘管修改了數據集並調用了相關的方法,但視圖仍然會顯示舊數據。最重要的是,我沒有看到任何動畫,所以我一定在做錯事。RecyclerView沒有正確更新
這裏是代碼中的相關片段:
private void refreshData() {
Utils.hideSoftKeyboard(this);
if (!Utils.isOnline(getApplicationContext())) {
Toast.makeText(getApplicationContext(), R.string.toast_no_conn, Toast.LENGTH_SHORT).show();
return;
}
String stopNumber = mStopEdit.getText().toString();
if (stopNumber.isEmpty()) {
Toast.makeText(getApplicationContext(), R.string.toast_no_stop, Toast.LENGTH_SHORT).show();
return;
}
mResultNoStop.setVisibility(View.GONE);
mResults.setVisibility(View.GONE);
mProgressCircle.setVisibility(View.VISIBLE);
if (!mDataset.isEmpty()) {
int size = mDataset.size();
mDataset.clear();
mAdapter.notifyItemRangeRemoved(0, size);
}
FiveT.getStopData(stopNumber, mRequestQueue, new FiveT.StopDataClientListener() {
@Override
public void onResponse(ApiResult result) {
mProgressCircle.setVisibility(View.GONE);
if (result.getStopResults().size() == 0) {
mResultNoStop.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), R.string.toast_no_data, Toast.LENGTH_SHORT).show();
return;
}
int i = 0;
mStopName.setText(result.getStopName());
for (StopResult res : result.getStopResults()) {
mDataset.add(res);
mAdapter.notifyItemInserted(i++);
}
mResults.setVisibility(View.VISIBLE);
}
});
}
編輯:我初始化RecyclerView
這樣的:
mDataset = new ArrayList<StopResult>();
mRecyclerView = (RecyclerView) findViewById(R.id.results_recycler_view);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerViewAdapter(mDataset, getApplicationContext());
mRecyclerView.setAdapter(mAdapter);
也許是一個愚蠢的問題,但是你是否在你的適配器類中綁定了mDataset? – mmark
@urudroid是的,我只在適配器的方法'onBindViewHolder'中(並且只能通過視圖持有者)修改視圖的佈局。 – rubik
你能提供你的recyclerview的初始化嗎?確保你設置了一個[item animator](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#setItemAnimator(android.support.v7.widget.RecyclerView.ItemAnimator) ) – mmark