Example usage for android.support.v4.view PagerAdapter PagerAdapter

List of usage examples for android.support.v4.view PagerAdapter PagerAdapter

Introduction

In this page you can find the example usage for android.support.v4.view PagerAdapter PagerAdapter.

Prototype

PagerAdapter

Source Link

Usage

From source file:com.wewow.MainActivity.java

private void setUpViewPagerBanner(final List<Banner> banners) {

    group = (ViewGroup) findViewById(R.id.viewGroup);

    LayoutInflater inflater = getLayoutInflater();

    pageview = new ArrayList<View>();
    for (int i = 0; i < banners.size(); i++) {

        View view = inflater.inflate(R.layout.banner_item, null);
        ImageView imageBanner = (ImageView) view.findViewById(R.id.imageViewIcon);
        TextView textViewBannerTitle = (TextView) view.findViewById(R.id.textViewBannerTitle);
        textViewBannerTitle.setText(banners.get(i).getTitle());
        Glide.with(context).load(banners.get(i).getImage()).placeholder(R.drawable.banner_loading_spinner)
                .crossFade(300).into(imageBanner);
        view.setClickable(true);/*from  w w  w. j ava  2s .c  o m*/
        view.setFocusable(true);
        view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    AppBarLayout.LayoutParams mParams = (AppBarLayout.LayoutParams) mAppBarLayout.getChildAt(0)
                            .getLayoutParams();
                    mParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
                            | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
                } else {

                    AppBarLayout.LayoutParams mParams = (AppBarLayout.LayoutParams) mAppBarLayout.getChildAt(0)
                            .getLayoutParams();
                    mParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
                            | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
                            | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP);
                }
            }
        });

        pageview.add(view);
        final int j = i;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Banner banner = banners.get(j);
                String type = banner.getType();
                if (type.equals(CommonUtilities.BANNER_TYPE_SUBJECT)) {
                    Intent intent = new Intent(MainActivity.this, SubjectActivity.class);
                    intent.putExtra("id", banner.getId());
                    startActivity(intent);

                } else if (type.equals(CommonUtilities.BANNER_TYPE_COLLECTION)) {
                    LabCollection lc = new LabCollection();
                    lc.image = banner.getImage();
                    lc.title = banner.getTitle();
                    lc.id = Long.parseLong(banner.getId());
                    Intent intent = new Intent(MainActivity.this, LifeLabItemActivity.class);
                    intent.putExtra(LifeLabItemActivity.LIFELAB_COLLECTION, lc);

                    startActivity(intent);
                } else if (type.equals(CommonUtilities.BANNER_TYPE_POST)) {
                    Intent intent = new Intent(MainActivity.this, LifePostActivity.class);
                    intent.putExtra(LifePostActivity.POST_ID, Integer.parseInt(banner.getId()));
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(MainActivity.this, WebPageActivity.class);
                    intent.putExtra("url", banner.getUrl());
                    startActivity(intent);

                }
            }
        });
    }

    group.removeAllViews();
    //
    imageViews = new ImageView[pageview.size()];
    for (int i = 0; i < pageview.size(); i++) {
        imageView = new ImageView(MainActivity.this);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        imageView.setPadding(12, 0, 12, 0);
        imageViews[i] = imageView;

        //
        if (i == 0) {
            imageViews[i].setBackgroundResource(R.drawable.dot_b);
        } else {
            imageViews[i].setBackgroundResource(R.drawable.dot);
        }

        group.addView(imageViews[i]);
    }

    PagerAdapter mPagerAdapter = new PagerAdapter() {

        @Override

        public int getCount() {
            // TODO Auto-generated method stub
            return pageview.size();
        }

        @Override

        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(pageview.get(arg1));
        }

        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(pageview.get(arg1));
            return pageview.get(arg1);
        }

    };

    //set adapter
    viewPager.setAdapter(mPagerAdapter);
    if (Build.VERSION.SDK_INT >= 21) {
        viewPager.setNestedScrollingEnabled(false);
    }

    //set page change listener
    viewPager.setOnPageChangeListener(new GuidePageChangeListener());

}