Android 为 viewpager 添加了圆形指示器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11486218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:48:49  来源:igfitidea点击:

Android added circle indicator to viewpager

androidandroid-viewpager

提问by Luke Batley

Hi i have created a view pager but does anyone know how i can show circles that represent pagination on a page view i think its clued an indicator but i'm not entirely sure?

嗨,我创建了一个视图寻呼机,但有谁知道我如何在页面视图上显示代表分页的圆圈,我认为它包含一个指示器,但我不完全确定?

heres the code so far that contains my view pager

继承人到目前为止包含我的视图寻呼机的代码

public class LevelSelect extends Activity {

    private ViewPager awesomePager;
    private static int NUM_AWESOME_VIEWS = 10;
    private Context cxt;
    private AwesomePagerAdapter awesomeAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_levelselect);
        cxt = this;

        awesomeAdapter = new AwesomePagerAdapter();
        awesomePager = (ViewPager) findViewById(R.id.viewpager);
        awesomePager.setAdapter(awesomeAdapter);
    }

    private class AwesomePagerAdapter extends PagerAdapter{


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


        @Override
        public Object instantiateItem(View collection, int position) {

            LayoutInflater mLayoutInflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View LeagueButtonView = mLayoutInflater.inflate(R.layout.ls_button, null);
            Button LeagueButton = (Button) LeagueButtonView.findViewById(R.id.league);
            LeagueButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.ls_level_eng1_locked));
            ((ViewPager) collection).addView(LeagueButtonView,0);

            return LeagueButtonView;
        }


        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View) view);
        }



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


        @Override
        public void finishUpdate(View arg0) {}


        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {}

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {}

    }

回答by K_Anas

You can use Jake Wharton's ViewPagerIndicator to add the titles or simple circles for each tab in your ViewPager.

您可以使用 Jake Wharton 的 ViewPagerIndicator 为 ViewPager 中的每个选项卡添加标题或简单的圆圈。

Jake Wharton has supplied a bunch of sample code on GitHub, you can also refer to the usage sectionon the Jake Wharton's site.

Jake Wharton 在GitHub 上提供了一堆示例代码,您也可以参考Jake Wharton 站点上的使用部分

enter image description here

在此处输入图片说明

You can also use the ViewPager and PageIndicator of Greendroid library

您还可以使用Greendroid 库的 ViewPager 和 PageIndicator

enter image description here

在此处输入图片说明

回答by Nirmal Dhara

Take two image(circle or any other shape image one for off another for on). Take one frame layout, show the image(indicator) on frame.When user change the image show the proper indicator.

拍摄两张图像(圆形或任何其他形状的图像,一个用于关闭另一个用于打开)。采取一帧布局,在框架上显示图像(指示器)。当用户更改图像时显示适当的指示器。

I have implemented this and working fine. You can downloadthe code and change accordingly. enter image description here

我已经实现了这个并且工作正常。您可以下载代码并进行相应更改。 在此处输入图片说明