使用ViewPager指标实现自定义指标 - 如何在图库中执行此操作?

时间:2013-08-12 20:46:37

标签: android android-layout android-viewpager viewpagerindicator

我正在开展一个项目,我设法达到了这样的程度,即我可以使用PagerAdapter以图库的方式显示我的图像。现在我要做的是添加一个指示器,在屏幕上显示哪个图像,我认为VPI可以帮助我 - 问题是我对代码真的很新,不知道如何实现它,可能是某人帮助

以下是我想要实现的内容的绿色指标:http://imgur.com/GgYgY8I

我制作了一个更简单的项目版本以适应这篇文章,这里是:

ImageAdapter.java

package com.exp.viewpagersstest1;

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

public class ImageAdapter extends PagerAdapter {

    Context ssContext;

    private int[] ssImages = new int[] { R.drawable.splash1,R.drawable.splash2, R.drawable.splash3 };

    ImageAdapter(Context ssContext) {
        this.ssContext = ssContext;
    }

    @Override
    public int getCount() {
        return ssImages.length;
    }

    @Override
    public boolean isViewFromObject(View ssView, Object ssObject) {
        return ssView == ((ImageView) ssObject);
    }

    @Override
    public Object instantiateItem(ViewGroup ssContainer, int ssPosition) {
        ImageView ssImageView = new ImageView(ssContext);
        ssImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        ssImageView.setImageResource(ssImages[ssPosition]);
        ((ViewPager) ssContainer).addView(ssImageView, 0);
        return ssImageView;
    }

    @Override
    public void destroyItem(ViewGroup ssContainer, int ssPosition,
            Object ssObject) {
        ((ViewPager) ssContainer).removeView((ImageView) ssObject);
    }

}

SSTest.java

package com.exp.viewpagersstest1;

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

public class SSTest extends Activity {

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

        ViewPager ssViewPager = (ViewPager) findViewById(R.id.ss_view_pager);
        ImageAdapter ssAdapter = new ImageAdapter(this);
        ssViewPager.setAdapter(ssAdapter);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sstest, menu);
        return true;
    }

activity_sstest.xml

<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"
    tools:context=".SSTest" >

    <android.support.v4.view.ViewPager
        android:id="@+id/ss_view_pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:adjustViewBounds="true" />

</RelativeLayout>

ViewPagerSSTest1清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exp.viewpagersstest1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.exp.viewpagersstest1.SSTest"
            android:label="@string/app_name"
            android:theme="@style/Theme.Sherlock.NoActionBar"
             >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

提前感谢任何回复的人。

1 个答案:

答案 0 :(得分:0)

比我想象的要简单得多,只需要将以下内容添加到我的xml中:

<com.viewpagerindicator.UnderlinePageIndicator 
    android:id="@+id/pageIndic"
    android:layout_height="3dp"
    android:layout_width="fill_parent"
    android:layout_alignParentBottom="true"
    app:selectedColor="#ef4154"/>

以下是java文件下的oncreate:

UnderlinePageIndicator pageIndicator = (UnderlinePageIndicator) findViewById(R.id.pageIndic);
pageIndicator.setViewPager(ssViewPager);