在ViewPager上显示指标

时间:2014-09-02 14:59:52

标签: android android-viewpager

我正在实施ViewPager来浏览3个片段。用户应该打开应用程序并从片段1开始,一旦完成,继续片段2.

为了让用户知道这一点,我正在尝试实现某种指示,当用户完成一个片段并且必须跟随下一个片段时,该指示应该出现。

为此,我决定将带有箭头的imageView放在GONE初始状态,当用户完成时该状态应该变为VISIBLE。这就是我编码的方式:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

     <ImageView
        android:id="@+id/arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:src="@drawable/arrow" />

    <android.support.v4.view.ViewPager 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.v4.view.PagerTabStrip
            android:id="@+id/pager_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:background="@color/Black"
            android:textAppearance="@style/PagerTabStripText" />     
    </android.support.v4.view.ViewPager>     
</RelativeLayout>

然后在MainActivity的onCreate()中我定义了imageView:

private ImageView arrow;

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

    arrow = (ImageView) findViewById(R.id.arrow);
    /*Make arrow gone*/
    arrow.setVisibility(View.GONE);

    viewPager = (ViewPager) findViewById(R.id.pager);
    vpAdapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(vpAdapter);

当用户完成后,我就会以这种方式看到它:

arrow.setVisibility(View.VISIBLE);

但我没有让它变得可见。

更新

它在xml文件中的外观

enter image description here

更新2

如果我在ViewPager下面的xml中设置ImageView,应用程序崩溃,这就是logcat所说的。

09-02 17:38:52.377: E/AndroidRuntime(11832): FATAL EXCEPTION: main
09-02 17:38:52.377: E/AndroidRuntime(11832): Process: com.itest, PID: 11832
09-02 17:38:52.377: E/AndroidRuntime(11832): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itest/com.itest.MainActivity}: java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.ImageView
09-02 17:38:52.377: E/AndroidRuntime(11832):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at android.os.Looper.loop(Looper.java:157)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at android.app.ActivityThread.main(ActivityThread.java:5356)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at java.lang.reflect.Method.invokeNative(Native Method)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at java.lang.reflect.Method.invoke(Method.java:515)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at dalvik.system.NativeStart.main(Native Method)
09-02 17:38:52.377: E/AndroidRuntime(11832): Caused by: java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.ImageView
09-02 17:38:52.377: E/AndroidRuntime(11832):    at com.itest.MainActivity.onCreate(MainActivity.java:102)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at android.app.Activity.performCreate(Activity.java:5426)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-02 17:38:52.377: E/AndroidRuntime(11832):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
09-02 17:38:52.377: E/AndroidRuntime(11832):    ... 11 more

4 个答案:

答案 0 :(得分:2)

尝试将RelativeLayout替换为FrameLayoutFrameLayout让您将View置于另一个之上,我认为您的问题是ViewPager始终位于箭头之上。

修改

您可以为箭头图像调用方法bringToFront(),将其放在所有内容之上。唯一的问题是你可以再将它移回去,但是如果你隐藏它就应该做到这一点。

答案 1 :(得分:0)

请更正您的代码:

private ImageView arrow;

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

  arrow = (ImageView) findViewById(R.id.arrow);
  /*Make arrow gone*/
  //arrow.setVisibility(View.GONE);
  arrow.setVisibility(View.INVISIABLE);

我认为,因为viewpager的属性是你设置你的图像,所以onCreate方法的可见性已经消失。

android:layout_width="match_parent"
android:layout_height="match_parent"

答案 2 :(得分:0)

为什么不使用JackWharton viewpagerindicator。

答案 3 :(得分:0)

您可以使用新引入的TabLayout执行此操作,并使用项目符号形式的字符串设置选项卡中的文本。检查完整的项目here