当前位置: 代码迷 >> Android >> 怎么实现类似IOS中 带分页效果的双向滚动scrollview
  详细解决方案

怎么实现类似IOS中 带分页效果的双向滚动scrollview

热度:85   发布时间:2016-05-01 18:03:12.0
如何实现类似IOS中 带分页效果的双向滚动scrollview
最近要实现一个带分页效果的双向滚动scrollview,看IOS上有相关的demo

不知道在android上如何实现,请各位高手指点一下,谢啦~

demo效果展示如下

http://v.youku.com/v_show/id_XNDEzNzAxMjYw.html

------解决方案--------------------
Java code
cta_my_custom_course_btn=(Button) findViewById(R.id.cta_my_custom_course_btn);        cta_my_optional_course_btn=(Button) findViewById(R.id.cta_my_optional_course_btn);        cta_my_study_record_btn=(Button) findViewById(R.id.cta_my_study_record_btn);        cta_my_custom_course_btn.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                 cta_my_custom_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_selected_btn));                cta_my_custom_course_btn.setTextColor(Color.rgb(51,68,68));                cta_my_optional_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_optional_course_btn.setTextColor(Color.rgb(255,255,255));                cta_my_study_record_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_study_record_btn.setTextColor(Color.rgb(255,255,255));                mPager.setCurrentItem(0);            }        });        cta_my_optional_course_btn.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                 cta_my_custom_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_custom_course_btn.setTextColor(Color.rgb(255,255,255));                cta_my_optional_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_selected_btn));                cta_my_optional_course_btn.setTextColor(Color.rgb(51,68,68));                cta_my_study_record_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_study_record_btn.setTextColor(Color.rgb(255,255,255));                mPager.setCurrentItem(1);            }        });        cta_my_study_record_btn.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                 cta_my_custom_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_custom_course_btn.setTextColor(Color.rgb(255,255,255));                cta_my_optional_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_optional_course_btn.setTextColor(Color.rgb(255,255,255));                cta_my_study_record_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_selected_btn));                cta_my_study_record_btn.setTextColor(Color.rgb(51,68,68));                mPager.setCurrentItem(2);            }        });    }    /**     * 初始化ViewPager     */    private void InitViewPager() {        mPager = (ViewPager) findViewById(R.id.cta_vp);        listViews = new ArrayList<View>();        LayoutInflater mInflater = getLayoutInflater();        View inflate = mInflater.inflate(R.layout.cta_my_custom_course_view, null);        ListView ListView01 = (ListView) inflate.findViewById(R.id.cta_my_custom_course_lv);        ListView01.setAdapter(new AS_CTA_my_custom_course_adapter(ASCourseTetailActivity.this));        listViews.add(inflate);        listViews.add(mInflater.inflate(R.layout.cta_my_optional_course_lv, null));        listViews.add(mInflater.inflate(R.layout.cta_my_study_record_lv, null));        mPager.setAdapter(new MyPagerAdapter(listViews));        mPager.setCurrentItem(0);        mPager.setOnPageChangeListener(new MyOnPageChangeListener());    }    /**     * 页卡切换监听     */    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);                }                cta_my_custom_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_selected_btn));                cta_my_custom_course_btn.setTextColor(Color.rgb(51,68,68));                cta_my_optional_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_optional_course_btn.setTextColor(Color.rgb(255,255,255));                cta_my_study_record_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_study_record_btn.setTextColor(Color.rgb(255,255,255));                break;            case 1:                if (currIndex == 0) {                    animation = new TranslateAnimation(offset, one, 0, 0);                } else if (currIndex == 2) {                    animation = new TranslateAnimation(two, one, 0, 0);                }                cta_my_custom_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_custom_course_btn.setTextColor(Color.rgb(255,255,255));                cta_my_optional_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_selected_btn));                cta_my_optional_course_btn.setTextColor(Color.rgb(51,68,68));                cta_my_study_record_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_study_record_btn.setTextColor(Color.rgb(255,255,255));                break;            case 2:                if (currIndex == 0) {                    animation = new TranslateAnimation(offset, two, 0, 0);                } else if (currIndex == 1) {                    animation = new TranslateAnimation(one, two, 0, 0);                }                cta_my_custom_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_custom_course_btn.setTextColor(Color.rgb(255,255,255));                cta_my_optional_course_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_btn));                cta_my_optional_course_btn.setTextColor(Color.rgb(255,255,255));                cta_my_study_record_btn.setBackgroundDrawable(resources.getDrawable(R.drawable.cta_selected_btn));                cta_my_study_record_btn.setTextColor(Color.rgb(51,68,68));                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 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) {        }    }        @Override    public void finish() {        super.finish();        System.exit(0);    }}