首页 > 代码库 > ViewPager
ViewPager
package com.example.viewpagertest;import java.util.ArrayList;import java.util.List;import java.util.zip.Inflater;import android.support.v4.view.PagerTabStrip;import android.support.v4.view.ViewPager;import android.support.v7.app.ActionBarActivity;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.Window;public class MainActivity extends Activity { private List<View> list; private ViewPager pager; private List<String> titList; private PagerTabStrip tabStrip;// 属于Pager的一个子标签 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); list = new ArrayList<View>(); /** * 通过View对象最为viewpager的数据源 */ View view1 = View.inflate(this, R.layout.view1, null); View view2 = View.inflate(this, R.layout.view2, null); View view3 = View.inflate(this, R.layout.view3, null); View view4 = View.inflate(this, R.layout.view4, null); list.add(view1); list.add(view2); list.add(view3); list.add(view4); // 为页卡设置标题 titList = new ArrayList<String>(); titList.add("第一页"); titList.add("第二页"); titList.add("第三页"); titList.add("第四页"); // 为PagerTabStrip设置一些属性 tabStrip = (PagerTabStrip) findViewById(R.id.tab); tabStrip.setTextColor(Color.BLACK); tabStrip.setBackgroundResource(R.drawable.ic_biaoti); tabStrip.setDrawFullUnderline(true);// 没有下划线 tabStrip.setTabIndicatorColor(Color.GRAY);// 粗线的颜色 // 初始化ViewPager pager = (ViewPager) findViewById(R.id.pager); // 创建PagerAdapter的适配器 MyPagerAdapter adapter = new MyPagerAdapter(list, titList); // viewPager加载适配器 pager.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
package com.example.viewpagertest;import java.util.List;import java.util.zip.Inflater;import android.app.Fragment;import android.support.v4.view.PagerAdapter;import android.view.View;import android.view.ViewGroup;public class MyPagerAdapter extends PagerAdapter { private List<View> list; private List<String> titList; public MyPagerAdapter(List<View> list, List<String> titList) { // TODO Auto-generated constructor stub this.list = list; this.titList = titList; } /** * 返回的是页卡的数量 * * **/ @Override public int getCount() { // TODO Auto-generated method stub return list.size();// 页卡的数量等于集合的大小 } /** * View对象是否来自于对象 */ @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } /** * 实例化一个页卡 */ @Override public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(list.get(position)); return list.get(position); } /** * 销毁一个页卡 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(list.get(position)); } /**设置页卡的标题 * 在添加标题之后加一个getPagerTitle方法 * */ @Override public CharSequence getPageTitle(int position) { // TODO Auto-generated method stub return titList.get(position); }}
package com.example.viewpagertest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view1, container, false); }}
package com.example.viewpagertest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view2, container, false); }}
package com.example.viewpagertest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view3, container, false); }}
package com.example.viewpagertest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment4 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.view4, container, false); }}
<?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:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" > <android.support.v4.view.PagerTabStrip android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="60dp" android:layout_gravity="top" > </android.support.v4.view.PagerTabStrip> </android.support.v4.view.ViewPager></LinearLayout>
<?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:orientation="vertical" > <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个页面" /></LinearLayout>
<?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:orientation="vertical" > <TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三个页面" />/></LinearLayout>
<?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:orientation="vertical" > <TextView android:id="@+id/text4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第四个页面" />/></LinearLayout>
ViewPager
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。