在MaterialViewPager中添加背景图像

时间:2015-05-25 07:50:52

标签: android android-viewpager

我正在使用来自github(https://github.com/florent37/MaterialViewPager)的库MaterialViewPager作为我的项目。在库的github页面上给出了说明,但这些还不够。我想以编程方式更改库中视图寻呼机标题的背景。如果有人使用过此库,请提供帮助。在这里我附上截图(来自git和我的项目)This one is from my project This one is from git

2 个答案:

答案 0 :(得分:2)

在你的materialViewAdapter中,你应该实现这个功能! 这一部分真有效!

    @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);

            //only if position changed
            if (position == oldPosition)
                return;
            oldPosition = position;

            int color = 0;
            String imageUrl = "";
            switch (position) {
                case 0:
                    imageUrl = "http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2014/06/wallpaper_51.jpg";
                    color = getResources().getColor(R.color.blue);
                    break;
                case 1:
                    imageUrl = "https://fs01.androidpit.info/a/63/0e/android-l-wallpapers-630ea6-h900.jpg";
                    color = getResources().getColor(R.color.green);
                    break;
                case 2:
                    imageUrl = "http://www.droid-life.com/wp-content/uploads/2014/10/lollipop-wallpapers10.jpg";
                    color = getResources().getColor(R.color.cyan);
                    break;
                case 3:
                    imageUrl = "http://www.tothemobile.com/wp-content/uploads/2014/07/original.jpg";
                    color = getResources().getColor(R.color.red);
                    break;
            }

            final int fadeDuration = 400;
            mViewPager.setImageUrl(imageUrl, fadeDuration);
            mViewPager.setColor(color, fadeDuration);
            mViewPager.resetOffset(position);
        }

答案 1 :(得分:0)

是的,在新版本中你应该使用

mViewPager.setMaterialViewPagerListener(new MaterialViewPager.MaterialViewPagerListener() {
            @Override
            public HeaderDesign getHeaderDesign(int page) {
                switch (page) {
                    case 0:
                        return HeaderDesign.fromColorResAndUrl(
                                R.color.blue,
                                "http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2014/06/wallpaper_51.jpg");
                    case 1:
                        return HeaderDesign.fromColorResAndUrl(
                                R.color.green,
                                "https://fs01.androidpit.info/a/63/0e/android-l-wallpapers-630ea6-h900.jpg");
                    case 2:
                        return HeaderDesign.fromColorResAndUrl(
                                R.color.cyan,
                                "http://www.droid-life.com/wp-content/uploads/2014/10/lollipop-wallpapers10.jpg");
                    case 3:
                        return HeaderDesign.fromColorResAndUrl(
                                R.color.red,
                                "http://www.tothemobile.com/wp-content/uploads/2014/07/original.jpg");
                }

                //execute others actions if needed (ex : modify your header logo)

                return null;
            }
        });