ViewPage

1. app/build.gradle 添加需要用到的v4包

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
//    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:support-v4:22.0.0'
}

2. layout/fragment_main.xml 在布局里添加ViewPager
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:animateLayoutChanges="true">

        <android.support.v4.view.ViewPager
            android:id="@+id/headerViewPager"
            android:layout_width="match_parent"
            android:layout_height="160dp" />

        <LinearLayout
            android:id="@+id/pageBulleLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:layout_marginBottom="16dp"
            android:layoutDirection="ltr"
            android:orientation="horizontal"></LinearLayout>
    </FrameLayout>

3. layout/view_main_header_one.xml ViewPagerAdapter Item布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/r"
    android:orientation="vertical">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/g"
        android:textSize="40dp"
        android:layout_centerInParent="true"/>
    <Button
        android:id="@+id/change"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/change"/>
</RelativeLayout>

4. layout/view_main_header_two.xml ViewPagerAdapter Item布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/h"
    android:orientation="vertical"/>

5.layout/view_header_view_bullet.xml bullet的布局文件

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginStart="4dp"
    android:layout_marginEnd="4dp"/>

6. mHeaderViewPagerAdapter 匿名内部类实现PagerAdapter

    protected final PagerAdapter mHeaderViewPagerAdapter = new PagerAdapter() {
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mHeaderViewList.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

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

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o; //check if two objects are same
        }
    };

7. onCreateView 设置Adapter和监听器

        mHeaderViewPager = (ViewPager) mRootView.findViewById(R.id.headerViewPager);
        mHeaderViewPager.setAdapter(mHeaderViewPagerAdapter);
        mHeaderViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updatePageBulletPosition();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state) {
                    case ViewPager.SCROLL_STATE_DRAGGING:
                        stopAutoScrollViewPager();
                        break;
                    case ViewPager.SCROLL_STATE_IDLE:
                        startAutoScrollViewPager();
                        break;
                    default:
                        break;
                }
            }
        });
        updateHeaderView();


8. 更新数据

    public void updateHeaderView() {
        List<View> headerViewList = new ArrayList<>();
        View oneHeaderView = mHeaderTypeViewMap.get(HeaderType.ONE);
        if (oneHeaderView == null) {
            oneHeaderView = createHeaderView(HeaderType.ONE);
            mHeaderTypeViewMap.put(HeaderType.ONE, oneHeaderView);
        }
        if (oneHeaderView != null) {
            headerViewList.add(oneHeaderView);
        }

        View twoHeaderView = mHeaderTypeViewMap.get(HeaderType.TWO);
        if (twoHeaderView == null) {
            twoHeaderView = createHeaderView(HeaderType.TWO);
            mHeaderTypeViewMap.put(HeaderType.TWO, twoHeaderView);
        }
        if (twoHeaderView != null) {
            headerViewList.add(twoHeaderView);
        }

        //Remake bullets if page count is changed.
        if (headerViewList.size() != mHeaderViewList.size()) {
            mPageBulletLayout.removeAllViews();
            LayoutInflater layoutInflater = (LayoutInflater) mMainActivity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            for (int i = 0; i < headerViewList.size(); i++) {
                ImageView bullteImageView = (ImageView) layoutInflater.inflate(R.layout.view_header_view_bullet,
                        mPageBulletLayout, false);
                mPageBulletLayout.addView(bullteImageView);
            }
        }

        //Remake & notify headerView if page set is change.
        if (!headerViewList.equals(mHeaderViewList)) {
            mHeaderViewList.clear();
            ;
            mHeaderViewList.addAll(headerViewList);

            mHeaderViewPagerAdapter.notifyDataSetChanged();
        }

        // Update bullets
        if (mHeaderViewList.size() > 1) {
            mPageBulletLayout.setVisibility(View.VISIBLE);
            updatePageBulletPosition();
        } else {
            mPageBulletLayout.setVisibility(View.GONE);
        }
    }

    public View createHeaderView(HeaderType type) {
        LayoutInflater layoutInflater = (LayoutInflater) mMainActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View headerView = null;
        switch (type) {
            case ONE: {
                headerView = layoutInflater.inflate(R.layout.view_main_header_one, mHeaderViewPager, false);
                final TextView titleTextView = (TextView) headerView.findViewById(R.id.title);
                titleTextView.setText(String.format(mMainActivity.getString(R.string.string_format), 0));
                Button changeButton = (Button) headerView.findViewById(R.id.change);
                changeButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        titleTextView.setText(String.format(mMainActivity.getString(R.string.string_format), 9));
                    }
                });
                break;
            }
            case TWO: {
                headerView = layoutInflater.inflate(R.layout.view_main_header_two, mHeaderViewPager, false);
                break;
            }
            default:
                break;
        }
        return headerView;
    }

    public void updatePageBulletPosition() {
        int currentIndex = mHeaderViewPager.getCurrentItem();
        for (int i = 0; i < mPageBulletLayout.getChildCount(); i++) {
            ImageView bulletImage = (ImageView) mPageBulletLayout.getChildAt(i);
            bulletImage.setImageDrawable(mMainActivity.getResources().getDrawable(i == currentIndex
                    ? R.drawable.home_keyvisual_swipe_on : R.drawable.home_keyvisual_swipe_off));
        }
    }


你可能感兴趣的:(viewpage)