无限滚动的图像ViewPager图像、ViewPager

由网友(要麽留要麽滾)分享简介:由于记录了谷歌画廊类是pcated在API层面16日$ P $。这个小工具不再支持。其他水平滚动部件包括Horizo​​ntalScrollView和ViewPager从支持库。所以我用ViewPager来替代库级,我的目标最终实现的无限滚动的图像viewpager与文字说明,我用波纹管code来实现图像viewp...

由于记录了谷歌画廊类是pcated在API层面16日$ P $。 这个小工具不再支持。其他水平滚动部件包括Horizo​​ntalScrollView和ViewPager从支持库。 所以我用ViewPager来替代库级,

我的目标最终实现的无限滚动的图像viewpager与文字说明,我用波纹管code来实现图像viewpager用文字描述每幅图像, 但如何申请无限滚动到viewpager 那是什么,我不能做到这一点,也是我在网上搜索方面,与没有结果,

注意:请提供详细的code答案,与viewpager类haven`t工作之前,

任何意见将AP preciated,谢谢

在code:

activity_main.xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
       机器人:layout_width =FILL_PARENT
       机器人:layout_height =FILL_PARENT
       机器人:方向=垂直>
  < android.support.v4.view.ViewPager
       机器人:ID =@ + ID / myimagepager
       机器人:layout_width =match_parent
       机器人:layout_height =match_parent/>
< / LinearLayout中>
 

custom_pager.xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
   机器人:layout_width =match_parent
   机器人:layout_height =match_parent
   机器人:方向=垂直
   机器人:重力=center_horizo​​ntal>
< ImageView的
   机器人:ID =@ + ID / MYIMAGE
   机器人:layout_width =match_parent
   机器人:layout_height =0dp
   机器人:layout_margin =5DP
   机器人:layout_weight =2/>
<的TextView
   机器人:ID =@ + ID / Image_Text的
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =0dp
   机器人:layout_weight =1/>
< / LinearLayout中>
 
AS的ViewPager

ImagePager:

 公共类ImagePager延伸活动{

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
ImagePagerAdapter适配器=新ImagePagerAdapter(这一点,imageArra,字符串数组);
 ViewPager myPager =(ViewPager)findViewById(R.id.myimagepager);
 myPager.setAdapter(适配器);
 myPager.setCurrentItem(0);}

 私人诠释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};

 私有String []字符串数组=新的String [] {图片一,图像B,图像C
                                               图像D,图像E,图像F,
                                               图像G,图像H,图像I};}
 

ImagePagerAdapter:

 公共类ImagePagerAdapter扩展PagerAdapter {

  活动活性;
  INT imageArray [];
  的String []字符串数组;

 公共ImagePagerAdapter(活动行为,INT [] imgArra,字符串[] stringArra){
    imageArray = imgArra;
    活动=行为;
    字符串数组= stringArra;}

公众诠释getCount将(){
    返回imageArray.length;}

公共对象instantiateItem(查看收集,INT位置){
    LayoutInflater充气=(LayoutInflater)collection.getContext
                       ().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              查看布局= inflater.inflate(R.layout.custom_pager,NULL);

    ImageView的IM =(ImageView的)layout.findViewById(R.id.myimage);
    im.setImageResource(imageArray [位置]);

    TextView的TXT =(TextView中)layout.findViewById(R.id.image_text);
    txt.setText(字符串数组[位置]);

    ((ViewPager)集合).addView(布局,0);
       返回布局; }

@覆盖
公共无效destroyItem(查看为arg0,INT ARG1,ARG2对象){
    ((ViewPager)为arg0).removeView((查看)ARG2);}

@覆盖
公共布尔isViewFromObject(查看为arg0,对象ARG1){
    返回将arg0 ==((查看)ARG1);}

@覆盖
公共Parcelable saveState和(){
    返回null; }}
 

解决方案

我有同样的问题的,但我能找到一种方法来解决它 - code可以在 github上。

如果您复制类InfiniteViewPager和InfinitePagerAdapter到你的项目,你可以实现无限的(包裹)滚动与一些小的变化。

在你的活动,包你PagerAdapter的 InfinitePagerAdapter

  PagerAdapter适配器=新InfinitePagerAdapter(新ImagePagerAdapter(这一点,imageArra,字符串数组));
 

更改ViewPager在活动XML是一种 InfiniteViewPager

 < com.antonyt.infiniteviewpager.InfiniteViewPager
       机器人:ID =@ + ID / myimagepager
       机器人:layout_width =match_parent
       机器人:layout_height =match_parent/>
 

您可以重命名为任何你想要的类。这code只有当你至少有三页(你必须在你的例子code九点,所以它会正常工作的那个)。

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 gallery class,

my goal to finally achieve infinite scrolling image viewpager with text description , i used the bellow code to achieve the image viewpager with text describing each image , but how to apply Infinite Scrolling to viewpager thats what i cant do it , also i searched on the net regard that with no result ,

NOTE: please PROVIDE Detailed CODE ANSWER , haven`t work before with viewpager class.

any advice will be appreciated , thanks

The code :

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; }}

解决方案

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:

PagerAdapter adapter = new InfinitePagerAdapter(new ImagePagerAdapter(this, imageArra, stringArray));

Change the ViewPager in the activity XML to be an InfiniteViewPager:

<com.antonyt.infiniteviewpager.InfiniteViewPager 
       android:id="@+id/myimagepager" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" />

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).

阅读全文

相关推荐

最新文章