viewPager的简单使用
2016-01-21 10:42
351 查看
代码下载地址: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; @Override protected 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() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.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() { @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 ==arg1; } @Override public int getCount() { // TODO Auto-generated method stub return views.size(); } @Override public void destroyItem(View container, int position, Object object) { // TODO Auto-generated method stub ((ViewPager)container).removeView(views.get(position)); } @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titles.get(position); } @Override public Object instantiateItem(View container, int position) { // TODO Auto-generated method stub ((ViewPager)container).addView(views.get(position)); //VIEW1 Button btn = (Button)findViewById(R.id.btn1); btn.setOnClickListener(new OnClickListener() { @Override public 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() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub TextView text2 = (TextView)findViewById(R.id.text2); text2.setText("View2"); //VIEW3 Button btn2 = (Button)findViewById(R.id.btn3); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub TextView text2 = (TextView)findViewById(R.id.text3); text2.setText("View3"); } }); } }); } }); // return views.get(position); } }; mViewPager.setAdapter(mPagerAdapter); } }
相关文章推荐
- 使用ViewPager实现高仿launcher左右拖动效果
- 灵活使用Android中ActionBar和ViewPager切换页面
- Android中viewPager使用指南
- Android 使用 ViewPager循环广告位的实现
- Android viewpager中动态添加view并实现伪无限循环的方法
- 实现轮转广告带底部指示的自定义ViewPager控件
- 自定义RadioButton和ViewPager实现TabHost带滑动的页卡效果
- Android基于ViewPager Fragment实现选项卡
- 使用ViewPager实现android软件使用向导功能实现步骤
- Android编程ViewPager回弹效果实例分析
- Android 使用ViewPager自动滚动循环轮播效果
- Android ViewPager相册横向移动的实现方法
- Android 使用viewpager实现无限循环(定时+手动)
- Android编程实现ListView头部ViewPager广告轮询图效果
- Android 利用ViewPager实现图片可以左右循环滑动效果附代码下载
- Android编程开发ScrollView中ViewPager无法正常滑动问题解决方法
- 使用ViewPager+Fragment实现顶部导航栏
- viewpager的layout_width="wrap_content"无效问题
- ScrollView ViewPager嵌套导致滑动冲突解决方案
- Fragment+viewpager+Fragment 嵌套 第二次进入该fragment不显示数据