viewPager的简单使用

来源:互联网 发布:jenkins源码管理none 编辑:程序博客网 时间:2024/04/26 08:35
代码下载地址:http://download.csdn.net/detail/u011324501/9412552
http://download.csdn.net/detail/u011324501/9412610
添加布局文件:main.xml</span>
<?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" >    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" >         <android.support.v4.view.PagerTitleStrip            android:id="@+id/pagertitle"            android:layout_width="wrap_content"            android:layout_height="30dip"            android:layout_gravity="top" />    </android.support.v4.view.ViewPager> </LinearLayout>

添加布局文件:view1.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#00cc66"    android:orientation="vertical" >    <Button    android:id="@+id/btn1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"/><TextView    android:id="@+id/text"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="30sp"    android:textColor="#000000"    android:text="view1"/></LinearLayout>

添加view2布局文件:view2.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#ffffcc"    android:orientation="vertical" >        <Button    android:id="@+id/btn2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"/><TextView    android:id="@+id/text2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="30sp"    android:textColor="#000000"    android:text=""/></LinearLayout>
添加布局文件:view3.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#00ccff"    android:orientation="vertical" >     <Button    android:id="@+id/btn3"    android:layout_width="wrap_content"    android:layout_height="wrap_content"/><TextView    android:id="@+id/text3"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="30sp"    android:textColor="#000000"    android:text=""/></LinearLayout>
添加MainActivity.java
package com.example.viewpager;import java.util.ArrayList;import android.os.Bundle;import android.app.Activity;import android.support.v4.view.PagerAdapter;import android.support.v4.view.PagerTitleStrip;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private ViewPager mViewPager;private PagerTitleStrip mPagerTitleStrip;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mViewPager = (ViewPager)findViewById(R.id.viewpager);mPagerTitleStrip = (PagerTitleStrip)findViewById(R.id.pagertitle);//上面的标题添加的监听器mPagerTitleStrip.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubToast.makeText(getApplication(), "dff", Toast.LENGTH_SHORT).show();}});//将分页显示的View装入数组中LayoutInflater mInflater = LayoutInflater.from(getApplication());View view1 = mInflater.inflate(R.layout.view1, null);View view2 = mInflater.inflate(R.layout.view2, null);View view3 = mInflater.inflate(R.layout.view3, null);//每个页面的Title数据final ArrayList<View> views = new ArrayList<View>();views.add(view1);views.add(view2);views.add(view3);final ArrayList<String> titles = new ArrayList<String>();titles.add("view1");titles.add("view2");titles.add("view3");//填充viewpager的数据适配器PagerAdapter mPagerAdapter = new PagerAdapter() {@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0 ==arg1;}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn views.size();}@Overridepublic void destroyItem(View container, int position, Object object) {// TODO Auto-generated method stub((ViewPager)container).removeView(views.get(position));}@Overridepublic CharSequence getPageTitle(int position) {// TODO Auto-generated method stubreturn titles.get(position);}@Overridepublic Object instantiateItem(View container, int position) {// TODO Auto-generated method stub((ViewPager)container).addView(views.get(position));//VIEW1Button btn = (Button)findViewById(R.id.btn1);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub   TextView text = (TextView)findViewById(R.id.text);   text.setText("view1");   //VIEW2   Button btn2 = (Button)findViewById(R.id.btn2);   btn2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubTextView text2 = (TextView)findViewById(R.id.text2);text2.setText("View2");//VIEW3Button btn2 = (Button)findViewById(R.id.btn3);   btn2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubTextView text2 = (TextView)findViewById(R.id.text3);text2.setText("View3");}});}});   }});//return views.get(position);}};mViewPager.setAdapter(mPagerAdapter);}}


1 0