zoukankan      html  css  js  c++  java
  • android pagerView

    在玩qq聊天软件的时候都会用到这个效果,以前一直不知道,现在解决了。,主要要新建一个lib文件,里面还要导入android-support-04.jar文件

    首先修改布局文件:

    View Code
     1 <?xml version="1.0" encoding="utf-8"?>
    2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:orientation="vertical"
    4 android:layout_width="fill_parent"
    5 android:layout_height="fill_parent">
    6 <android.support.v4.view.ViewPager
    7 android:layout_width="match_parent"
    8 android:layout_height="120dp"
    9 android:id="@+id/awesomepager"/>
    10 </LinearLayout>


    第二步:导入照片

    第三步:修改AwesomePagerActivity.java文件解释都在代码里

    View Code
      1 package cn.shaoyangjiang.com;
    2
    3 import java.util.ArrayList;
    4 import java.util.List;
    5 import com.geekyouup.paug.awesomepager.R;
    6 import android.app.Activity;
    7 import android.os.Bundle;
    8 import android.os.Parcelable;
    9 import android.support.v4.view.PagerAdapter;
    10 import android.support.v4.view.ViewPager;
    11 import android.view.View;
    12 import android.widget.ImageView;
    13
    14 public class AwesomePagerActivity extends Activity {
    15 //ViewPager实例化
    16 private ViewPager awesomePager;
    17 //实例化一个AwesomeAdapter
    18 private AwesomePagerAdapter awesomeAdapter;
    19 //实例化容器
    20 private List<View> mListViews;
    21 @Override
    22 public void onCreate(Bundle savedInstanceState) {
    23 super.onCreate(savedInstanceState);
    24 setContentView(R.layout.main);
    25 //创建awesomeAdapter
    26 awesomeAdapter = new AwesomePagerAdapter();
    27 //获得awesomePaper实例
    28 awesomePager = (ViewPager) findViewById(R.id.awesomepager);
    29 //绑定
    30 awesomePager.setAdapter(awesomeAdapter);
    31 mListViews = new ArrayList<View>();
    32 //为mListViews添加四个组件
    33 mListViews.add(addImageView(R.drawable.katong));
    34 mListViews.add(addImageView(R.drawable.shan));
    35 mListViews.add(addImageView(R.drawable.touming));
    36 mListViews.add(addImageView(R.drawable.meinv));
    37
    38 }
    39 // 定义添加ImageView的工具方法
    40 private View addImageView(int resId){
    41 ImageView imageView = new ImageView(this);
    42 imageView.setImageResource(resId);
    43 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    44 return imageView;
    45 }
    46
    47 private class AwesomePagerAdapter extends PagerAdapter{
    48 @Override
    49 public int getCount() {
    50 return mListViews.size();
    51 }
    52
    53 /**
    54 * Create the page for the given position. The adapter is responsible
    55 * for adding the view to the container given here, although it only
    56 * must ensure this is done by the time it returns from
    57 * {@link #finishUpdate()}.
    58 *
    59 * @param container The containing View in which the page will be shown.
    60 * @param position The page position to be instantiated.
    61 * @return Returns an Object representing the new page. This does not
    62 * need to be a View, but can be some other container of the page.
    63 */
    64 @Override
    65 public Object instantiateItem(View collection, int position) {
    66
    67
    68 ((ViewPager) collection).addView(mListViews.get(position),0);
    69
    70 return mListViews.get(position);
    71 }
    72
    73 /**
    74 * Remove a page for the given position. The adapter is responsible
    75 * for removing the view from its container, although it only must ensure
    76 * this is done by the time it returns from {@link #finishUpdate()}.
    77 *
    78 * @param container The containing View from which the page will be removed.
    79 * @param position The page position to be removed.
    80 * @param object The same object that was returned by
    81 * {@link #instantiateItem(View, int)}.
    82 */
    83 @Override
    84 public void destroyItem(View collection, int position, Object view) {
    85 ((ViewPager) collection).removeView(mListViews.get(position));
    86 }
    87
    88 @Override
    89 public boolean isViewFromObject(View view, Object object) {
    90 return view==(object);
    91 }
    92 /**
    93 * Called when the a change in the shown pages has been completed. At this
    94 * point you must ensure that all of the pages have actually been added or
    95 * removed from the container as appropriate.
    96 * @param container The containing View which is displaying this adapter's
    97 * page views.
    98 */
    99 @Override
    100 public void finishUpdate(View arg0) {}
    101
    102
    103 @Override
    104 public void restoreState(Parcelable arg0, ClassLoader arg1) {}
    105
    106 @Override
    107 public Parcelable saveState() {
    108 return null;
    109 }
    110
    111 @Override
    112 public void startUpdate(View arg0) {}
    113
    114 }
    115
    116 }

    效果图:

  • 相关阅读:
    服务器端渲染SSR的优缺点?
    Vue轻量级富文本编辑器-Vue-Quill-Editor
    浏览器跨域问题
    CSS中 !important 的使用
    如何使用vue-table-with-tree-grid的树形表格组件
    各类程序员学习路线图
    Vue中 el-table大数据量加载,不分页,节省内存的性能优化
    看完让你彻底理解 WebSocket 原理,附完整的实战代码(包含前端和后端)
    vue中class类名根据绑定的值三元表达式或字符串拼接动态添加类名
    close事件 vue_vue 监听浏览器关闭事件
  • 原文地址:https://www.cnblogs.com/shaoyangjiang/p/2373901.html
Copyright © 2011-2022 走看看