給viewpager加上滑動條

佈局文件:主要看ImageView的位置:
<?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:orientation="vertical">

    <LinearLayout
        android:id="@+id/product_title_layout"
        android:layout_width="match_parent"
        android:layout_height="40.0dip"
        android:background="#FFFFFF" >

        <TextView
            android:id="@+id/product_title_detail_txtv"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="圖文詳情"
            style="@style/textStyle.Small.black" />

        <TextView
            android:id="@+id/product_title_comment_txtv"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="評論"
            style="@style/textStyle.Small.black" />
    </LinearLayout>

    <ImageView
        android:id="@+id/cursor"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/icon_move_line" />

    <android.support.v4.view.ViewPager
        android:id="@+id/product_comment_vpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:background="#000000"
        android:flipInterval="30"
        android:persistentDrawingCache="animation" />
</LinearLayout>

代碼:
private ImageView cursor;
private int bmpw = 0; // 遊標寬度
private int offset = 0;// // 動畫圖片偏移量
private int currIndex = 0;// 當前頁卡編號
ArrayList<Fragment> listFragment = new ArrayList<Fragment>();

cursor  = (ImageView) view.findViewById(R.id.cursor);
mViewPager.setOffscreenPageLimit(1);
mViewPager.setAdapter(new MyFragmentPagerAdapter(fragmentManager, listFragment));//適配器
mViewPager.setOnPageChangeListener(new MyPageChangeListener());

/**
 * 初始化指示器位置
 */
public void initCursorPos() {
    // 初始化動畫
    bmpw = BitmapFactory.decodeResource(getResources(), R.drawable.img_line_red).getWidth();// 獲取圖片寬度
    DisplayMetrics dm = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
    int screenW = dm.widthPixels;// 獲取分辨率寬度
    offset = (screenW / 2 - bmpw) / 2;// 計算偏移量
    Matrix matrix = new Matrix();
    matrix.postTranslate(offset, 0);
    cursor.setImageMatrix(matrix);// 設置動畫初始位置
}
//頁面改變監聽器
public class MyPageChangeListener implements ViewPager.OnPageChangeListener {
    int one = offset * 2 + bmpw;// 頁卡1 -> 頁卡2 偏移量
    int two = one * 2;// 頁卡1 -> 頁卡3 偏移量

    @Override
    public void onPageSelected(int arg0) {
        Animation animation = null;
        switch (arg0) {
            case 0:
                fragmentFlag=0;
                textTypeOne.setTextColor(getResources().getColor(R.color.color_FA4148));//                textTypeTwo.setTextColor(getResources().getColor(R.color.color_7e7e7e));//                if (currIndex == 1) {
                    animation = new TranslateAnimation(one, 0, 0, 0);
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, 0, 0, 0);
                }
                break;
            case 1:
                fragmentFlag=1;
                textTypeOne.setTextColor(getResources().getColor(R.color.color_7e7e7e));
                textTypeTwo.setTextColor(getResources().getColor(R.color.color_FA4148));
                if (currIndex == 0) {
                    animation = new TranslateAnimation(offset, one, 0, 0);
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, one, 0, 0);
                }
                break;
            case 2:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(offset, two, 0, 0);
                } else if (currIndex == 1) {
                    animation = new TranslateAnimation(one, two, 0, 0);
                }
                break;
        }

        currIndex = arg0;
        animation.setFillAfter(true);// True:圖片停在動畫結束位置
        animation.setDuration(300);
        cursor.startAnimation(animation);

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }
}

/**
 * ViewPager適配器
 */
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    ArrayList<Fragment> list;
    public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
        super(fm);
        this.list = list;
    }

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

    @Override
    public Fragment getItem(int arg0) {
        return list.get(arg0);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章