Infinite Scrolling Image ViewPager

后端 未结 7 785
Happy的楠姐
Happy的楠姐 2020-11-27 17:33

As documented by Google, the Gallery class was deprecated in API level 16. This widget is no longer supported. Other horizontally scrolling widgets include HorizontalScrollV

相关标签:
7条回答
  • 2020-11-27 18:03

    i have already found a way by making simple trick , i hope it helps to you

    import java.util.ArrayList;
    import android.os.Bundle;
    import android.app.Activity;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.util.Log;
    
    public class ImagePager extends Activity {
        String[] stringArray;
        int[] imageArra;
        ViewPager myPager;
        Boolean isScrooled = false;
        // use this array yo understand swipe left or right ?
        ArrayList<Float> pos = new ArrayList<Float>();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
                // put  empty view at the beginnig and to end
    
            imageArra = new int[] { 0, R.drawable.ic_launcher,
                    R.drawable.ic_launcher, R.drawable.ic_launcher,
                    R.drawable.ic_launcher, R.drawable.ic_launcher,
                    R.drawable.ic_launcher, R.drawable.ic_launcher,
                    R.drawable.ic_launcher, R.drawable.ic_launcher, 0 };
    
               // put  empty string at the beginnig and to end
    
            stringArray = new String[] { "", "Image a", "Image b", "Image c",
                    "Image d", "Image e", "Image f", "Image g", "Image h",
                    "Image i", "" };
    
            ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra,
                    stringArray);
            myPager = (ViewPager) findViewById(R.id.myimagepager);
            myPager.setAdapter(adapter);
            myPager.setCurrentItem(1);
    
            myPager.setOnPageChangeListener(new OnPageChangeListener() {
    
                @Override
                public void onPageSelected(int arg0) {
                    Log.v("onPageSelected", String.valueOf(arg0));
                    pos.clear();
                }
    
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    try {
                                // while scrolling i add ever pos to array
                        pos.add(arg1);
                                        // if pos.get(0) > pos.get(pos.size() - 1) 
                        // <----- swipe <-----  
                                        // we should check isScroll because setCurrent item is not a croll ? 
    
                        if (pos.size() > 0)
                            if (arg0 == imageArra.length - 1
                                    & pos.get(0) > pos.get(pos.size() - 1)
                                    & isScrooled == true) {
                                try {
                                    isScrooled = false;
                                    myPager.setCurrentItem(1, false);
                                } catch (Exception e) {
                                    Log.v("hata",
                                            "<----- swipe <-----  " + e.toString());
                                }
    
                            }
                            // ----> swipe ---->
                            else if (arg0 == 0
                                    & pos.get(0) < pos.get(pos.size() - 1)
                                    & isScrooled == true) {
                                try {
                                    isScrooled = false;
                                    myPager.setCurrentItem(imageArra.length - 1,
                                            false);
                                } catch (Exception e) {
                                    Log.v("hata",
                                            "----> swipe ---->  " + e.toString());
                                }
    
                            } else if (arg0 == 0 & pos.size() == 1
                                    & isScrooled == true) {
                                try {
                                    isScrooled = false;
                                    myPager.setCurrentItem(imageArra.length - 1,
                                            false);
                                } catch (Exception e) {
                                    Log.v("hata",
                                            "----> swipe ---->  " + e.toString());
                                }
    
                            }
    
                    } catch (Exception e) {
                        Log.v("hata", e.toString());
                    }
    
                }
    
                @Override
                public void onPageScrollStateChanged(int arg0) {
                    Log.v("onPageScrollStateChanged", String.valueOf(arg0));
                                // set is scrolling
                    isScrooled = true;
                }
            });
    
        }
    
    }
    

    [EDIT 1]

    import java.util.ArrayList;
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.util.Log;
    
    public class ImagePager extends Activity {
        String[] stringArray;
        int[] imageArra;
        ViewPager myPager;
        int scrollState;
        Boolean isFirstVisitEnd= true,isFirstVisitBegin = true;
        ArrayList<Integer> pos = new ArrayList<Integer>();
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            imageArra = new int[] { 0,R.drawable.ic_launcher,
                    R.drawable.ic_launcher, R.drawable.ic_launcher,
                    R.drawable.ic_launcher, R.drawable.ic_launcher,
                    R.drawable.ic_launcher, R.drawable.ic_launcher,
                    R.drawable.ic_launcher, R.drawable.ic_launcher, 0 };
    
            stringArray = new String[] {"","Image a", "Image b", "Image c",
                    "Image d", "Image e", "Image f", "Image g", "Image h",
                    "Image i", "" };
    
            ImagePagerAdapter adapter = new ImagePagerAdapter(this, imageArra,
                    stringArray);
            myPager = (ViewPager) findViewById(R.id.myimagepager);
            myPager.setAdapter(adapter);
            myPager.setCurrentItem(1);
    
            myPager.setOnPageChangeListener(new OnPageChangeListener() {
                @Override
                public void onPageSelected(int arg0) {
                    Log.v("onPageSelected", String.valueOf(arg0));
                    pos.clear();
                    if (arg0 == imageArra.length - 1)
                        isFirstVisitEnd = false;
                    else
                        isFirstVisitEnd = true;
    
    
                    if (arg0 == 0)
                        isFirstVisitBegin = false;
                    else
                        isFirstVisitBegin = true;
                }
    
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {                                
                    try { 
                        pos.add(Integer.valueOf(arg2));
    
                        if (pos.size() > 0) {
                            //Log.v("onPageScrolled_arg2","arg0  : "+String.valueOf(arg0)+"   ilk : "+pos.get(0).toString()+"    son : " +pos.get(pos.size() - 1).toString()+ "   "+ String.valueOf(pos.get(0)-(pos.get(pos.size() - 1)))+"    isFirstVisitEnd: "+String.valueOf(isFirstVisitEnd.booleanValue()) );
    
                            // <----- swipe <-----
                            if (arg0 == imageArra.length - 2& (pos.get(pos.size() - 1) -pos.get(0)  < 100)& scrollState == 2 & isFirstVisitEnd == false) {                          
                                myPager.setCurrentItem(1, false);
                            }
    
                            //Log.v("onPageScrolled_arg2","arg0  : "+String.valueOf(arg0)+"   ilk : "+pos.get(0).toString()+"    son : " +pos.get(pos.size() - 1).toString()+ "   "+ String.valueOf(pos.get(0)-(pos.get(pos.size() - 1)))+"    isFirstVisitbegin: "+String.valueOf(isFirstVisitBegin.booleanValue()) );
                            if (arg0 == 0 & (pos.get(pos.size() - 1) -pos.get(0)  > -100)& scrollState == 2 & isFirstVisitBegin == false) {                         
                                myPager.setCurrentItem(imageArra.length - 2, false);
                            }       
                        }
    
                    } catch (Exception e) {
                        Log.v("hata", e.toString());
                    }
    
                }
    
                @Override
                public void onPageScrollStateChanged(int arg0) {
                    Log.v("onPageScrollStateChanged", String.valueOf(arg0));
                    scrollState =arg0;
                }
            });
        }
    }
    
    0 讨论(0)
提交回复
热议问题