There is a fragment code where for recyclerView I put my adapter rvDownloadAdapter If I fill the array before the adapter is initialized, then the elements are displayed in recyclerView works. As for example, I initially add one element to the array.

Then I call AsyncTask where I get data from the PHPFunction and in the PHPFunction class PHPFunction fill in the array and return it. But when onPostExecute update the adapter in onPostExecute it is not updated, I initially thought that the array was empty, but then I log the size of the array and it shows me that the size is four, ie array is not empty.

Here is the fragment code:

 public class DownloadCentrFragment extends Fragment { Context context; Resources resources; TabLayout tabLayout; ViewPager viewPager; List<View> pages = new ArrayList<View>(); List<String> titles = new ArrayList<String>(); ArrayList<itemGetServerList> itemGetServerInfos = new ArrayList<itemGetServerList>(); RVDownloadAdapter rvDownloadAdapter = null; public DownloadCentrFragment() {} public DownloadCentrFragment setInstance() { DownloadCentrFragment fragment = new DownloadCentrFragment(); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_download_centr, container, false); context = this.getActivity().getApplicationContext(); resources = this.getActivity().getResources(); tabLayout = (TabLayout) view.findViewById(R.id.tabLayout); viewPager = (ViewPager) view.findViewById(R.id.viewPager); titles.add(resources.getString(R.string.text_UsersList)); titles.add(resources.getString(R.string.text_YouList)); View v1 = inflater.inflate(R.layout.fragment_home_download_centr, null); EditText SearchText = (EditText) v1.findViewById(R.id.SearchText); ImageView Search = (ImageView) v1.findViewById(R.id.Search); ImageView Filtr = (ImageView) v1.findViewById(R.id.Filtr); RecyclerView recyclerView = (RecyclerView) v1.findViewById(R.id.recycler_view); LinearLayoutManager llm = new LinearLayoutManager(view.getContext()); recyclerView.setLayoutManager(llm); itemGetServerInfos.add(new itemGetServerList("Таблица умножения на два", 228, 5, "7-9", "Таблица на два от тред до семи", "","","arif","", null)); rvDownloadAdapter = new RVDownloadAdapter(itemGetServerInfos, view.getContext()); recyclerView.setAdapter(new AlphaInAnimationAdapter(rvDownloadAdapter)); ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(view.getContext(),pages,titles); pages.add(v1); viewPager.setAdapter(viewPagerAdapter); tabLayout.setupWithViewPager(viewPager); new LoadInfoTask().execute(); return view; } class LoadInfoTask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... params) { itemGetServerInfos = new PHPFunction(resources, context).getInList(0,30); return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); rvDownloadAdapter.notifyDataSetChanged(); Log.d("myLog", itemGetServerInfos.size() + " sizes"); } } } 

rvDownloadAdapter

 public class RVDownloadAdapter extends RecyclerView.Adapter<RVDownloadAdapter.ViewHolder> { ArrayList<itemGetServerList> itemGetServerInfo = new ArrayList<itemGetServerList>(); Context context; public RVDownloadAdapter (ArrayList<itemGetServerList> itemGetServerInfoLoad, Context context) { this.itemGetServerInfo = itemGetServerInfoLoad; this.context = context; } public static class ViewHolder extends RecyclerView.ViewHolder { View view; CardView cardView; ImageView icon; TextView NameList; TextView Des; TextView Rating; TextView Age; ViewHolder(View itemView) { super(itemView); cardView = (CardView) itemView.findViewById(R.id.card_view); icon = (ImageView) itemView.findViewById(R.id.icon); NameList = (TextView) itemView.findViewById(R.id.nameList); Des = (TextView) itemView.findViewById(R.id.des); Rating = (TextView) itemView.findViewById(R.id.rating); Age = (TextView) itemView.findViewById(R.id.age); view = itemView; } } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_download_card, parent, false); ViewHolder vh = new ViewHolder(v); return vh; } @Override public void onBindViewHolder(final ViewHolder holder, final int position) { final itemGetServerList infoData = itemGetServerInfo.get(position); holder.NameList.setText(itemGetServerInfo.get(position).NameList); holder.Des.setText(itemGetServerInfo.get(position).Des); holder.Rating.setText(itemGetServerInfo.get(position).Rating + ""); holder.Age.setText(itemGetServerInfo.get(position).Age); switch (itemGetServerInfo.get(position).Tag) { case "orph": holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_orphographic)); break; case "arif": holder.icon.setImageDrawable(context.getResources().getDrawable(R.drawable.icon_arithmetic)); break; } } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } @Override public int getItemCount() { return itemGetServerInfo.size(); } } 
  • one
    What is the rvDownloadAdapter class? You have designated the class AlphaInAnimationAdapter as an adapter, it is for the mind that you need to do notifyDataSetChanged() so that the list is updated. As data for the AlphaInAnimationAdapter adapter assigned by RecyclerView you are using another adapter or I do not understand your logic at all. - pavlofff
  • Show AlphaInAnimationAdapter and RVDownloadAdapter - Shwarz Andrei
  • @pavlofff 'AlphaInAnimationAdapter' is a component from the github.com/wasabeef/recyclerview-animators library for animation, and 'rvDownloadAdapter' is a normal adapter for 'recyclerview' - Andrei
  • @pavlofff updated - Andrew
  • one
    Well, did you read the instructions for the library that you use in step 2 , especially the highlighted note? - pavlofff

1 answer 1

I don’t know if this example will solve your particular problem, since you are using a third-party library in the adapter, the description of which says that the notifyDataSetChanged() method does not work when using animation.

The RecyclerView adapter does have some kind of problem and the notifyDataSetChanged() method does not always work correctly, the developer himself recommends using this method in extreme cases, and in return offers a whole set: notifyItemChanged() , notifyItemInserted() , notifyItemRemoved() , notifyItemRangeChanged() , notifyItemRangeInserted() , notifyItemRangeRemoved() , which does not always solve the required tasks and sometimes the opal method is needed more than ever.

In general, I used such a “crutch” and it worked properly - to force the changed data to the adapter and only then update the list. To do this, we make a simple method in the adapter (here, dataChanged() ), which updates the data, and then the list itself.

I will give my decision based on the code of your adapter, since it is available:

 public class RVDownloadAdapter extends RecyclerView.Adapter<RVDownloadAdapter.ViewHolder> { ArrayList<itemGetServerList> itemGetServerInfo; Context context; public RVDownloadAdapter (ArrayList<itemGetServerList> itemGetServerInfoLoad, Context context) { itemGetServerInfo = itemGetServerInfoLoad; this.context = context; } ..... // прочий код адаптера public dataChanged( ArrayList<itemGetServerList> itemGetServerInfoLoad){ itemGetServerInfo = itemGetServerInfoLoad; notifyDataSetChanged(); } } 

when data needs to be updated, this method is used, to which new data is directly transmitted:

 rvDownloadAdapter.dataChanged(itemGetServerInfos);