ViewPager引导
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ViewPager引导相关的知识,希望对你有一定的参考价值。
1、首先要导入android-support.v4.jar第三方工具包
2、activity_guid.xml
<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=".GuidActivity" >
<android.support.v4.view.ViewPager 包的封装类
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<LinearLayout 用来装图片
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
<Button
android:id="@+id/isOpen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="80dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:text="进入" />
</RelativeLayout>
3、GuidActivity.class
private List<View> list;
private List<ImageView> images;
private ViewPager viewPager;
private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guid);
viewPager = (ViewPager) findViewById(R.id.viewPager);
layout = (LinearLayout) findViewById(R.id.layout1);
init();
initImager();
}
public void init(){
list = new ArrayList<View>();
View v1 = LayoutInflater.from(this).inflate(R.layout.viewpager_item, null);
ImageView iv1 = (ImageView) v1.findViewById(R.id.item_image);
iv1.setBackgroundResource(R.drawable.guide_1);
list.add(v1);
View v2 = LayoutInflater.from(this).inflate(R.layout.viewpager_item, null);
ImageView iv2 = (ImageView) v2.findViewById(R.id.item_image);
iv2.setBackgroundResource(R.drawable.guide_2);
list.add(v2);
View v3 = LayoutInflater.from(this).inflate(R.layout.viewpager_item, null);
ImageView iv3 = (ImageView) v3.findViewById(R.id.item_image);
iv3.setBackgroundResource(R.drawable.guide_3);
list.add(v3);
View v4 = LayoutInflater.from(this).inflate(R.layout.viewpager_item, null);
ImageView iv4 = (ImageView) v4.findViewById(R.id.item_image);
iv4.setBackgroundResource(R.drawable.guide_4);
list.add(v4);
ViewPagerAdapter adapter = new ViewPagerAdapter(list);
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) { 设置底部点的效果
// TODO Auto-generated method stub
for(int i=0;i<images.size();i++){
if(i==position){
images.get(i).setImageResource(R.drawable.banner_dian_focus);
if(i==images.size()-1){
bt_open.setVisibility(View.VISIBLE);
}else{
bt_open.setVisibility(View.INVISIBLE);
}
}else{
images.get(i).setImageResource(R.drawable.banner_dian_blur);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
public void initImager(){ 向底部添加点
images = new ArrayList<ImageView>();
for(int i=0;i<list.size();i++){
ImageView iv = new ImageView(this);
iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
if(i == 0){
iv.setImageResource(R.drawable.banner_dian_focus);
}else{
iv.setImageResource(R.drawable.banner_dian_blur);
}
iv.setPadding(30, 0, 0, 0);
images.add(iv);
layout.addView(iv);
}
}
4、ViewPagerAdapter.class
public class ViewPagerAdapter extends PagerAdapter{
private List<View> list;
public ViewPagerAdapter(List<View> list) {
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub 添加布局
((ViewPager)container).addView(list.get(position));
return list.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager)container).removeView(list.get(position)); 删除布局
}
}
以上是关于ViewPager引导的主要内容,如果未能解决你的问题,请参考以下文章
Android基础控件——ViewPager实现带有动画的引导页
Android零基础入门第69节:ViewPager快速实现引导页