Android CirclePageIndicator not working

2019-02-21 00:54发布

Im not able to load the page with circlepageindicator. This is the xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="@drawable/bg2" android:layout_width="match_parent"
    android:layout_height="match_parent" />
<LinearLayout android:layout_width="220dp"
    android:orientation="vertical" android:layout_gravity="center_horizontal"
    android:layout_height="32dp" android:layout_marginTop="32dp">
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:id="@+id/myfivepanelpager" />   

    <com.viewpagerindicator.CirclePageIndicator
    android:id="@+id/indicator"
    android:padding="10dip"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />

</LinearLayout>

java file

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MyPagerAdapter adapter=new MyPagerAdapter();
    ViewPager myPager=(ViewPager)findViewById(R.id.myfivepanelpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(2);

    CirclePageIndicator titleIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
    titleIndicator.setViewPager(myPager);
}

jake wharton project has been added to the library. It doesnt give any error while building, but not loading the page. whats wrong?

2条回答
Ridiculous、
2楼-- · 2019-02-21 00:59

Your ViewPager is set to match_parent which will prevent any other view in the container from showing. Set the ViewPager height to 0dp with android:layout_weight="1".

The container is also hard-coded to 32dp which is extremely tiny and likely will not hold both the pager and indicator comfortably.

查看更多
孤傲高冷的网名
3楼-- · 2019-02-21 01:08

Missing to add myIndicator.onPageScrolled(myViewPager.getCurrentItem(), 0, 0); may also cause CirclePageIndicator not working.

eg: myViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { myIndicator.onPageScrolled(myViewPager.getCurrentItem(), 0, 0); } });

查看更多
登录 后发表回答