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
- Android ViewPager控件的使用
- android的viewpager控件的使用
- android常用的控件ViewPager的使用
- Android ViewPager控件的使用(基于ViewPager的横向相册)
- Android ViewPager控件的使用(基于ViewPager的横向相册)!!!
- Android ViewPager控件的使用(基于ViewPager的横向相册)!!!
- Android ViewPager控件的使用(基于ViewPager的横向相册)!!!
- Android常用控件之ViewPager的使用
- 安卓中ViewPager控件的使用
- Android学习笔记32:滑屏控件ViewPager的使用
- Android高级控件系列六之ViewPager的使用
- android viewpager的使用
- Android-ViewPager的使用
- android ViewPager的使用
- Android ViewPager的使用
- Android基础:翻页控件Viewpager的使用方法,viewpager+fragment混合使用
- 【Android 开发】:UI控件之 ViewPager 多页面滑动效果控件的的使用
- 【Android 开发】:UI控件之 ViewPager 多页面滑动效果控件的的使用(二)
- It is not safe to rely on the system's timezone settings
- Android布局文件layout.xml的一些属性值
- 值传递和引用传递的区别
- YUI table 拓展使用
- linux shell脚本编程if语句内判断参数详解
- android的viewpager控件的使用
- 图解Git
- VC编程实现位图图像二值化、反相
- Android Studio 快捷键
- jquery文字折叠展开
- git恢复节点
- UI特效
- 关于ThreadLocal的一道面试题(酷我公司)
- poj 3297 Fliptile 深搜