很多app在第一次进入软件的时候都会有一个引导用户的滑动页面,三页到五页不等
如下效果:
demoViewPager.gif
这里采用的是viewPager实现
首先上java代码目录结构:(就两个Activity,主要是GuideActivity)
image.png
然后是XML:
image.png
代码
GuideActivity:
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class GuideActivity extends AppCompatActivity {
private ViewPager viewPager;
private List<View> list;//定义一个View的集合
private View view1;
private View view2;
private View view3;//定义三个View
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
//初始化viewPager组件
viewPager = findViewById(R.id.viewPager);
//布局填充器
LayoutInflater inflater = LayoutInflater.from(this);
//初始化list
list = new ArrayList<>();
//初始化三个View并添加到集合中
view1 = inflater.inflate(R.layout.view1,null);
view2 = inflater.inflate(R.layout.view2,null);
view3 = inflater.inflate(R.layout.view3,null);
list.add(view1);
list.add(view2);
list.add(view3);
//初始化进入按钮,view3页面的按钮,通过view3.findViewById()
Button enterBtn = view3.findViewById(R.id.enterBtn);
//进入按钮的监听事件
enterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(GuideActivity.this,MainActivity.class));
finish();
}
});
/**
* ViewPager的适配器
* 一般复写getCount、isViewFromObject、destroyItem、instantiateItem即可
*/
PagerAdapter pagerAdapter = new PagerAdapter() {
/**
* 滑动视图的个数
* @return
*/
@Override
public int getCount() {
return list.size();
}
/**
* 如果对应的是同一个View,返回True,否则返回False
* @param view
* @param object
* @return
*/
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
/**
* 移除指定位置的视图
* @param container
* @param position
* @param object
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(list.get(position));
}
/**
* 创建指定位置的页面视图并返回
* @param container
* @param position
* @return
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(list.get(position));
return list.get(position);
}
};
//适配viewPager
viewPager.setAdapter(pagerAdapter);
}
}
MainActivity是空的,就不贴代码了
activity_guide.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</RelativeLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.demo.kinsin.demoviewpager.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="26sp"/>
</android.support.constraint.ConstraintLayout>
view3:(view1和view2比view3少个button,其他的都一样)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="26sp"
android:text="@string/viewPager3"/>
<Button
android:id="@+id/enterBtn"
android:layout_width="120dp"
android:layout_height="50dp"
android:text="@string/enterBtn"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
代码+注释,理解应该不难了,如有错误,请不吝指出,谢谢!
网友评论