Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView items disappear after switching between fragments

I have 4 fragments in my app, which managed in 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);
        }
    }
}

}

I face one problem, items disappear after switching between fragments i think the implement with RecyclerView And the viewHolder has something to do with it.

Im trying to fetch data with volley from my server, into fragment C (frag num 3), the data streams well everything show correctly.

If i swap to frag B(frag num 2) or D(frag num 4), still everything works. But if ill swap to frag A(frag num 1) and back to frag 3(where is my list item), everything goes away, i get blank screen, even if i swap up and down still the items wont download again.

Here is Frag num 3 with my 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 ;
}

and the customAdapter class:

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);
    }
}
like image 798
2Stoned Avatar asked Jul 10 '16 06:07

2Stoned


1 Answers

Try this:

viewPager.setOffscreenPageLimit(3);

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

setOffscreenPageLimit

like image 162
guichristovao Avatar answered Oct 11 '22 17:10

guichristovao