@Override public Object instantiateItem(ViewGroup container, final int position) { final Info info = mDatas.get(position); // 设置一大堆演示用的数据,麻里麻烦~~ View view = LayoutInflater.from(mContext).inflate(R.layout.viewpager_layout, null); ImageView ivPortrait = (ImageView) view.findViewById(R.id.iv); ImageView ivSex = (ImageView) view.findViewById(R.id.iv_sex); TextView tvName = (TextView) view.findViewById(R.id.user_name); TextView tvDistance = (TextView) view.findViewById(R.id.tv_distance); tvName.setText(info.getName()); tvDistance.setText(info.getDistance() + "km"); ivPortrait.setImageResource(info.getPortraitId()); if (info.getSex()) { ivSex.setImageResource(R.drawable.girl); } else { ivSex.setImageResource(R.drawable.boy); } ivPortrait.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "这是 " + info.getName() + " >.<", Toast.LENGTH_SHORT).show(); } }); container.addView(view); return view; }
@Override protected void initViewsAndEvents(View rootView) { mTitle.setText("附近的人"); mTitleRight.setVisibility(View.GONE); mIcon.setImageResource(R.drawable.icon_head); // toggleShowLoading(true,"loading"); for (int i = 0; i < mImgs.length; i++) { Info info = new Info(); info.setPortraitId(mImgs[i]); info.setAge(((int) Math.random() * 25 + 16) + "岁"); info.setName(mNames[(int) (Math.random() * mNames.length)]); info.setSex(i % 3 == 0 ? false : true); info.setDistance(Math.round((Math.random() * 10) * 100) / 100); mDatas.put(i, info); } /** 将Viewpager所在容器的事件分发交给ViewPager */ ryContainer.setOnTouchListener( new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return viewPager.dispatchTouchEvent(event); } }); ViewpagerAdapter mAdapter = new ViewpagerAdapter(); viewPager.setAdapter(mAdapter); // 设置缓存数为展示的数目 viewPager.setOffscreenPageLimit(mImgs.length); viewPager.setPageMargin(getResources().getDimensionPixelOffset(R.dimen.viewpager_margin)); // 设置切换动画 viewPager.setPageTransformer(true, new ZoomOutPageTransformer()); viewPager.addOnPageChangeListener(this); setViewPagerSpeed(250); new Handler() .postDelayed( new Runnable() { @Override public void run() { radarViewGroup.setDatas(mDatas); } }, 1500); radarViewGroup.setiRadarClickListener(this); }