问题
hi i think my issue is related to this notifyDataSetChanged not working on RecyclerView im trying to refresh my layout from an async task but when im calling notify datasetchanged nothing happens, but if i create a new instance of the adapter and call notifydatasetchanged then it does work ive tried a few things to no avail and wonder what im doing wrong,
i reference it like this
public ListAdapter cardAdapter;
then in onCreate i initialise everything to do with my recycler view, (the only bit thats needed here is how i initialise my adapter but for clarity ill add everything to do with my recycler view)
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);
if i add items to the list before these methods then my recycler view shows the information but what i want is to grab the information in an async task and populate the list in onPostExecute but when i call
cardAdapter.notifyDataSetChanged();
from onPostExecute nothing happens, any help?
EDIT adding the adapter
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();
}
}
回答1:
There is many reasons that this might occur, and one of them is the list
variable is not being updated because at some point it loses it's primary instance and the adapter no longer listens to it. In this case you should implement in your adapter a method like
public void refreshMyList(List<String> list){
this.cardMakerList.clear();
this.cardMakerList.addAll(list);
this.notifyDataSetChanged();
}
Now every time you want to refresh the RecyclerView just call this method with the new list.
来源:https://stackoverflow.com/questions/43142831/notifydatasetchanged-not-working-from-async-task