在Android中迭代ViewPager视图

时间:2014-04-03 19:05:27

标签: android android-fragments android-viewpager

我只是试图遍历我使用ViewPager创建的页面。每个页面都有一个我需要获取其值的textview。我可以获取当前片段,但我需要能够在for循环中指定页面,如下所示:

public void setEntryValues() {

        int numPages = mAdapter.getCount();

        JournalEntryFragment frag;
        View view;
        TextView txtView;

        for(int i = 0; i < numPages; i++) {

            frag = (JournalEntryFragment)mAdapter.instantiateItem(mPager,mPager.getCurrentItem());

            //frag = (JournalEntryFragment)mAdapter.getItem(i); // Doesn't work, returns null for view


            view = frag.getView();
            txtView = (TextView)view.findViewById(R.id.scale);
            int scale = Integer.valueOf(txtView.getText().toString());

            Log.v("rx", "scale: " + scale);

            if(i == 0) {
                mEntry.setPain(scale);
            } else if(i ==1) {
                mEntry.setStress(scale);
            } else {
                mEntry.setSleep(scale);
            }

            Log.v("rx", mEntry.toString());


        }


    }

getCurrentItem()工作正常,但仅适用于当前幻灯片。当我尝试使用getItem()时,我总是为视图获取null。

/ *更新 - 这是我的FragmentPagerAdapter * /

public class JournalPagerAdapter extends FragmentPagerAdapter {

    public JournalPagerAdapter(FragmentManager mgr) {
        super(mgr);
    }
    @Override
    public int getCount() {
        return(3);
    }
    @Override
    public Fragment getItem(int position) {
        return(JournalEntryFragment.newInstance(position));
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return super.getPageTitle(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

}

1 个答案:

答案 0 :(得分:1)

我认为您尝试在滑动时从viewpager适配器检索信息,并且您尝试通过循环实例化项目。您可能会发现此解决方案与您要求的解决方案略有不同。但无论如何我会写它。

以下是编写PagerAdapter的方法,

public class ViewPagerAdapter extends PagerAdapter {

    Context context;
    LayoutInflater inflater;
    ArrayList<Item> itemsList = new ArrayList<Item>();

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

    public ViewPagerAdapter(Context context, ArrayList<Item> items) {
        this.context = context;
        itemsList = items;
    }

    /**
     * This method returns the number of views to be displayed.
     */
    @Override
    public int getCount() {
        return itemsList.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        TextView txtMessage;
        TextView txtHeading;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewItem = inflater.inflate(R.layout.viewpagercontent, container, false);
        txtHeading = (TextView) viewItem.findViewById(R.id.txtHeadline);
        txtMessage = (TextView) viewItem.findViewById(R.id.txtMsg);

        container.addView(viewItem);
        return viewItem;
    }

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

如果您想要检索某个特定事件的信息(假设您在ViewPager上轻扫),您可以执行以下操作,

    viewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int pPosition) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int pPosition, float pPositionOffset, int pPositionOffsetPixels) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int pState) {
            // TODO Auto-generated method stub

        }
    });
}

希望它对你有所帮助。