PagerAdapter中的位置无法正常工作

时间:2012-02-28 17:49:21

标签: android horizontalscrollview

对于我的应用程序,我需要水平滚动,我在http://code.google.com/p/android-dc-tutorial-projects/source/browse/trunk/SuperSimpleViewPager/src/com/mamlambo/simpleviewpager/SimpleViewPagerActivity.java?r=5上找到了示例,但是此示例可能包含错误,或者我不知道如何使用PageAdapter。

如果我按照示例中的方式记录位置,我会收到以下消息:

02-28 18:43:35.701: I/pos:(18089): 2
02-28 18:43:36.568: I/pos:(18089): 3
02-28 18:43:37.279: I/pos:(18089): 4
02-28 18:43:39.556: I/pos:(18089): 2
02-28 18:43:40.084: I/pos:(18089): 1
02-28 18:43:40.689: I/pos:(18089): 0
02-28 18:43:42.584: I/pos:(18089): 2
02-28 18:43:43.119: I/pos:(18089): 3
02-28 18:43:43.685: I/pos:(18089): 4
02-28 18:43:51.892: I/pos:(18089): 2
02-28 18:43:52.298: I/pos:(18089): 1
02-28 18:43:52.756: I/pos:(18089): 0

总是跳过一个位置,为什么???我尝试增加/减少视图,但我得到随机位置。我糊涂了???我注意到有问题的开始和结束位置。

package com.mamlambo.simpleviewpager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

public class SimpleViewPagerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MyPagerAdapter adapter = new MyPagerAdapter();
        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(5);
    }

    public void farLeftButtonClick(View v)
    {
        Toast.makeText(this, "Far Left Button Clicked", Toast.LENGTH_SHORT).show(); 

    }

    public void farRightButtonClick(View v)
    {
        Toast.makeText(this, "Far Right Elephant Button Clicked", Toast.LENGTH_SHORT).show(); 

    }

    private class MyPagerAdapter extends PagerAdapter {

        public int getCount() {
            return 5;
        }

        public Object instantiateItem(View collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            Log.i("pos: ", position+"");
            int resId = 0;
            switch (position) {
            case 0:
                resId = R.layout.farleft;
                break;
            case 1:
                resId = R.layout.left;
                break;
            case 2:
                resId = R.layout.middle;
                break;
            case 3:
                resId = R.layout.right;
                break;
            case 4:
                resId = R.layout.farright;
                break;
            }

            View view = inflater.inflate(resId, null);

            ((ViewPager) collection).addView(view, 0);

            return view;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);

        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

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

        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

    }

}

2 个答案:

答案 0 :(得分:2)

您看到的行为是因为offScreenPageLimit,其默认值为1。基本上,这决定了当前索引左右两侧的位置应该加载到内存中,以便在滑动过程中提高性能。

您可以将值更改为适合您需要的值,尽管我不建议您将其设置为0。相关方法是setOffScreenPageLimit(int)

答案 1 :(得分:0)

您还可以将OnPageChangeListener设置为ViewPager并使用此界面的方法onPageSelected