«

Android-ViewPager使用示例

时间:2024-3-2 19:41     作者:韩俊     分类: Android


创建ViewPagerAdapter.java

package com.example.viewpager;

import java.util.List;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

public class ViewPagerAdapter extends PagerAdapter {

    private List<View> views;
    Context context;

    ViewPagerAdapter(List<View> views, Context context) {
        this.views = views;
        this.context = context;
    }

    @Override
    public int getCount() {
        // 返回当前count的数量
        return views.size();
    }

    // 移除当前view
    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView(views.get(position));
    }

    // 添加当前页
    @Override
    public Object instantiateItem(View container, int position) {
        ((ViewPager) container).addView(views.get(position));
        return views.get(position);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

}

创建Guide.java

package com.example.viewpager;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;

public class Guide extends Activity {
    ViewPager mViewPager;
    ViewPagerAdapter mViewPagerAdapter;
    List<View> views;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide);
        initViews();
    }

    void initViews() {
        LayoutInflater inflater = LayoutInflater.from(this);

        views = new ArrayList<View>();
        views.add(inflater.inflate(R.layout.gridview, null));
        mViewPagerAdapter = new ViewPagerAdapter(views, this);
        //实现mViewPager
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setAdapter(mViewPagerAdapter);
    }

}

创建guide.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <android.support.v4.view.ViewPager
        android:id = "@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height = "match_parent"
        android:background="#90EE90">
    </android.support.v4.view.ViewPager>
</RelativeLayout>

创建gridview.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" >

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFE0" >
    </GridView>

    <TextView android:background="#FAEBD7" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp">
    </TextView>

</LinearLayout>

配置文件

<activity android:name="com.example.viewpager.Guide">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>

标签: android

热门推荐