ViewPager+PagerAdapter實(shí)現(xiàn)帶指示器的引導(dǎo)頁
更新時間:2020年09月23日 09:45:11 作者:hellcw
這篇文章主要為大家詳細(xì)介紹了ViewPager+PagerAdapter實(shí)現(xiàn)帶指示器的引導(dǎo)頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
一個簡單的引導(dǎo)頁,由ViewPager和PagerAdapter組成,供大家參考,具體內(nèi)容如下
package com.xspacing.viewpager;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
/**
*
* @ClassName MainActivity.java
* @Description TODO 引導(dǎo)頁
* @author Smile
* @version v1.0
* @date 2016年9月26日
*/
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
ViewPager mViewPager;
List<ImageView> list;
private LinearLayout mLinearLayout;
// 兩個小灰點(diǎn)的距離
private int poitWidth;
private ImageView mViewPress;
private ImageView mViewNotPress;
private Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initDatas();
}
private void initViews() {
mViewPager = (ViewPager) findViewById(R.id.main_view_pager);
mLinearLayout = (LinearLayout) findViewById(R.id.main_point);
mViewPress = (ImageView) findViewById(R.id.main_red_point_press);
btnStart = (Button) findViewById(R.id.main_btn);
}
private void initDatas() {
list = new ArrayList<ImageView>();
final int imageId[] = { R.drawable.a, R.drawable.b, R.drawable.c };
for (int i = 0; i < imageId.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource(imageId[i]);
list.add(imageView);
mViewNotPress = new ImageView(this);
mViewNotPress.setImageResource(R.drawable.shape_grey_point_not_press);
LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
if (i != 0) {
layoutParams.leftMargin = 20;
}
mLinearLayout.addView(mViewNotPress, layoutParams);
}
// 由于在執(zhí)行onCreate()的時候,界面還沒有被繪制完成,所有拿不到poitWidth,
// 要拿到視圖樹,監(jiān)聽視圖的繪制完成,才能拿到poitWidth
mViewPress.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
poitWidth = mLinearLayout.getChildAt(1).getLeft() - mLinearLayout.getChildAt(0).getLeft();
Log.i(TAG, poitWidth + "");
}
});
mViewPager.setAdapter(new ViewPgerAdapter(this, list));
mViewPager.addOnPageChangeListener(new OnPageChangeListener() {
// 當(dāng)前選中的是第幾個界面
@Override
public void onPageSelected(int position) {
if (position==imageId.length-1) {
btnStart.setVisibility(View.VISIBLE);
}
else {
btnStart.setVisibility(View.GONE);
}
}
// 界面滑動的時候,會不斷地回調(diào)這個方法
// 第二個參數(shù)是當(dāng)前頁面滑過的百分比(0.0到1.0)
@Override
public void onPageScrolled(int position, float offset, int arg2) {
Log.i(TAG, "arg1:" + offset);
// 小紅點(diǎn)當(dāng)前滑動的距離
int width = (int) (poitWidth * offset + position * poitWidth);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mViewPress.getLayoutParams();
layoutParams.leftMargin = width;
mViewPress.setLayoutParams(layoutParams);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
}
package com.xspacing.viewpager;
import java.util.List;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ViewPgerAdapter extends PagerAdapter {
Context mContext;
List<ImageView> list;
public ViewPgerAdapter(Context context, List<ImageView> list) {
this.mContext = context;
this.list = list;
}
// 頁數(shù)
@Override
public int getCount() {
return list.size();
}
// 復(fù)用頁卡
@Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
// 銷毀頁卡
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(list.get(position));
}
// 生成頁卡
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = list.get(position);
container.addView(view);
return view;
}
}
shape文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:height="10dp"
android:width="10dp" />
<solid android:color="#C3C3C3"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<size
android:height="10dp"
android:width="10dp" />
<solid android:color="#44FF0000"/>
</shape>
<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.xspacing.viewpager.MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/main_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
<RelativeLayout
android:id="@+id/main_relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp" >
<LinearLayout
android:id="@+id/main_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
<ImageView
android:id="@+id/main_red_point_press"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape_red_point_press" />
</RelativeLayout>
<Button
android:id="@+id/main_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:visibility="gone"
android:layout_above="@id/main_relative"
android:layout_centerHorizontal="true"
android:text="開始體驗(yàn)" />
</RelativeLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android使用ViewPager完成app引導(dǎo)頁
- Android ViewPager實(shí)現(xiàn)滑動指示條功能
- Android使用viewpager實(shí)現(xiàn)畫廊式效果
- android使用ViewPager實(shí)現(xiàn)圖片自動切換
- RecyclerView+PagerSnapHelper實(shí)現(xiàn)抖音首頁翻頁的Viewpager效果
- Android自定義引導(dǎo)玩轉(zhuǎn)ViewPager的方法詳解
- TabLayout+ViewPager實(shí)現(xiàn)切頁的示例代碼
- android使用viewpager計(jì)算偏移量實(shí)現(xiàn)選項(xiàng)卡功能
- Android使用ViewPager實(shí)現(xiàn)滾動廣告
- ViewPager滑動靈敏度調(diào)整的方法實(shí)力
- Android Studio使用ViewPager+Fragment實(shí)現(xiàn)滑動菜單Tab效果
- Android 兩個ViewPager的聯(lián)動效果的實(shí)現(xiàn)
- ViewPager2滑動沖突解決方案
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)ListView異步加載數(shù)據(jù)的方法詳解
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)ListView異步加載數(shù)據(jù)的方法,結(jié)合具體實(shí)例形式分析了Android操作ListView實(shí)現(xiàn)異步加載數(shù)據(jù)的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-11-11
Android App開發(fā)中Gradle構(gòu)建過程的配置方法
這篇文章主要介紹了Android App開發(fā)中Gradle構(gòu)建過程的配置方法,包括在Gradle中配置manifest的方法,需要的朋友可以參考下2016-06-06
Android中fragment嵌套fragment問題解決方法
這篇文章主要介紹了Android中fragment嵌套fragment問題解決方法,本文給出兩個解決方法,需要的朋友可以參考下2015-06-06
Android嵌套滾動與協(xié)調(diào)滾動的實(shí)現(xiàn)方式匯總
如何實(shí)現(xiàn)這種協(xié)調(diào)滾動的布局呢,我們使用CoordinatorLayout+AppBarLayout或者CoordinatorLayout+Behavior實(shí)現(xiàn),另一種方案是MotionLayout,我們看看都是怎么實(shí)現(xiàn)的吧2022-06-06
Android加載html中svg格式圖片進(jìn)行顯示
這篇文章主要為大家詳細(xì)介紹了Android加載html中svg格式圖片進(jìn)行顯示,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05

