带TextView的ViewPager显示奇怪的结果

时间:2015-08-10 16:56:57

标签: android

我有一个ViewPager和一个TextView,有些奇怪的事情正在发生。 我的代码中有一个Images数组,String中有一个String数组

  <string-array name="helper_text">
        <item>first</item>
        <item>second</item>
        <item>third</item>
        <item>fourth</item>
    </string-array>

这是我的xml文件

<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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.toyberman.tmdb.HelpActivity"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager
        android:id="@+id/help_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/tv_help"
        android:layout_below="@+id/help_pager"

        />
</LinearLayout>

这是我的java代码

package com.toyberman.tmdb;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class HelpActivity extends AppCompatActivity {

    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        //finding view pager
        viewPager = (ViewPager) findViewById(R.id.help_pager);

        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewPager.setAdapter(adapter);
    }

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


    private class ImagePagerAdapter extends PagerAdapter {

        private String [] text;

        private Integer[] images = {
                R.drawable.first, R.drawable.second
                , R.drawable.third, R.drawable.fourth
        };

        public ImagePagerAdapter() {
            this.text = getResources().getStringArray(R.array.helper_text);
        }

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

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

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }

        @Override
        public Object instantiateItem(final ViewGroup container, int position) {
            Context context = getApplicationContext();
            final ImageView imageView = new ImageView(context);

            TextView tv= (TextView) findViewById(R.id.tv_help);
            tv.setText(text[position]);
            imageView.setImageBitmap(
                    decodeSampledBitmapFromResource(getResources(),images[position],500, 500));


            ((ViewPager) container).addView(imageView, 0);

            return imageView;
        }

    }

}

问题是String数组中的字符串与图像不对应。第一张图片应该是&#34;第一张&#34;字符串,但我看到&#34;第二&#34;代替。

谢谢!

1 个答案:

答案 0 :(得分:1)

InstantiateItem 上,int position参数是正在呈现的项目的位置,当前焦点项目的位置。您可以((ViewPager) container).getCurrentItem()获取当前项目。

希望这有帮助。