1. 程式人生 > >Android ViewPager使用詳解,載入幾個簡單佈局案例+程式碼

Android ViewPager使用詳解,載入幾個簡單佈局案例+程式碼

MainActivity程式碼如下:
package com.example.viewpage;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extendsAppCompatActivity {

    private List<View> viewList
; private ViewPager pager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewList= new ArrayList<View>(); pager = (ViewPager) findViewById(R.id.pager); 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); viewList.add(view1); viewList.add(view2); viewList.add(view3); viewList.add(view4); //建立視訊日期 MyPageAdapter adapter = new MyPageAdapter(viewList
); pager.setAdapter(adapter); } }

自定義的PageView的介面卡程式碼如下:

package com.example.viewpage;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
 * Created by 聯想 on 2016/7/4.
 */
public class MyPageAdapter extends PagerAdapter{
    private List<View> viewList;
    public MyPageAdapter(List<View> viewList)
    {
        this.viewList = viewList;
}

    @Override
public int getCount() {
        return viewList.size();
}

    @Override
public boolean isViewFromObject(View view, Object object) {
        return view == object;
}

    @Override
public Object instantiateItem(ViewGroup container, int position) {
        container.addView(viewList.get(position));
        return viewList.get(position);
}

    @Override
public void destroyItem(ViewGroup container, int position, Object object) {
        //super.destroyItem(container, position, object);
        //container.removeView(viewList.get(position));
}
}

activity_main的佈局程式碼如下:

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.viewpage.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
</android.support.v4.view.ViewPager>
</RelativeLayout>

在依次新建四個佈局檔案,view1,view2,view3,view4。他們的程式碼如下:

<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一個介面"/>
</LinearLayout>

o了。。