简体   繁体   English

使用viewPager更改片段中imageView的背景

[英]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. 我正在尝试改编我已经利用导航栏和viewPager制作的现有应用程序。

When I swipe left/right I want an imageView within my fragment to change to a different drawable stored in an array. 当我向左/向右滑动时,我希望片段中的imageView更改为存储在数组中的其他可绘制对象。 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. 但是,我看过的所有教程都告诉我,在片段中很难使用viewPagers,并且我可能需要创建许多不同的布局文件。

Is a viewPager the best way to do this? viewPager是做到这一点的最佳方法吗? 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. 当然, ViewPager可能很棘手,但是如果您想要“分页”效果,即通过滑动将图像A从屏幕上移出并将图像B移到屏幕上,则可以使用ViewPager Don't be intimidated. 不要被吓到 I use ViewPager a lot. ViewPager使用ViewPager

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. ViewPager +片段,您可能需要定义每个片段和每个布局。
  2. ViewPager + ViewPagerIndicator , much easier to do and you only have to create one fragment and adapter to implement. ViewPager + ViewPagerIndicator ,更容易实现,您只需创建一个片段和适配器即可实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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