嗨,我认为我的问题与此notifyDataSetChanged not working on RecyclerView有关 我试图从异步任务刷新我的布局,但当我调用通知datasetchange没有任何反应,但如果我创建一个适配器的新实例并调用notifydatasetchanged然后它确实工作香港专业教育学院尝试了一些事无济于事,并想知道我做错了什么,
我像这样引用它
public ListAdapter cardAdapter;
然后在onCreate中我初始化所有与我的回收器视图有关的事情(这里需要的唯一一点就是我如何初始化我的适配器,但为了清楚起见,添加了与我的回收器视图有关的一切)
staggeredGridLayoutManagerVertical = new
customLinearLayoutManager(this,customLinearLayoutManager.VERTICAL,false);
recyclerView = (RecyclerView) findViewById(R.id.list_view);
OnItemTouchListener itemTouchListener = new
FinalListActivity.OnItemTouchListener() {
@Override
public void onCardClick(View view, int position) {
//removeAt(position);
list.remove(position);
cardAdapter.notifyItemRemoved(position);
}
@Override
public void onCardLongClick(View view, int position) {
//clearGrid();
}
};
cardAdapter = new ListAdapter(list,this,itemTouchListener);
recyclerView.setLayoutManager(staggeredGridLayoutManagerVertical);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(cardAdapter);
//cardAdapter.notifyDataSetChanged();
ItemTouchHelper.Callback callback =
new SimpleItemTouchHelperCallback(cardAdapter);
mItemTouchHelper = new ItemTouchHelper(callback);
mItemTouchHelper.attachToRecyclerView(recyclerView);
如果我在这些方法之前向列表中添加项目,那么我的回收站视图会显示信息,但我想要的是获取异步任务中的信息并在onPostExecute中填充列表但是当我调用时
cardAdapter.notifyDataSetChanged();
从onPostExecute没有任何反应,任何帮助?
修改 添加适配器
public class ListAdapter extends
RecyclerView.Adapter<ListAdapter.MyViewHolder>
implements ItemTouchHelperAdapter{
private List<String> cardMakerList;
private OnStartDragListener mDragStartListener;
private FinalListActivity.OnItemTouchListener onItemTouchListener;
public class MyViewHolder extends RecyclerView.ViewHolder{
public EditText cardText;
public ImageView close;
public ContentLoadingProgressBar progress;
public ImageView handleView;
public RelativeLayout cardBack;
public MyViewHolder(View view){
super(view);
cardText = (EditText) view.findViewById(R.id.cardText);
close = (ImageView) view.findViewById(R.id.close);
cardBack =(RelativeLayout)view.findViewById(R.id.cardLayout);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemTouchListener.onCardClick(v, getPosition());
}
});
/*
close.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onItemTouchListener.onCardLongClick(v, getPosition());
return true;
}
});
*/
progress = (ContentLoadingProgressBar)
view.findViewById(R.id.progress);
handleView = (ImageView)view.findViewById(R.id.handle);
}
}
public ListAdapter(List<String> cardMakerList,OnStartDragListener
dragStartListener,FinalListActivity.OnItemTouchListener onItemTouchListener)
{
this.cardMakerList = cardMakerList;
mDragStartListener = dragStartListener;
this.onItemTouchListener = onItemTouchListener;
}
@Override
public boolean onItemMove(int fromPosition, int toPosition) {
if (fromPosition < toPosition){
for (int i = fromPosition; i < toPosition; i++){
Collections.swap(cardMakerList,i,i+1);
}
}else{
for (int i = fromPosition; i > toPosition; i--){
Collections.swap(cardMakerList,i,i-1);
}
}
notifyItemMoved(fromPosition,toPosition);
return true;
}
@Override
public void onItemDismiss(int position) {
cardMakerList.remove(position);
notifyItemRemoved(position);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view_holder, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position)
{
String cardString = cardMakerList.get(position);
holder.cardText.setText(cardString);
holder.handleView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) ==
MotionEvent.ACTION_DOWN) {
mDragStartListener.onStartDrag(holder);
}
return false;
}
});
holder.close.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//cardMakerList.remove(position);
return false;
}
});
}
@Override
public int getItemCount(){
return cardMakerList.size();
}
}
答案 0 :(得分:2)
可能会出现这种情况的原因有很多,其中一个原因是list
变量没有被更新,因为在某些时候它失去了它的主要实例并且适配器不再监听它。在这种情况下,您应该在适配器中实现类似
public void refreshMyList(List<String> list){
this.cardMakerList.clear();
this.cardMakerList.addAll(list);
this.notifyDataSetChanged();
}
现在,每次要刷新RecyclerView时,只需使用新列表调用此方法。