Los elementos de RecyclerView desaparecen después de cambiar entre fragmentos

Tengo 4 fragmentos en mi aplicación, que gestionó en NavActivity:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_nav); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); viewPager = (ViewPager) findViewById(R.id.viewpager); setupViewPager(viewPager); fab = (FloatingActionButton) findViewById(R.id.fab); // Fab for fragments viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { switch (state) { case ViewPager.SCROLL_STATE_DRAGGING: case ViewPager.SCROLL_STATE_SETTLING: fab.hide(); // Hide animation break; case ViewPager.SCROLL_STATE_IDLE: switch (viewPager.getCurrentItem()) { case 0: fragment1.shareFab(fab); case 1: fragment2.shareFab(fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(NavActivity.this, MapsActivity.class)); } }); break; default: fragment1.shareFab(null); break; } if(viewPager.getCurrentItem() == 0 || viewPager.getCurrentItem() == 1) fab.show(); // Show animation break; } } }); tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); tabLayout.setupWithViewPager(viewPager); } private void setupViewPager(ViewPager viewPager) { ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); adapter.addFragment(new MainFragment(), "Main"); adapter.addFragment(new AboutFragment(), "About"); adapter.addFragment(new CartFragment(), "Cart"); adapter.addFragment(new CatalogFragment(), "Catalog"); viewPager.setAdapter(adapter); } ///////////new class class ViewPagerAdapter extends FragmentPagerAdapter { private final List<Fragment> mFragmentList = new ArrayList<>(); private final List<String> mFragmentTitleList = new ArrayList<>(); public ViewPagerAdapter(FragmentManager manager) { super(manager); } @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } public void addFragment(Fragment fragment, String title) { mFragmentList.add(fragment); mFragmentTitleList.add(title); } @Override public CharSequence getPageTitle(int position) { return mFragmentTitleList.get(position); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); List<Fragment> fragments = getSupportFragmentManager().getFragments(); if (fragments != null) { for (Fragment fragment : fragments) { fragment.onRequestPermissionsResult(requestCode, permissions, grantResults); } } } 

}

Me enfrento a un problema, los elementos desaparecen después de cambiar entre los fragmentos creo que el implemento con RecyclerView Y el viewHolder tiene algo que ver con ello.

Estoy tratando de buscar los datos con volley desde mi servidor, en el fragmento C (frag n º 3), los flujos de datos y todo lo muestran correctamente.

Si cambio a frag B (frag número 2) o D (frag número 4), todavía todo funciona. Pero si el intercambio enfermo a frag A (frag número 1) y de nuevo a frag 3 (donde está mi artículo de la lista), todo va lejos, consigo la pantalla en blanco, incluso si intercambio arriba y abajo todavía los artículos no volverán a descargar otra vez.

Aquí está Frag número 3 con mi RecyclerView:

 private FloatingActionButton mSharedFab; private List<MyProducts> productsList; //Creating Views private RecyclerView recyclerView; private RecyclerView.LayoutManager layoutManager; private RecyclerView.Adapter adapter; //Volley Request Queue private RequestQueue requestQueue; private int requestCount = 1; private static final String URL_INDEX = "http://myserverip/android/product.php?page="; //Tag values to read from json public static final String TAG_IMAGE_URL = "product_img"; public static final String TAG_PRODUCT_SN = "product_serial_num"; public static final String TAG_PRODUCT_TITLE = "product_title"; public static final String TAG_PRODUCT_PRICE = "product_price"; public static final String TAG_PRODUCT_DESCRIPTION = "product_description"; public CartFragment() { // Required empty public constructor } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_cart, container, false); recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView); layoutManager = new LinearLayoutManager(getActivity().getApplicationContext(), LinearLayoutManager.VERTICAL, false); recyclerView.setLayoutManager(layoutManager); recyclerView.setItemAnimator(new DefaultItemAnimator()); recyclerView.setHasFixedSize(true); //Initializing our superheroes list productsList = new ArrayList<>(); requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext()); //Calling method to get data to fetch data getData(); //Adding an scroll change listener to recyclerview if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) { recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (isLastItemDisplaying(recyclerView)) { getData(); } } }); } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { recyclerView.setOnScrollChangeListener( new RecyclerView.OnScrollChangeListener() { @Override public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) { if (isLastItemDisplaying(recyclerView)) { getData(); } } }); } } //initializing our adapter adapter = new CustomAdapter(productsList, getActivity()); //Adding adapter to recyclerview recyclerView.setAdapter(adapter); return view ; } 

Y la clase customAdapter:

 public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> { //Imageloader to load image private ImageLoader imageLoader; private Context context; List<MyProducts> myProducts; public CustomAdapter(List<MyProducts> myProducts, Context context) { super(); this.myProducts = myProducts; this.context = context; } @Override public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.product_list, parent, false); ViewHolder viewHolder = new ViewHolder(v); return viewHolder; } @Override public void onBindViewHolder(CustomAdapter.ViewHolder holder, int position) { if( myProducts != null) { MyProducts myProducts1 = myProducts.get(position); imageLoader = ImageVolley.getInstance(context).getImageLoader(); imageLoader.get(myProducts1.getProductImage(), ImageLoader.getImageListener(holder.imageView, R.drawable.android_store_log, android.R.drawable.ic_dialog_alert)); //Showing data to the views holder.imageView.setImageUrl(myProducts1.getProductImage(), imageLoader); holder.textViewProductTitle.setText(myProducts1.getProductTitle()); holder.textViewProductDescription.setText(myProducts1.getProductDescription()); holder.textViewProductSerialNumber.setText(myProducts1.getProductSn()); holder.textViewProductPrice.setText(myProducts1.getProductPrice()); } } @Override public int getItemCount() { return myProducts.size(); } public class ViewHolder extends RecyclerView.ViewHolder { //Views public NetworkImageView imageView; public TextView textViewProductTitle; public TextView textViewProductDescription; public TextView textViewProductSerialNumber; public TextView textViewProductPrice; //Initializing Views public ViewHolder(View itemView) { super(itemView); imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero); textViewProductTitle = (TextView) itemView.findViewById(R.id.textViewProductTitle); textViewProductDescription = (TextView) itemView.findViewById(R.id.textViewProductDescription); textViewProductSerialNumber = (TextView) itemView.findViewById(R.id.textViewProductSerialNumber); textViewProductPrice = (TextView) itemView.findViewById(R.id.textViewProductPrice); } } 

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.