java – 无限滚动图像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;
}
}
解决方法:
我有same problem,但我能找到解决方法 – 代码可以在github找到.
如果将类InfiniteViewPager和InfinitePagerAdapter复制到项目中,则可以通过一些小的更改实现无限(包装)滚动.
在您的Activity中,使用InfinitePagerAdapter包装PagerAdapter:
PagerAdapter adapter = new InfinitePagerAdapter(new ImagePagerAdapter(this, imageArra, stringArray));
将活动XML中的ViewPager更改为InfiniteViewPager:
<com.antonyt.infiniteviewpager.InfiniteViewPager
android:id="@+id/myimagepager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
您可以将类重命名为您想要的任何名称.此代码仅在您至少有三个页面时才有效(您的示例代码中有九个,因此它可以正常工作).
标签:android,java,android-viewpager,infinite-scroll 来源: https://codeday.me/bug/20190918/1810792.html