无限滚动图像ViewPager
根据 Google 的记录,Gallery 类在 API 级别 16 中已被弃用。 不再支持此小部件。其他水平滚动小部件包括支持库中的 HorizontalScrollView 和 ViewPager。所以我使用 ViewPager 作为 Gallery 类的替代品。
我的目标是最终实现一个带有文本描述的无限滚动图像ViewPager。我使用下面的代码来实现带有描述每个图像的文本的图像 ViewPager,但是如何将无限滚动应用于 ViewPager?
我之前没有使用过 ViewPager,因此请尝试提供详细的代码(如果可能)。
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/myimagepager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
custom_pager.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/myimage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="2" />
<TextView
android:id="@+id/image_text"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
ImagePager:
public class ImagePager extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra, stringArray );
ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
private int imageArra[] = { R.drawable.a, R.drawable.b,R.drawable.c,
R.drawable.d,R.drawable.e,R.drawable.f,
R.drawable.g, R.drawable.h, R.drawable.i};
private String[] stringArray = new String[] { "Image a", "Image b","Image c"
"Image d","Image e","Image f",
"Image g","Image h","Image i"};
}
ImagePagerAdapter:
public class ImagePagerAdapter extends PagerAdapter {
Activity activity;
int imageArray[];
String[] stringArray;
public ImagePagerAdapter(Activity act, int[] imgArra, String[] stringArra) {
imageArray = imgArra;
activity = act;
stringArray = stringArra;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext
().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_pager, null);
ImageView im=(ImageView) layout.findViewById(R.id.myimage);
im.setImageResource(imageArray[position]);
TextView txt=(TextView) layout.findViewById(R.id.image_text);
txt.setText(stringArray[position]);
((ViewPager) collection).addView(layout, 0);
return layout;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
As documented by Google, the Gallery class was deprecated in API level 16.
This widget is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library. So I used ViewPager as an alternative to the Gallery class.
My goal to finally achieve an infinite scrolling image ViewPager with text descriptions. I used the below code to achieve the image ViewPager with text describing each image but how do I apply Infinite Scrolling to a ViewPager?
I have not worked with ViewPager before so please try to provide detailed code if possible.
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/myimagepager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
custom_pager.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/myimage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="2" />
<TextView
android:id="@+id/image_text"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
ImagePager:
public class ImagePager extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra, stringArray );
ViewPager myPager = (ViewPager) findViewById(R.id.myimagepager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
private int imageArra[] = { R.drawable.a, R.drawable.b,R.drawable.c,
R.drawable.d,R.drawable.e,R.drawable.f,
R.drawable.g, R.drawable.h, R.drawable.i};
private String[] stringArray = new String[] { "Image a", "Image b","Image c"
"Image d","Image e","Image f",
"Image g","Image h","Image i"};
}
ImagePagerAdapter:
public class ImagePagerAdapter extends PagerAdapter {
Activity activity;
int imageArray[];
String[] stringArray;
public ImagePagerAdapter(Activity act, int[] imgArra, String[] stringArra) {
imageArray = imgArra;
activity = act;
stringArray = stringArra;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater)collection.getContext
().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_pager, null);
ImageView im=(ImageView) layout.findViewById(R.id.myimage);
im.setImageResource(imageArray[position]);
TextView txt=(TextView) layout.findViewById(R.id.image_text);
txt.setText(stringArray[position]);
((ViewPager) collection).addView(layout, 0);
return layout;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我遇到了同样的问题,但我找到了解决方法它 - 代码可以在 github 上找到。
如果您复制类 InfiniteViewPager 和 InfinitePagerAdapter 到您的项目中,您可以通过一些小的更改来实现无限(包裹)滚动。
在您的 Activity 中,使用 InfinitePagerAdapter 包装 PagerAdapter:
将活动 XML 中的 ViewPager 更改为 InfiniteViewPager:
您可以将类重命名为您想要的任何名称。此代码仅在您至少有三个页面时才有效(示例代码中有九个页面,因此它可以正常工作)。
I had the same problem, but I was able to find a way to solve it - the code can be found on github.
If you copy the classes InfiniteViewPager and InfinitePagerAdapter into your project, you can achieve infinite (wrapped) scrolling with some small changes.
In your Activity, wrap your PagerAdapter with an
InfinitePagerAdapter
:Change the ViewPager in the activity XML to be an
InfiniteViewPager
:You can rename the classes to whatever you want. This code only works if you have at least three pages (you have nine in your example code, so it will work fine for that).
我认为我的解决方案更简单。
注意我的图像结构数组:
技巧在于
onPageScrollStateChanged
:当用户滚动到最后一项时 -> 寻呼机跳转到第一个图像(位置 = 1),没有动画
当用户滚动到第一个项目 -> 时, 寻呼机在没有动画的情况下跳转到最后一个图像(位置 = 计数 - 2)
I think my solution is simpler.
Attention to my array of images structure:
The trick is on
onPageScrollStateChanged
:When user scrolls to last item -> the pager jumps without animation to first image (position = 1)
When user scrolls to first item -> the pager jumps without animation to last image (position = count - 2)
我已经通过简单的技巧找到了一种方法,我希望它对你有帮助
[编辑1]
i have already found a way by making simple trick , i hope it helps to you
[EDIT 1]
我找到了另一个解决方案,基于 Shlomi Hasin 和 antonyt 回答,无需修改集合。
完整课程:
工作原理:
我们有一些集合
然后向其中添加两个附加项目
在最后一个位置显示第一个元素,在第一个位置显示最后一个元素
添加侦听器和交换页面活动中,从 4 点到 1 点以及从 0 到 3
就是这样。
我不建议您在那里应用动画或重型布局,元素交换时它可能会滞后。(也许,我没有遇到任何滞后)
也不要多次将此适配器设置为寻呼机,或将 SwapPageListener 移至外部类并将其设置在初始化块中。
I found another solution, based on Shlomi Hasin and antonyt answers, without modifying collection.
Full class:
How it works:
We have some collection
then add two additional items to it
show first element on last position and last element on first position
add listener and swap pages on event, from 4 to 1 and from 0 to 3
thats all.
I would not advise you to apply animation there or heavy layouts, it can be lagging when elements swap.(maybe, I did not encounter any lags)
Also don't set this adapter to pager more than once, or move SwapPageListener to external class and set it in initialization block.
对于几天的无限滚动,重要的是你在寻呼机中拥有好的片段,因此我在页面上写下了我的答案(Android 中的 Viewpager 可在不同日期之间无限切换)
它运行得很好!上面的答案对我来说不起作用,因为我希望它起作用。
For infinite scrolling with days it's important you have the good fragment in the pager therefore I wrote my answer on this on page (Viewpager in Android to switch between days endlessly)
It's working very well! Above answers did not work for me as I wanted it to work.
RecyclerViewPager 实现了
无限滚动
,可以像画廊一样滚动。RecyclerViewPager has implement
infinite scrolling
and can scroll like a gallery.