溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

開源組件:DEMO學(xué)習(xí)-DWinterTabDemo:自定義tab+viewpager

發(fā)布時間:2020-08-04 18:18:14 來源:網(wǎng)絡(luò) 閱讀:601 作者:glblong 欄目:開發(fā)技術(shù)

效果圖:

開源組件:DEMO學(xué)習(xí)-DWinterTabDemo:自定義tab+viewpager

demo源碼:

public class MainActivity extends Activity
{
    // ViewPager是google SDk中自帶的一個附加包的一個類,可以用來實(shí)現(xiàn)屏幕間的切換。
    // android-support-v4.jar
    private ViewPager mPager;// 頁卡內(nèi)容
    private List<View> listViews; // Tab頁面列表
    private ImageView cursor;// 動畫圖片
    private TextView t1, t2, t3;// 頁卡頭標(biāo)
    private int offset = 0;// 動畫圖片偏移量
    private int currIndex = 0;// 當(dāng)前頁卡編號
    private int bmpW;// 動畫圖片寬度
            
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        InitImageView();
        InitTextView();
        InitViewPager();
    }
            
    /**
     * 初始化頭標(biāo)
     */
    private void InitTextView()
    {
        t1 = (TextView) findViewById(R.id.text1);
        t2 = (TextView) findViewById(R.id.text2);
        t3 = (TextView) findViewById(R.id.text3);
                
        t1.setOnClickListener(new MyOnClickListener(0));
        t2.setOnClickListener(new MyOnClickListener(1));
        t3.setOnClickListener(new MyOnClickListener(2));
    }
            
    /**
     * 初始化ViewPager
     */
    private void InitViewPager()
    {
        mPager = (ViewPager) findViewById(R.id.vPager);
        listViews = new ArrayList<View>();
        LayoutInflater mInflater = getLayoutInflater();
        listViews.add(mInflater.inflate(R.layout.lay1, null));
        listViews.add(mInflater.inflate(R.layout.lay2, null));
        listViews.add(mInflater.inflate(R.layout.lay3, null));
        mPager.setAdapter(new MyPagerAdapter(listViews));
        mPager.setCurrentItem(0);
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }
            
    /**
     * 初始化動畫
     */
    private void InitImageView()
    {
        cursor = (ImageView) findViewById(R.id.cursor);
        bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a).getWidth();// 獲取圖片寬度
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 獲取分辨率寬度
        offset = (screenW / 3 - bmpW) / 2;// 計(jì)算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        cursor.setImageMatrix(matrix);// 設(shè)置動畫初始位置
    }
            
    /**
     * ViewPager適配器
     */
    public class MyPagerAdapter extends PagerAdapter
    {
        public List<View> mListViews;
                
        public MyPagerAdapter(List<View> mListViews)
        {
            this.mListViews = mListViews;
        }
                
        @Override
        public void destroyItem(View arg0, int arg1, Object arg2)
        {
            ((ViewPager) arg0).removeView(mListViews.get(arg1));
        }
                
        @Override
        public void finishUpdate(View arg0)
        {
        }
                
        @Override
        public int getCount()
        {
            return mListViews.size();
        }
                
        @Override
        public Object instantiateItem(View arg0, int arg1)
        {
            ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
            return mListViews.get(arg1);
        }
                
        @Override
        public boolean isViewFromObject(View arg0, Object arg1)
        {
            return arg0 == (arg1);
        }
                
        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1)
        {
        }
                
        @Override
        public Parcelable saveState()
        {
            return null;
        }
                
        @Override
        public void startUpdate(View arg0)
        {
        }
    }
            
    /**
     * 頭標(biāo)點(diǎn)擊監(jiān)聽
     */
    public class MyOnClickListener implements View.OnClickListener
    {
        private int index = 0;
                
        public MyOnClickListener(int i)
        {
            index = i;
        }
                
        @Override
        public void onClick(View v)
        {
            mPager.setCurrentItem(index);
        }
    };
            
    /**
     * 頁卡切換監(jiān)聽
     */
    public class MyOnPageChangeListener implements 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:
                    if (currIndex == 1)
                    {
                        animation = new TranslateAnimation(one, 0, 0, 0);
                    }
                    else if (currIndex == 2)
                    {
                        animation = new TranslateAnimation(two, 0, 0, 0);
                    }
                    break;
                case 1:
                    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:圖片停在動畫結(jié)束位置
            animation.setDuration(300);
            cursor.startAnimation(animation);
        }
                
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2)
        {
        }
                
        @Override
        public void onPageScrollStateChanged(int arg0)
        {
        }
    }
}




其他方法:

1.ViewPager使用

http://zhangfy068.iteye.com/blog/1819979


2.頁卡實(shí)現(xiàn)詳解:Android ViewPager使用詳解

http://blog.csdn.net/wangjinyu501/article/details/8169924











向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI