[android] 轮播图-无限循环详解手机开发

 

实现无限循环

getCount()方法中,返回一个很大的值,Integer.MAX_VALUE

instantiateItem()方法中,获取当前View的索引时,进行取于操作,传递进来的int position是个非常大的数,对他进行求余数

destroyItem()方法中,同样

onPageSelected()监听方法中,对传递进来的索引进行取于

 

反向的无限循环

调用ViewPager对象的setCurrentItem()方法,设置第一次进来时候的当前页,参数:int数字,我们把它定位到Integer.MAX_VALUE的中间,并且保证第一条是当前位置,Integer.MAX_VALUE/2-((Integer.MAX_VALUE/2)%banners.size())

 

目前有个左划空白页的问题没解决

package com.tsh.myviewpager; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v4.view.ViewPager.OnPageChangeListener; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
    private ViewPager vp_banner; 
    private LinearLayout ll_points; 
    private TextView tv_title; 
    private List<View> banners; 
    private String[] titles=new String[]{ 
            "新闻标题1", 
            "新闻标题2", 
            "新闻标题3" 
    }; 
    private List<ImageView> points=new ArrayList<ImageView>(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        // 初始化 
        vp_banner = (ViewPager) findViewById(R.id.vp_banner); 
        ll_points = (LinearLayout) findViewById(R.id.ll_points); 
        tv_title = (TextView) findViewById(R.id.tv_title); 
         
        // banner部分 
        banners = new ArrayList<>(); 
        ImageView img1 = new ImageView(this); 
        img1.setImageResource(R.drawable.a); 
        banners.add(img1); 
        ImageView img2 = new ImageView(this); 
        img2.setImageResource(R.drawable.b); 
        banners.add(img2); 
        ImageView img3 = new ImageView(this); 
        img3.setImageResource(R.drawable.c); 
        banners.add(img3); 
         
        //小图标 
        ll_points=(LinearLayout) findViewById(R.id.ll_points); 
        for(int i=0;i<banners.size();i++){ 
            ImageView image=new ImageView(this); 
            image.setImageResource(R.drawable.point_selector); 
            LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(5, 5); 
            params.rightMargin=4; 
            image.setLayoutParams(params); 
             
            if(i==0){ 
                image.setEnabled(true); 
            }else{ 
                image.setEnabled(false); 
            } 
            points.add(image); 
            ll_points.addView(image); 
        } 
         
        // 设置适配器 
        vp_banner.setAdapter(new MyPagerAdapter()); 
        vp_banner.setCurrentItem(Integer.MAX_VALUE/2-((Integer.MAX_VALUE/2)%banners.size())); 
        vp_banner.setOnPageChangeListener(new OnPageChangeListener() { 
            @Override 
            public void onPageSelected(int arg0) { 
                tv_title.setText(titles[arg0%banners.size()]); 
                //小图标处理 
                for(ImageView point:points){ 
                    point.setEnabled(false); 
                } 
                points.get(arg0%banners.size()).setEnabled(true); 
            } 
             
            @Override 
            public void onPageScrolled(int arg0, float arg1, int arg2) { 
                // TODO Auto-generated method stub 
                 
            } 
             
            @Override 
            public void onPageScrollStateChanged(int arg0) { 
                // TODO Auto-generated method stub 
                 
            } 
        }); 
         
    } 
 
    private class MyPagerAdapter extends PagerAdapter { 
        @Override 
        public void destroyItem(ViewGroup container, int position, Object object) { 
            System.out.println("销毁:"+position); 
            container.removeView((View)object); 
            object=null; 
        } 
 
        @Override 
        public Object instantiateItem(ViewGroup container, int position) { 
            System.out.println("初始化:"+position); 
            position=position%banners.size(); 
             
            //解决右划出错 
            if(banners.get(position).getParent()!=null){ 
                //container.removeView(banners.get(position)); 
            }else{ 
                container.addView(banners.get(position)); 
            } 
             
            return banners.get(position); 
        } 
 
        @Override 
        public int getCount() { 
            return Integer.MAX_VALUE; 
        } 
 
        @Override 
        public boolean isViewFromObject(View arg0, Object arg1) { 
            return arg0 == arg1; 
        } 
    } 
}

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/app/5403.html

(0)
上一篇 2021年7月16日 23:47
下一篇 2021年7月16日 23:47

相关推荐

发表回复

登录后才能评论