第三個引導頁新增button按鈕(滑動viewpager)
阿新 • • 發佈:2019-01-27
我們今天所說的是在專案的歡迎頁的中最後一頁的button點選事件
第一種:如果說是歡迎頁都是一張圖片的話,最後一頁的按鈕還得我們自己來新增的話,那麼我們只需在佈局里弄上一個viewpager和button的控制元件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".activity.GuideActivity">
<android.support.v4.view.ViewPager
android:id="@+id/guide_vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/guide_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="80dp"
android:background="@mipmap/bg_splash_start_3_btn" />
</LinearLayout >
ImageView就在程式碼裡實現就好了
1、先在歡迎頁宣告三張歡迎頁和viewpager,ImageView的list集合,還有Adapter;
private ImageView iv1, iv2, iv3;
private ViewPager vp;
private Button startBtn;
private List<View>list;
private ImgVPAdapter vpAdapter;
2、然後初始化圖片,並且優化圖片;
iv1 = new ImageView(this);
iv2 = new ImageView(this);
iv3 = new ImageView(this);
//放縮圖片
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.bg_splash_start_1);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.mipmap.bg_splash_start_2);
Bitmap bitmap3 = BitmapFactory.decodeResource(getResources(), R.mipmap.bg_splash_start_3);
//矩陣物件
Matrix matrix = new Matrix();
//縮放原圖
matrix.postScale(1f, 1f);
bitmap1 = Bitmap.createBitmap(bitmap1, 0, 0, bitmap1.getWidth(), bitmap1.getHeight(), matrix, true);
bitmap2 = Bitmap.createBitmap(bitmap2, 0, 0, bitmap2.getWidth(), bitmap2.getHeight(), matrix, true);
bitmap3 = Bitmap.createBitmap(bitmap3, 0, 0, bitmap3.getWidth(), bitmap3.getHeight(), matrix, true);
iv1.setImageBitmap(bitmap1);
iv2.setImageBitmap(bitmap2);
iv3.setImageBitmap(bitmap3);
//設定圖片充滿螢幕
iv1.setScaleType(ImageView.ScaleType.FIT_XY);
iv2.setScaleType(ImageView.ScaleType.FIT_XY);
iv3.setScaleType(ImageView.ScaleType.FIT_XY);
3、初始化list集合,然後繫結圖片;
list=new ArrayList<ImageView>();
list.add(iv1);
list.add(iv2);
list.add(iv3);
4、初始化viewpager,然後繫結list集合,設立viewpager的點選事件;
vp = (ViewPager) findViewById(R.id.guide_vp);
vpAdapter=new ImgVPAdapter(list);
vp.setAdapter(vpAdapter);
//確定是否點選按鈕進入應用
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position==list.size()-1){
startBtn.setVisibility(View.VISIBLE);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor =preferences.edit();
editor.putBoolean("isFirst",false);
editor.commit();
Intent intent =new Intent(GuideActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
這個就不貼原始碼了!
第二種:就是有三個引導頁,引導頁裡還有三張圖片,最後一頁的引導頁裡面有一個button來點選進入首頁。那我們就說說如何把這三張圖片放到一起,新增到程式碼裡。
1、首先我們先建立第一個引導頁的LinearLayout的佈局,把三張圖片垂直佈局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@mipmap/ic_launcher"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@mipmap/ic_launcher"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginTop="20dp"
android:background="@mipmap/ic_launcher"/>
</LinearLayout>
效果是這樣的:
2、第二頁的引導頁與第一頁的佈局是一樣的,在這就不多做說明了。
3、第三頁的引導頁的佈局就是兩張圖片,然後再加一個button,就可以了!
程式碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@mipmap/ic_launcher"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher"/>
<Button
android:id="@+id/startbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginTop="20dp"
android:background="@mipmap/start_btn"/>
</LinearLayout>
效果圖如下:
上面兩個是圖片,底下是一個button控制元件;
4、佈局我們寫完了,我們就可以回到程式碼裡頭來引入了;還有最主要的就是在主佈局裡新增上viewpager的控制元件;
1)宣告viewpager,View的list集合,viewpager的adapter
2)然後再用View來引入第一、二、三頁的佈局;
View view1 = View.inflate(this,R.layout.guide_start_one,null);
View view2 = View.inflate(this,R.layout.guide_start_two,null);
View view3 = View.inflate(this,R.layout.guide_start_three,null);
3)再把view與list集合繫結;
list=new ArrayList<View>();
list.add(view1);
list.add(view2);
list.add(view3);
4)再把list與adapter進行繫結,然後再把adapter與viewpager繫結起來;
vp =(ViewPager)findViewById(R.id.guide_vp);
vpAdapter=new ImgVPAdapter(list);
vp.setAdapter(vpAdapter);
5)然後設定viewpager的點選事件,主要是viewpager裡的button的;
還有一點,如何找到第三頁的button:用第三頁的view來找,並且設定出到第三頁顯示出來button;
view3.findViewById(R.id.startbtn).setVisibility(View.VISIBLE);
找到第三引導頁的button就設定其點選事件,跳到首頁;
public void onPageSelected(int position) {
if (position==list.size()-1){
view3.findViewById(R.id.startbtn).setVisibility(View.VISIBLE);
view3.findViewById(R.id.startbtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(GuideActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
注:寫button的點選事件要在viewpager的onPageSelected的方法裡寫!
第二種方法的activity裡程式碼如下:
package com.xiaogao.tingfm.activity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.xiaogao.tingfm.R;
import com.xiaogao.tingfm.adapter.ImgVPAdapter;
import java.util.ArrayList;
import java.util.List;
public class GuideActivity extends Activity {
private ViewPager vp;
private List<View>list;
private ImgVPAdapter vpAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide);
initView();
}
private void initView() {
View view1 = View.inflate(this,R.layout.guide_start_one,null);
View view2 = View.inflate(this,R.layout.guide_start_two,null);
final View view3 = View.inflate(this,R.layout.guide_start_three,null);
list=new ArrayList<View>();
list.add(view1);
list.add(view2);
list.add(view3);
preferences=getSharedPreferences("config",MODE_PRIVATE);
vp = (ViewPager) findViewById(R.id.guide_vp);
vpAdapter=new ImgVPAdapter(list);
vp.setAdapter(vpAdapter);
//確定是否點選按鈕進入應用
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position==list.size()-1){
view3.findViewById(R.id.startbtn).setVisibility(View.VISIBLE);
view3.findViewById(R.id.startbtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(GuideActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}