viewpager

原创
2018/09/27 14:45
阅读数 55
    public class HelpAdapter extends PagerAdapter{
        private Context context;
        public HelpAdapter(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
        	 View view= LayoutInflater.from(context).inflate(R.layout.item_ad,null);
             ImageView imv= (ImageView) view.findViewById(R.id.imv_ad_item);
             ImageLoaderService.displayImage(options, imv,list.get(position));
             container.addView(view);
             return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
    private ImageView[] dotViews;
    private LinearLayout ll_help_dot;
    private void initDots() {
        // TODO Auto-generated method stub
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mParams.setMargins(smg.dip2px(4), 0, smg.dip2px(4), 0);
        dotViews = new ImageView[list.size()];
        for(int i = 0; i < list.size(); i++)
        {
            ImageView imv_dot = new ImageView(this);
            imv_dot.setBackgroundResource(R.drawable.icon_weixuanzhong_2x_selector);
            imv_dot.setLayoutParams(mParams);
            if(i== 0)
            {
                imv_dot.setSelected(true);
            }
            else {
                imv_dot.setSelected(false);
            }
            dotViews[i] = imv_dot;
            ll_help_dot.addView(dotViews[i]);
        }

    }

更新

public class FlashActivity extends BaseActivity {

    private ViewPager viewPageFlash;
    private PagerAdapter pagerAdapter;
    private List<View> viewList;
    private ImageView[] dotViews;
    private LinearLayout ll_dot;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flash);
        viewPageFlash=findViewById(R.id.viewPageFlash);
        viewList=new ArrayList<>();
        initDots();
        pagerAdapter=new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                // TODO Auto-generated method stub
                container.addView(viewList.get(position));


                return viewList.get(position);
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        };
        viewPageFlash.setAdapter(pagerAdapter);
        viewPageFlash.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for(int i = 0; i < dotViews.length; i++)
                {
                    if(position == i)
                    {
                        dotViews[i].setSelected(true);
                    }
                    else {
                        dotViews[i].setSelected(false);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initDots() {
        // TODO Auto-generated method stub
        ll_dot=findViewById(R.id.ll_dot);
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        DisplayMetrics displayMetrics=getResources().getDisplayMetrics();
        mParams.setMargins(displayMetrics.densityDpi*4, 0, displayMetrics.densityDpi*4, 0);
        dotViews = new ImageView[viewList.size()];
        for(int i = 0; i < viewList.size(); i++)
        {
            ImageView imv_dot = new ImageView(this);
            imv_dot.setBackgroundResource(R.drawable.ic_launcher_background);
            imv_dot.setLayoutParams(mParams);
            if(i== 0)
            {
                imv_dot.setSelected(true);
            }
            else {
                imv_dot.setSelected(false);
            }
            dotViews[i] = imv_dot;
            ll_dot.addView(dotViews[i]);
        }

    }

    public void skip(View view) {
        startActivity(new Intent(getContext(),MainActivity.class));
    }

}

 

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部