Alper Aslan Alper Aslan -4 years ago 158
Android Question

How can I change Viewpager image sources (different albums)?

I have 4 albums which each has 20 images. As default first albums images loading. I want to switch between images. I use viewpager for sliding but I can't change the source of images to other albums images.

How can I change the resouorceIDs list with the drawer item selection or anotherway?

Here is my MainActivty.java. I try to change resourceIDs to r1,r2,r3,r4,r5...

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

private BitmapFactory.Options options;
private ViewPager viewPager;
private FragmentStatePagerAdapter adapter;
private ArrayList<Integer> images;

public static int[] resourceIDs = new int[] {
R.mipmap.p19,
R.mipmap.p18,
R.mipmap.p17,
R.mipmap.p16,
R.mipmap.p15,
R.mipmap.p14,
R.mipmap.p13,
R.mipmap.p12,
R.mipmap.p11,
R.mipmap.p10,
R.mipmap.p9,
R.mipmap.p8,
R.mipmap.p7,
R.mipmap.p6,
R.mipmap.p5,
R.mipmap.p4,
R.mipmap.p3,
R.mipmap.p2,
R.mipmap.p1,
R.mipmap.p0
} ;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

images = new ArrayList<>();

//find view by id
viewPager = (ViewPager) findViewById(R.id.view_pager);
setImagesData();


// init viewpager adapter and attach
adapter = new ViewPagerAdapter(getSupportFragmentManager(), images);
viewPager.setAdapter(adapter);
//right to left sliding
viewPager.setCurrentItem(adapter.getCount() - 1);
}

private void setImagesData() {
for (int i = 0; i < resourceIDs.length; i++) {
images.add(resourceIDs[i]);
}
}


Here is the drawer code:

private void displaySelectedScreen(int itemId) {

//creating fragment object
Fragment fragment = null;

//initializing the fragment object which is selected
switch (itemId) {
case nav_album1:
fragment = new Album1();
images = new ArrayList<>();
resourceIDs = new int[] {
R.mipmap.p5,
R.mipmap.p4,
R.mipmap.p3,
R.mipmap.p2,
R.mipmap.p1};


adapter.notifyDataSetChanged();

adapter = new ViewPagerAdapter(getSupportFragmentManager(), images);
viewPager.setAdapter(adapter);
//setContentView(R.layout.kdaria);
break;
case R.id.nav_album2:
fragment = new Album2();

images = new ArrayList<>();
resourceIDs = new int[] {
R.mipmap.r5,
R.mipmap.r4,
R.mipmap.r3,
R.mipmap.r2,
R.mipmap.r1};


adapter.notifyDataSetChanged();

adapter = new ViewPagerAdapter(getSupportFragmentManager(), images);
viewPager.setAdapter(adapter);
break;

case R.id.nav_album3:
fragment = new Album3();

images = new ArrayList<>();
resourceIDs = new int[] {
R.mipmap.s5,
R.mipmap.s4,
R.mipmap.s3,
R.mipmap.s2,
R.mipmap.s1};


adapter.notifyDataSetChanged();

adapter = new ViewPagerAdapter(getSupportFragmentManager(), images);
viewPager.setAdapter(adapter);

break;
}

Answer Source

So this is your code.

private void displaySelectedScreen(int itemId) { 
    Fragment fragment = null; 
    switch (itemId) { 
        case R.id.nav_album2: 
            fragment = new Album2(); 
            images = new ArrayList<>(); 
            resourceIDs = new int[] { R.mipmap.r5, R.mipmap.r4, R.mipmap.r3, R.mipmap.r2, R.mipmap.r1}; 
            adapter.notifyDataSetChanged(); 
            break;

What are you notifying? You changed nothing about the adapter data.

If you want to simply show different data in the ViewPager, you need to actually update the images list. Then you can notify.

private void displaySelectedScreen(int itemId) { 
    images.clear(); // Always clear
    List<Integer> newImages=new ArrayList<>();

    switch (itemId) { 
        case R.id.nav_album2: 
            newImages = Arrays.asList(
                R.mipmap.r5, R.mipmap.r4, R.mipmap.r3, R.mipmap.r2, R.mipmap.r1);
            break;
        case R.id_nav_album3:
            newImages = Arrays.asList(...);
            break;
        }

    images.addAll(newImages);
    adapter.notifyDataSetChanged(); // always notify
}

Takeaways:

  1. Don't make images = new ArrayList<>() because your adapter no longer "sees" images.

  2. You only need one adapter. You update the one reference to images, and you can call notifyDataSetChanged() properly.

  3. You don't need to repeat so much code of setting and creating adapters.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download