简体   繁体   中英

Using an viewPager to change the background of my imageView in my fragment

I'm trying to adapt a pre-existing application I have made to make use of a navigation bar and a viewPager.

When I swipe left/right I want an imageView within my fragment to change to a different drawable stored in an array. However, all of the tutorials I have seen have told me that viewPagers are very difficult to use within fragments, and I may need to create many different layout files.

Is a viewPager the best way to do this? If anyone has any suggestions or needs any more information that would be great!

Sure ViewPager can be tricky, but if you want a "paging" effect, where a swipe moves image A off the screen and image B onto the screen, then ViewPager is the way to go. Don't be intimidated. I use ViewPager a lot.

Now, if you want the swipe to just change the image while it stays stationary on the screen, then you should look to another method. But this approach doesn't give the user that nice feedback that their gesture is actually doing something.

Go through the tutorials & put some code together; when you get to a point where you're stuck, post your code here and the community can help you from there.

It is not hard at all, actually. Here are the two ways I usually implement it:

  1. ViewPager + Fragments, which you may need to define each fragment and each layout.
  2. ViewPager + ViewPagerIndicator , much easier to do and you only have to create one fragment and adapter to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM