我是新的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:
}
}
}
答案 0 :(得分:4)
为此您需要从here
下载库在你的项目中使用这个库,如下所示:
在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代码中使用它
答案 1 :(得分:1)
不要重新发明轮子 - 使用here中的ViewPagerIndicator
,ViewPager
将与{{1}}兼容。