android的viewpager控件的使用

来源:互联网 发布:怎么获取三级域名 编辑:程序博客网 时间:2024/05/20 21:23

布局文件中布局viewpager:

 

</pre> <RelativeLayout        android:layout_width="fill_parent"        android:layout_height="0dp"        android:layout_weight="5"        android:orientation="vertical" >        <android.support.v4.view.ViewPager            android:id="@+id/adv_pager"            android:layout_width="fill_parent"            android:layout_height="fill_parent" >        </android.support.v4.view.ViewPager>        <LinearLayout            android:id="@+id/viewGroup"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_below="@id/adv_pager"            android:layout_marginTop="-25px"            android:gravity="center"            android:orientation="horizontal" >        </LinearLayout>    </RelativeLayout><p></p><p><span style="font-size:24px;color:#ff0000;">在代码中使用viewpager:</span></p><span style="font-size:14px;">advPager = (ViewPager) view.findViewById(R.id.adv_pager);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">List<View> advPics = new ArrayList<View>();</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">ImageView img1 = new ImageView(getActivity());</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">img1.setBackgroundResource(R.drawable.page1);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">advPics.add(img1);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">ImageView img2 = new ImageView(getActivity());</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">img2.setBackgroundResource(R.drawable.page2);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">advPics.add(img2);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">ImageView img3 = new ImageView(getActivity());</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">img3.setBackgroundResource(R.drawable.page3);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">advPics.add(img3);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">ImageView img4 = new ImageView(getActivity());</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">img4.setBackgroundResource(R.drawable.page4);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">advPics.add(img4);</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">ViewGroup group = (ViewGroup) view.findViewById(R.id.viewGroup);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">imageViews = new ImageView[advPics.size()];</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">for (int i = 0; i < advPics.size(); i++) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">imageView = new ImageView(getActivity());</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">imageView.setLayoutParams(new LayoutParams(20, 20));</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">imageView.setPadding(20, 0, 20, 0);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">imageViews[i] = imageView;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">if (i == 0) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">imageViews[i]</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">.setBackgroundResource(R.drawable.banner_dian_focus);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">} else {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">imageViews[i]</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">.setBackgroundResource(R.drawable.banner_dian_blur);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">group.addView(imageViews[i]);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">advPager.setAdapter(new AdvAdapter(advPics));</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">advPager.setOnPageChangeListener(new GuidePageChangeListener());</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">advPager.setOnTouchListener(new View.OnTouchListener() {</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public boolean onTouch(View v, MotionEvent event) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">switch (event.getAction()) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">case MotionEvent.ACTION_DOWN:</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">case MotionEvent.ACTION_MOVE:</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">isContinue = false;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">break;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">case MotionEvent.ACTION_UP:</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">isContinue = true;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">break;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">default:</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">isContinue = true;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">break;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">return false;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">});</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">new Thread(new Runnable() {</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public void run() {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">while (true) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">if (isContinue) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">viewHandler.sendEmptyMessage(what.get());</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">whatOption();</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}).start();</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">return view;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">private void whatOption() {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">what.incrementAndGet();</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">if (what.get() > imageViews.length - 1) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">what.getAndAdd(-4);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">try {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">Thread.sleep(2000);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">} catch (InterruptedException e) {</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">private final Handler viewHandler = new Handler() {</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">@Override</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public void handleMessage(Message msg) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">advPager.setCurrentItem(msg.what);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">super.handleMessage(msg);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">};</span><span style="font-size:14px;">private final class AdvAdapter extends PagerAdapter {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">private List<View> views = null;</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public AdvAdapter(List<View> views) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">this.views = views;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">@Override</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public void destroyItem(View arg0, int arg1, Object arg2) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">((ViewPager) arg0).removeView(views.get(arg1));</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">@Override</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public void finishUpdate(View arg0) {</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">@Override</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public int getCount() {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">return views.size();</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">@Override</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public Object instantiateItem(View arg0, int arg1) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">((ViewPager) arg0).addView(views.get(arg1), 0);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">return views.get(arg1);</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">@Override</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public boolean isViewFromObject(View arg0, Object arg1) {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">return arg0 == arg1;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">@Override</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public void restoreState(Parcelable arg0, ClassLoader arg1) {</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">@Override</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public Parcelable saveState() {</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">return null;</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">@Override</span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">public void startUpdate(View arg0) {</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><span style="font-size:14px;"></span><span style="font-size:14px; white-space: pre;"></span><span style="font-size:14px;">}</span><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><p></p></blockquote><p><pre name="code" class="html">

0 0