2

我是 android 新手,在我的应用程序中我正在使用圆形指示器。但我不知道如何添加它.. 谁能告诉我如何在我的代码中添加圆形指示器。这是我的代码

public class WalkthroughActivity extends Activity {

    private static final int MAX_VIEWS = 5;

    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(new WalkthroughPagerAdapter());
        mViewPager.setOnPageChangeListener(new WalkthroughPageChangeListener());
    }


    class WalkthroughPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return MAX_VIEWS;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == (View) object;
        }

        @Override
        public Object instantiateItem(View container, int position) {
            Log.e("walkthrough", "instantiateItem(" + position + ");");
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View imageViewContainer = inflater.inflate(R.layout.activity_view_pager, null);
            ImageView imageView = (ImageView) imageViewContainer.findViewById(R.id.imageView1);

            switch(position) {
            case 0:
                imageView.setImageResource(R.drawable.kierkegaarddrop);
                break;

            case 1:
                imageView.setImageResource(R.drawable.nature);
                break;

            case 2:
                imageView.setImageResource(R.drawable.path);
                break;

            case 3:
                imageView.setImageResource(R.drawable.supportlife);
                break;

            case 4:
                imageView.setImageResource(R.drawable.willow);
                break;
            }

            ((ViewPager) container).addView(imageViewContainer, 0);
            return imageViewContainer;
        }

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


    class WalkthroughPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

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

        }

        @Override
        public void onPageSelected(int position) {
            // Here is where you should show change the view of page indicator
            switch(position) {

            case MAX_VIEWS - 1:
                break;

            default:

            }

        }

    }
4

2 回答 2

4

为此,您需要从这里下载一个库

在你的项目中使用这个库,如下所示:
在 xml 中:你想要
多少:width height

<com.viewpagerindicator.CirclePageIndicator
                android:id="@+id/indicator"
                android:layout_width="248dp"
                android:layout_height="30dp"
                android:layout_below="@+id/pager"
                android:layout_centerHorizontal="true"
                android:padding="3dip" />

并相应地在java代码中使用它

于 2013-01-05T07:30:17.523 回答
1

不要重新发明轮子——ViewPagerIndicator这里开始使用,这将与您的ViewPager.

于 2013-01-05T07:14:12.607 回答