欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ViewPager

程序员文章站 2022-06-09 20:27:43
...

1.是什么:页面切换组件,可以实现左右滑动,从而切换不同的View。

2.在xml文件中建立。(必须纯手工打全)

<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="360dp"
    android:layout_marginTop="30dp"
    android:clipChildren="false" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="100dp"
        android:layout_marginRight="100dp"
        android:clipChildren="false" >
    </android.support.v4.view.ViewPager>

</RelativeLayout>

3.在Layout文件夹中建立三个视图界面。

ViewPager

4.在MainActivity.java中建立代码

package com.example.asdfa;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity {
	private ViewPager viewpager1;
	private List<View> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		viewpager1 = (ViewPager) findViewById(R.id.viewpager);
		View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.a,
				null);
		View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.b,
				null);
		View view3 = LayoutInflater.from(MainActivity.this).inflate(R.layout.c,
				null);
		list = new ArrayList<View>();
		list.add(view1);
		list.add(view2);
		list.add(view3);
		viewpager1.setAdapter(new MyAdapter());
	}

	class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();

		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			// super.destroyItem(container, position, object);
			((ViewPager) container).removeView(list.get(position));
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return super.getPageTitle(position);

		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub
			((ViewPager) container).addView(list.get(position));
			return list.get(position);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;

		}

	}

}

6ViewPager

 

viewpager实现界面中间的间隔空隙如下实现:

viewpager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.page_margin));

该命令在初始界面打。后面的id在dimen中添加间隔为20dip

viewpager获取当前的页面如下:

Toast.makeText(TestLieKongActivity.this, "选择"+viewpager.getCurrentItem(), 2000).show();

 

 

 

 

相关标签: Viewpager