Android簡(jiǎn)單實(shí)現(xiàn)引導(dǎo)頁
本文實(shí)例為大家分享了Android簡(jiǎn)單實(shí)現(xiàn)引導(dǎo)頁的具體代碼,供大家參考,具體內(nèi)容如下
一.思路
我們選擇ViewPager + View + ImageView 來實(shí)現(xiàn)引導(dǎo)頁效果,ViewPager用來實(shí)現(xiàn)滑動(dòng),View則是用來顯示每頁的圖像,而ImageView則是用來實(shí)現(xiàn)下面的小紅點(diǎn)。
二.XML代碼
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/image1"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="10dp"/>
<ImageView
android:id="@+id/image2"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="10dp"/>
<ImageView
android:id="@+id/image3"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="10dp"/>
<ImageView
android:id="@+id/image4"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_margin="10dp"/>
</LinearLayout>
</RelativeLayout>
有多少個(gè)頁面就寫多少個(gè)ImageView,這里用相對(duì)布局的主要原因是為了能讓小紅點(diǎn)位于父布局的底部。
三.實(shí)現(xiàn)代碼
1.自定義ViewPagerAdapter
class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return list.size(); // List<View> list = new ArrayList<>();
}
@Override
public boolean isViewFromObject(View p1, Object p2) {
return p1 == p2; // 判斷當(dāng)前對(duì)象是否對(duì)應(yīng)相應(yīng)的視圖 并返回一個(gè)布爾值
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(list.get(position)); // 添加一個(gè)View
return list.get(position); // 返回一個(gè)View
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object); // 銷毀一個(gè)View 因?yàn)槭荗bject類型所以需要轉(zhuǎn)型為View
}
}
2.自定義setImageView用來實(shí)現(xiàn)下方的紅點(diǎn)顯示
private void setImageView(boolean bool1, boolean bool2, boolean bool3, boolean bool4){
if(bool1){
image1.setBackgroundColor(Color.RED);
image2.setBackgroundColor(Color.WHITE);
image3.setBackgroundColor(Color.WHITE);
image4.setBackgroundColor(Color.WHITE);
}else if(bool2){
image2.setBackgroundColor(Color.RED);
image1.setBackgroundColor(Color.WHITE);
image3.setBackgroundColor(Color.WHITE);
image4.setBackgroundColor(Color.WHITE);
}else if(bool3){
image3.setBackgroundColor(Color.RED);
image2.setBackgroundColor(Color.WHITE);
image1.setBackgroundColor(Color.WHITE);
image4.setBackgroundColor(Color.WHITE);
}else if(bool4){
image4.setBackgroundColor(Color.RED);
image2.setBackgroundColor(Color.WHITE);
image3.setBackgroundColor(Color.WHITE);
image1.setBackgroundColor(Color.WHITE);
}
}
這里很好理解,有幾個(gè)頁面就傳入幾個(gè)參數(shù),參數(shù)類型都是布爾型,當(dāng)在第一個(gè)頁面是就應(yīng)該第一個(gè)參數(shù)是true后面都為false,后面的原理都一樣,然后就是ImageView的顯示,可以直接用兩張圖片來設(shè)置,而我沒有圖片就直接用的顏色。
3.設(shè)置ViewPager監(jiān)聽
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
@Override
public void onPageScrolled(int p1, float p2, int p3) {
}
@Override
public void onPageSelected(int p1) {
switch(p1){
case 0:
setImageView(true,false,false,false);
break;
case 1:
setImageView(false,true,false,false);
break;
case 2:
setImageView(false,false,true,false);
break;
case 3:
setImageView(false,false,false,true);
break;
}
}
@Override
public void onPageScrollStateChanged(int p1) {
}
});
在onPageSelected里面寫了一個(gè)switch是為了獲取當(dāng)前對(duì)應(yīng)的頁面并讓下方的小紅點(diǎn)跟隨變化。
4.完整代碼
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
List<View> list = new ArrayList<>();
View view1, view2, view3, view4;
ImageView image1, image2, image3, image4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
view1 = View.inflate(this, R.layout.view1, null);
view2 = View.inflate(this, R.layout.view2, null);
view3 = View.inflate(this, R.layout.view3, null);
view4 = View.inflate(this, R.layout.view4, null);
image1 = findViewById(R.id.image1);
image2 = findViewById(R.id.image2);
image3 = findViewById(R.id.image3);
image4 = findViewById(R.id.image4);
viewPager = findViewById(R.id.viewPager);
list.add(view1);
list.add(view2);
list.add(view3);
list.add(view4);
viewPager.setAdapter(new ViewPagerAdapter());
setImageView(true,false,false,false);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
@Override
public void onPageScrolled(int p1, float p2, int p3) {
}
@Override
public void onPageSelected(int p1) {
switch(p1){
case 0:
setImageView(true,false,false,false);
break;
case 1:
setImageView(false,true,false,false);
break;
case 2:
setImageView(false,false,true,false);
break;
case 3:
setImageView(false,false,false,true);
break;
}
}
@Override
public void onPageScrollStateChanged(int p1) {
}
});
}
private void setImageView(boolean bool1, boolean bool2, boolean bool3, boolean bool4){
if(bool1){
image1.setBackgroundColor(Color.RED);
image2.setBackgroundColor(Color.WHITE);
image3.setBackgroundColor(Color.WHITE);
image4.setBackgroundColor(Color.WHITE);
}else if(bool2){
image2.setBackgroundColor(Color.RED);
image1.setBackgroundColor(Color.WHITE);
image3.setBackgroundColor(Color.WHITE);
image4.setBackgroundColor(Color.WHITE);
}else if(bool3){
image3.setBackgroundColor(Color.RED);
image2.setBackgroundColor(Color.WHITE);
image1.setBackgroundColor(Color.WHITE);
image4.setBackgroundColor(Color.WHITE);
}else if(bool4){
image4.setBackgroundColor(Color.RED);
image2.setBackgroundColor(Color.WHITE);
image3.setBackgroundColor(Color.WHITE);
image1.setBackgroundColor(Color.WHITE);
}
}
class ViewPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View p1, Object p2) {
return p1 == p2;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(list.get(position));
return list.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
}
四.總結(jié)
我們使用了ViewPager + View + ImageView簡(jiǎn)單的實(shí)現(xiàn)了引導(dǎo)頁效果,當(dāng)然我們也可以使用ViewPager + Fragment + ImageView也可以,這個(gè)看個(gè)人習(xí)慣罷了,引導(dǎo)頁的實(shí)現(xiàn)并不難我們只要能熟練掌握ViewPager的使用方法就行。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)過渡動(dòng)畫、引導(dǎo)頁 Android判斷是否第一次啟動(dòng)App
- Android使用ViewPager實(shí)現(xiàn)啟動(dòng)引導(dǎo)頁
- Android開發(fā)實(shí)戰(zhàn)之漂亮的ViewPager引導(dǎo)頁
- Android開發(fā)實(shí)現(xiàn)的ViewPager引導(dǎo)頁功能(動(dòng)態(tài)加載指示器)詳解
- Android控件ViewPager實(shí)現(xiàn)帶有動(dòng)畫的引導(dǎo)頁
- Android引導(dǎo)頁面的簡(jiǎn)單實(shí)現(xiàn)
- Android實(shí)現(xiàn)繞球心旋轉(zhuǎn)的引導(dǎo)頁效果
- Android實(shí)現(xiàn)漸變啟動(dòng)頁和帶有指示器的引導(dǎo)頁
- RxJava兩步打造華麗的Android引導(dǎo)頁
- Android自定義控件打造絢麗平行空間引導(dǎo)頁
相關(guān)文章
Android NDK開發(fā)的環(huán)境搭建與簡(jiǎn)單示例
本文主要介紹Android NDK的知識(shí),這里整理了相關(guān)資料,來說明如何搭建相應(yīng)環(huán)境和簡(jiǎn)單實(shí)例,幫助大家理解,有興趣的小伙伴可以參考下2016-09-09
Android 沉浸式狀態(tài)欄與隱藏導(dǎo)航欄實(shí)例詳解
沉浸式狀態(tài)欄是指狀態(tài)欄與ActionBar顏色相匹配,隱藏導(dǎo)航欄,就是將導(dǎo)航欄隱藏,去掉下面的黑條。下面通過實(shí)例給大家詳解android沉浸式狀態(tài)欄與隱藏導(dǎo)航欄,感興趣的朋友一起看看2017-07-07
Android無障礙自動(dòng)化結(jié)合opencv實(shí)現(xiàn)支付寶能量自動(dòng)收集操作方法
opencv可以進(jìn)行圖像識(shí)別,兩者結(jié)合在一起即可實(shí)現(xiàn)支付寶能量自動(dòng)收集,opencv用于識(shí)別能量,無障礙服務(wù)用于模擬手勢(shì),即點(diǎn)擊能量,這篇文章主要介紹了Android無障礙自動(dòng)化結(jié)合opencv實(shí)現(xiàn)支付寶能量自動(dòng)收集,需要的朋友可以參考下2024-07-07
Android編程設(shè)計(jì)模式之抽象工廠模式詳解
這篇文章主要介紹了Android編程設(shè)計(jì)模式之抽象工廠模式,結(jié)合實(shí)例形式詳細(xì)分析了Android抽象工廠模式的概念、原理、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12
Android自定義ViewPager實(shí)現(xiàn)個(gè)性化的圖片切換效果
這篇文章主要介紹了Android自定義ViewPager實(shí)現(xiàn)個(gè)性化的圖片切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
android ScrollView實(shí)現(xiàn)下拉放大頭部圖片
這篇文章主要為大家詳細(xì)介紹了android ScrollView實(shí)現(xiàn)下拉放大頭部圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android照片墻應(yīng)用實(shí)現(xiàn) 再多的圖片也不怕崩潰
這篇文章主要為大家詳細(xì)介紹了Android照片墻應(yīng)用實(shí)現(xiàn),再多的圖片也不怕崩潰,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

