therealshabi / AutoImageFlipper

Auto Scrolling Image Pager with Pager Indicator and Text
Apache License 2.0
106 stars 25 forks source link

ViewPager scrolls back immediately to the first item when there are only two items #38

Closed Sonphil closed 4 years ago

Sonphil commented 4 years ago

When the user scrolls to the second item, if the layout has only two FlipperViews, the ViewPager scrolls back immediately to the first item. The ViewPager should remain at that position (or switch after a certain amount of time if auto cycle is enabled).

Here is an example: ezgif-6-444070d61c5f

This problem doesn't occur when there are more than two FlipperViews. ezgif-6-153ac05e0290

Version of the library used: 1.6.0

therealshabi commented 4 years ago

Hi @Sonphil Thanks for reporting this issue, will surely look into this. Meanwhile, if you want to fix this, you can contribute to it as well.

Sonphil commented 4 years ago

@therealshabi I made a small change and opened a pull request: #39

therealshabi commented 4 years ago

Merged your PR into the dev branch. Will merge and release from the master after testing the implementation. Thanks for your contribution

Poornima-Dixit commented 4 years ago

How to set Multiple images in viewpager??

Poornima-Dixit commented 4 years ago

view.setImage(url[i], new Function2<ImageView, Object, Unit>() { @Override public Unit invoke(ImageView imageView, Object image) { Picasso.get().load((String) image).into(imageView); return Unit.INSTANCE; } }); } catch (MalformedURLException e) { e.printStackTrace(); }

Poornima-Dixit commented 4 years ago

The above code not working

therealshabi commented 4 years ago

@Poornima-Dixit is it showing any error? What exactly is happening? Can you please share the stack trace?

Poornima-Dixit commented 4 years ago

view.setImage(url[i], new Function2<ImageView, Object, Unit>() { @override public Unit invoke(ImageView imageView, Object image) { Picasso.get().load((String) image).into(imageView); return Unit.INSTANCE; } }); } catch (MalformedURLException e) { e.printStackTrace(); } The above code contains ERROR when I am running It's giving error on Function2 & Unit

Poornima-Dixit commented 4 years ago

How to solve can you plzzz help????

Poornima-Dixit commented 4 years ago

@Sonphil

Poornima-Dixit commented 4 years ago

@therealshabi

therealshabi commented 4 years ago

@Poornima-Dixit Are you using Java 8 in your project, because this will work if your project is running on Java8?

Poornima-Dixit commented 4 years ago

How to check that??Where it is mention in android studio?

Poornima-Dixit commented 4 years ago

I am using Java 8 still giving error

Poornima-Dixit commented 4 years ago

Have you used Kotlin??

therealshabi commented 4 years ago

@Poornima-Dixit this library also has a Kotlin support, so you can use Kotlin syntax as well (for syntax please refer the Readme). Having said that I'm curious why it is not working for you in Java. Can you please share the codebase or a test code for me if you can in which this code is not working. I'll try to debug and present the solution to you as soon as I can

Poornima-Dixit commented 4 years ago

@therealshabi Yes sure The program is running when I am adding only one image MainActivity.java File:- protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

    final FlipperLayout flipperLayout = findViewById(R.id.flipperLayoutTwo);

    String[] url = {"https://blog.eap.ucop.edu/wp-content/uploads/2016/01/Julie-Huang-27.jpg",
            "https://source.unsplash.com/random",
            "https://i.pinimg.com/originals/18/40/72/184072abb72399c23ab635faaa0a94ad.jpg"
    };
    for (int i = 0; i < url.length; i++) {
        FlipperView view = new FlipperView(getBaseContext());
        view.setDescription("Cool" + (i + 1))
                .setDescriptionBackgroundColor(Color.TRANSPARENT)
                .resetDescriptionTextView();
        flipperLayout.setCircleIndicatorHeight(60);
        flipperLayout.setCircleIndicatorWidth(200);
        flipperLayout.removeCircleIndicator();
        flipperLayout.showCircleIndicator();
        view.setOnFlipperClickListener(new FlipperView.OnFlipperClickListener() {
            @Override
            public void onFlipperClick(@NotNull FlipperView flipperView) {
                Toast.makeText(getBaseContext(),
                        "Here " + (flipperLayout.getCurrentPagePosition() + 1), Toast.LENGTH_SHORT).show();
            }
        });

     view.setImageDrawable(R.drawable.lean);// this is only one image

      flipperLayout.addFlipperView(view);
    }

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
            .setDrawerLayout(drawer)
            .build();
 //   NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
   // NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);

// NavigationUI.setupWithNavController(navigationView, navController); }

Poornima-Dixit commented 4 years ago

@therealshabi Now the Problem is that when I am adding below code its giving error:- try { view.setImage(url[i], new Function2<ImageView, Object, Unit>() { @Override public Unit invoke(ImageView imageView, Object image) { Picasso.get().load((String) image).into(imageView); return Unit.INSTANCE; } }); } catch (MalformedURLException e) { e.printStackTrace(); }

Poornima-Dixit commented 4 years ago

@therealshabi

therealshabi commented 4 years ago

@Poornima-Dixit I'll check this asap and will revert

Poornima-Dixit commented 4 years ago

@therealshabi Yes sure please do it on ASAP.IT's urgent

therealshabi commented 4 years ago

@Poornima-Dixit The images that you're trying to load, are they a drawable, url or what?

Poornima-Dixit commented 4 years ago

Yes,they are drawable

Poornima-Dixit commented 4 years ago

@therealshabi

therealshabi commented 4 years ago

For drawable set image like:

try {
    view.setImage(drawableList[i], new Function2<ImageView, Object, Unit>() {
        @Override
        public Unit invoke(ImageView imageView, Object image) {
            imageView.setImageDrawable((Drawable) image);
            return Unit.INSTANCE;
        }
    });
} catch (MalformedURLException e) {
    e.printStackTrace();
}
Poornima-Dixit commented 4 years ago

@therealshabi Yes But it is giving error on"Function2" & "Unit" Where is these classes available?

therealshabi commented 4 years ago

Ohh! Are you having Kotlin dependency in your project, these are Java version of Kotlin callback method

Poornima-Dixit commented 4 years ago

No I haven't added here ,How to add it??

therealshabi commented 4 years ago

Can you please google it, it'll be a simple process.

Poornima-Dixit commented 4 years ago

@therealshabi Yes I solved it.Just One Query I had

Poornima-Dixit commented 4 years ago

@therealshabi May I Ask one query?

Poornima-Dixit commented 4 years ago

@therealshabi Thank you so much for your support answers.Just last one error is "setImage"giving error.How to solve it

therealshabi commented 4 years ago

Can you please specify clearly, what error it is giving?

Poornima-Dixit commented 4 years ago

error

Poornima-Dixit commented 4 years ago

@therealshabi above is the error

therealshabi commented 4 years ago

This is weird, can you please clean and build the app and check again?

Poornima-Dixit commented 4 years ago

Ok