Android_ViewPage_手势滑动

Posted zlfxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android_ViewPage_手势滑动相关的知识,希望对你有一定的参考价值。

学习笔记整理,利用ViewPage实现手势滑动。

1、先说说布局文件吧。

(1)主布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <android.support.v4.view.ViewPager
            android:id="@+id/guidePages"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/viewGroup"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="60dp"
                android:gravity="center_horizontal"
                android:orientation="horizontal" >
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

</FrameLayout>
(2)图片的布局文件item01.xml,五个图片文件都是同样的布局,就不一一列举了。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/tmall_pic_g1" />

</LinearLayout>
2、主类文件MainActivity。
package com.viewpage.demo;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity


	private ViewPager viewPager;
	private ArrayList<View> pageViews;
	private ImageView imageView;
	private ImageView[] imageViews;
	// 包裹滑动图片LinearLayout
	private ViewGroup main;
	// 包裹小圆点的LinearLayout
	private ViewGroup group;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 设置无标题窗口
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		LayoutInflater inflater = getLayoutInflater();
		pageViews = new ArrayList<View>();
		
		pageViews.add(inflater.inflate(R.layout.item01, null));
		pageViews.add(inflater.inflate(R.layout.item02, null));
		pageViews.add(inflater.inflate(R.layout.item03, null));
		pageViews.add(inflater.inflate(R.layout.item04, null));
		//pageViews.add(inflater.inflate(R.layout.item05, null));

		View find = inflater.inflate(R.layout.item05, null);
		Button button = (Button) find.findViewById(R.id.NavigateHome);
		button.setOnClickListener(new OnClickListener()
		
			
			@Override
			public void onClick(View v)
			
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,MainActivity.class);
				startActivity(intent);
				finish();
			
		);
		
		pageViews.add(find);
		
		imageViews = new ImageView[pageViews.size()];
		main = (ViewGroup) inflater.inflate(R.layout.activity_main, null);
		group = (ViewGroup) main.findViewById(R.id.viewGroup);
		viewPager = (ViewPager) main.findViewById(R.id.guidePages);

		for (int i = 0; i < pageViews.size(); i++)
		
			imageView = new ImageView(MainActivity.this);
			imageView.setLayoutParams(new LayoutParams(30, 30));
			imageView.setPadding(20, 0, 20, 0);
			imageViews[i] = imageView;

			if (i == 0)
			
				// 默认选中第一张图片
				imageViews[i]
						.setBackgroundResource(R.drawable.point_on);
			 else
			
				imageViews[i].setBackgroundResource(R.drawable.point);
			

			group.addView(imageViews[i]);
		

		setContentView(main);

		viewPager.setAdapter(new GuidePageAdapter());
		viewPager.setOnPageChangeListener(new GuidePageChangeListener());
	
	

	// 指引页面数据适配器
	class GuidePageAdapter extends PagerAdapter
	

		@Override
		public int getCount()
		
			return pageViews.size();
		

		@Override
		public boolean isViewFromObject(View arg0, Object arg1)
		
			return arg0 == arg1;
		

		@Override
		public int getItemPosition(Object object)
		
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2)
		
			// TODO Auto-generated method stub
			((ViewPager) arg0).removeView(pageViews.get(arg1));
		

		@Override
		public Object instantiateItem(View arg0, int arg1)
		
			// TODO Auto-generated method stub
			((ViewPager) arg0).addView(pageViews.get(arg1));
			return pageViews.get(arg1);
		

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1)
		
			// TODO Auto-generated method stub

		

		@Override
		public Parcelable saveState()
		
			// TODO Auto-generated method stub
			return null;
		

		@Override
		public void startUpdate(View arg0)
		
			// TODO Auto-generated method stub

		

		@Override
		public void finishUpdate(View arg0)
		
			// TODO Auto-generated method stub

		
	

	// 指引页面更改事件监听器
	class GuidePageChangeListener implements OnPageChangeListener
	

		@Override
		public void onPageScrollStateChanged(int arg0)
		
			// TODO Auto-generated method stub

		

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2)
		
			// TODO Auto-generated method stub

		

		@Override
		public void onPageSelected(int arg0)
		
			for (int i = 0; i < imageViews.length; i++)
			
				imageViews[arg0]
						.setBackgroundResource(R.drawable.point_on);

				if (arg0 != i)
				
					imageViews[i]
							.setBackgroundResource(R.drawable.point);
				
			
		
	
3、实现的效果如图:

本文例子所用的图片来自天猫的apk,反编译天猫apk后,提取他的图片。


4、如需源代码练习的朋友,请从此下载:

http://download.csdn.net/download/zlfxy/5212004

以上是关于Android_ViewPage_手势滑动的主要内容,如果未能解决你的问题,请参考以下文章

uni-app踩坑:微信小程序中view设置overflow: auto无法手势滑动

关于移动端滑动手势

Swift 3 中的滑动手势

是否可以将手势识别器附加到按钮,以便用户在按下按钮之后/期间向上滑动?

使用滑动手势在 swift3 中编辑表格视图单元格

ios 滑动手势 怎么判断左右滑动