53 Android fragment ViewPager 滑动效果

来源:互联网 发布:淘宝刷到钻石多少钱 编辑:程序博客网 时间:2024/06/09 13:55

layout 文件下 三个 xml 布局文件 (View)

f1.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:orientation="vertical" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/a1" /></LinearLayout>

f2.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:orientation="vertical" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/a2" /></LinearLayout>


f3.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:orientation="vertical" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/a4" /></LinearLayout>


activity_main.xml (主布局  里面加 ViewPager)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"            >     <!-- 看作一个控件 -->    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="match_parent"        android:layout_height="match_parent"         android:layout_gravity="center"        >        <!-- 标题 -->        <android.support.v4.view.PagerTitleStrip            android:id="@+id/pagerTitleStrip"            android:layout_width="match_parent"            android:layout_height="20dp"            android:layout_gravity="top" >        </android.support.v4.view.PagerTitleStrip>    </android.support.v4.view.ViewPager>    </LinearLayout>

MainActivity.java (主要代码)

package com.example.android_fragment_viewpager;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.PagerTitleStrip;import android.support.v4.view.ViewPager;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;/** * 侧滑的实现 * @author Administrator * */public class MainActivity extends Activity {private ViewPager viewPager;//表示ViewPager 显示的内容private List<View> contentlist;//表示ViewPager 显示的标题private List<String> title;private LayoutInflater inflater;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);viewPager=(ViewPager)findViewById(R.id.viewpager);inflater=LayoutInflater.from(this);//使用静态contentlist=new ArrayList<View>();//给每一个 Page 添加一个 ViewView firstView=inflater.inflate(R.layout.f1, null);View secondView=inflater.inflate(R.layout.f2, null);View thirdView=inflater.inflate(R.layout.f3, null);contentlist.add(firstView);contentlist.add(secondView);contentlist.add(thirdView);//给每一页添加标题title=new ArrayList<String>();title.add("体育");title.add("娱乐");title.add("新闻");//设置适配器viewPager.setAdapter(new MyAdapter());}/**自定义适配器 *  * @author Administrator * */public class MyAdapter extends PagerAdapter{@Overridepublic Object instantiateItem(ViewGroup container, int position) {// TODO Auto-generated method stub    ((ViewPager)container).addView(contentlist.get(position));;return contentlist.get(position);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn contentlist.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn arg0==arg1;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {// TODO Auto-generated method stub((ViewPager)container).removeView(contentlist.get(position));//super.destroyItem(container, position, object);}//至少需要以上四个@Overridepublic CharSequence getPageTitle(int position) {// TODO Auto-generated method stubreturn title.get(position);}}@Overridepublic 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;}}






//专门用于 ViewPage 的适配器 FragmentPagerAdapter   FragmentStatePageAdapter 




0 0