1、之前在项目中一直使用自己手动的写ViewPager引导页卡片切换,但是看到网易新闻等一些做的比较好的APK的卡片切换,使用到了ViewpagerIndicator 进行相关设置。在这里不得不多啰嗦几句那就是,之前在一些教学视频中有的讲解师就提到了,要避免重复创造代码,如果你觉得你的代码重构和一些相关逻辑处理的很好的话,我就不多讲了!所以说:不要重复创造代码,但这并不代表的是一味地拿来用,要更加深入的了解它的一些框架,切入重点。
在这个ViewpagerIndicator 当,我们需要导入 相关的library库。所以我们的下载这个Library 当中的 sample 当中包含了各种卡片样式的切换效果!具体的使用我们可以参照 sample 例子中的设置。那么Github地址:https://github.com/JakeWharton/ViewPagerIndicator
1、创建相关的 layout 文件 main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.wangly.project.helloworld.MainActivity">
<TextView
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="ViewPagerIndicator(指示器)"
android:layout_width="wrap_content"
android:textSize="17sp"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView"
android:layout_above="@+id/indicator">
</android.support.v4.view.ViewPager>
<com.wangly.project.helloworld.CirclePageIndicator
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:id="@+id/indicator" />
</RelativeLayout>
2、Activity调用的时候,即可使用如下:
public class MainActivity extends FragmentActivity {
private TestAdapter adapter;
private ViewPager viewPager;
private CirclePageIndicator mIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
adapter = new TestAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(viewPager);
}
以上几步操作即可完成相关的操作!
本文介绍如何使用ViewPagerIndicator实现卡片切换效果,包括布局配置和Activity调用示例。
211

被折叠的 条评论
为什么被折叠?



