3

我正在通过 SwipeyTabsAdapter 的库 ViewPager ViewPagerExtensions (com.astuetz.viewpager.extensions) 水平实现一个。有没有办法在改变观点时拦截?同样,我可以随时知道用户的观点是什么?

ViewPager 的代码

public class DiccionariuPagerAdapter extends PagerAdapter {

    protected transient Activity mContext;

    private int mLength = 0;

    public DiccionariuPagerAdapter(Activity context, int length) {
        mContext = context;
        mLength = length;
    }

    @Override
    public int getCount() {
        return mLength;
    }

    @Override
    public Object instantiateItem(View container, int position) {

        RelativeLayout v = new RelativeLayout(mContext);

        View vi; // Creating an instance for View Object
        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (position == 0) {            
            vi = inflater.inflate(R.layout.diccionariu, null);
        }
        else if (position == 1) {
            vi = inflater.inflate(R.layout.conxugar, null);
        }
        else {
            vi = inflater.inflate(R.layout.traducir, null);
        }

        v.addView(vi);

        //v.addView(t);

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

        startUpdate(container);

        return v;
    }

    @Override
    public void destroyItem(View container, int position, Object view) {
        ((ViewPager) container).removeView((View) view);
    }

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

    @Override
    public void finishUpdate(View container) {}

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {}

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {}

}

非常感谢您

4

2 回答 2

9

要获取当前项目,请使用:

int itemId = ViewPager.getCurrentItem()

有了它,您可以使用适配器从适配器中检索实际视图

Adapter.getItem(itemId)

你也可以使用监听器OnPageChangeListener

viewPager.setOnPageChangeListener(new OnPageChangeListener...
于 2012-08-08T11:33:15.787 回答
5

编译和工作:

~~~

    mPager.setOnPageChangeListener(new OnPageChangeListener()
    {
        @Override
        public void onPageSelected(int position) 
        {
                // if listener is set - when using an indicator, must update that here
                mIndicator.setCurrentItem(position);

                String msg = "onPageSelected - position: " + position;
                Log.d("debug1", msg);
        }

        @Override
        public void onPageScrollStateChanged(int state) {}

        @Override
        public void onPageScrolled(int position, float arg1, int arg2) {}
    });
于 2014-04-29T18:38:41.137 回答