PagerAdapter最初调用isViewFromObject两次

时间:2017-12-07 13:02:46

标签: android layout android-viewpager android-pageradapter

我正在尝试使用PagerAdapter实现viewpager重写方法isViewFromObject最初为同一视图调用两次。

ViewPagerAdapater.java

  public class ViewPagerAdapater extends PagerAdapter {
        HashMap<Integer,List<Sites>> mSiteList = null;
        Context mContext = null;
        public ViewPagerAdapater(Context context, HashMap<Integer,List<Sites>>  siteList){
            mSiteList = siteList;
            mContext  = context;
        }



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

            LayoutInflater inflater = LayoutInflater.from(mContext);
            ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.viewpager_mobile_single, container, false);
            container.addView(layout);
            return layout;

        }

        @Override
        public int getCount() {
            return mSiteList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            Rect rec =new Rect();
            if(view == object){

                CustomViewPageChild mSelectedChild = (CustomViewPageChild)view;
                if(view.getGlobalVisibleRect(rec))
                {
                    System.out.println("View Visible  true "+rec.flattenToString()); //this is printing two times
                }
            }
            return view == object;


        }

        @Override
        public CharSequence getPageTitle(int position) {

            return super.getPageTitle(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            container.removeView((View) object);

        }
    }

0 个答案:

没有答案