Android ViewPager加载图片效果
目前项目中需要用到ViewPager加载图片,现在在此记录一下。
首先先看布局文件:activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.fly.viewpagerbanner.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_above="@+id/ll" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <LinearLayout android:id="@+id/ll" android:layout_marginTop="10.0dip" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="24.0dip" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/> </RelativeLayout>
MainActivity
packagecom.example.fly.viewpagerbanner; importandroid.support.v4.view.ViewPager; importandroid.support.v7.app.AppCompatActivity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.ImageView; importandroid.widget.LinearLayout; importandroid.widget.Toast; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; /** *类名: *类描述:ViewPager展示图片 *创建人:fly *创建日期:2017/1/16. *版本:V1.0 */ publicclassMainActivityextendsAppCompatActivityimplementsViewPager.OnPageChangeListener,View.OnClickListener{ privateViewPagervp; privateViewPagerAdaptervpAdapter; privateList<Map<String,Object>>data; privateintlastValue=-1;//是不是最后一张图片 privateImageView[]dots;//底部小点图片 privateintcurrentIndex;//记录当前选中位置 @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Intentintent=newIntent(); //intent.putStringArrayListExtra("GalleryPlusActivity",ArrayList<String>);//怎么传递集合 ArrayList<String>imageLists=getIntent().getStringArrayListExtra("GalleryPlusActivity");//动态获取展示图片的集合 data=getData(imageLists); vp=(ViewPager)findViewById(R.id.viewpager); vpAdapter=newViewPagerAdapter(data,this); vp.setAdapter(vpAdapter); vp.addOnPageChangeListener(this); initDots();//初始化底部小点 } //这个方法长度是动态的,可以改成你从服务器获取的图片,这样数量就不确定啦 publicList<Map<String,Object>>getData(ArrayList<String>imageLists){ List<Map<String,Object>>mData=newArrayList<>(); //if(imageLists!=null&&imageLists.size()>0){ //for(inti=0;i<imageLists.size();i++){ //Map<String,Object>map=newHashMap<>(); //map.put("url",imageLists.get(i)); //map.put("view",newImageView(this)); //mData.add(map); //} //} Map<String,Object>map=newHashMap<>(); map.put("url","http://img2.duitang.com/uploads/item/201207/19/20120719132725_UkzCN.jpeg"); map.put("view",newImageView(this)); mData.add(map); Map<String,Object>map1=newHashMap<>(); map1.put("url","http://img4.duitang.com/uploads/item/201404/24/20140424195028_vtvZu.jpeg"); map1.put("view",newImageView(this)); mData.add(map1); Map<String,Object>map2=newHashMap<>(); map2.put("url","http://download.pchome.net/wallpaper/pic-5041-8-240x320.jpg"); map2.put("view",newImageView(this)); mData.add(map2); Map<String,Object>map3=newHashMap<>(); map3.put("url","http://www.mangowed.com/uploads/allimg/130425/572-130425105311304.jpg"); map3.put("view",newImageView(this)); mData.add(map3); returnmData; } privatevoidinitDots(){ LinearLayout.LayoutParamsmLayoutParams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); mLayoutParams.setMargins(3,0,3,0); LinearLayoutll=(LinearLayout)findViewById(R.id.ll); dots=newImageView[data.size()]; //循环取得小点图片 for(inti=0;i<data.size();i++){ dots[i]=newImageView(this); dots[i].setLayoutParams(mLayoutParams); dots[i].setBackgroundResource(R.drawable.dot); dots[i].setEnabled(true);//都设为灰色 dots[i].setOnClickListener(this); dots[i].setTag(i);//设置位置tag,方便取出与当前位置对应 ll.addView(dots[i]); } currentIndex=0; dots[currentIndex].setEnabled(false);//设置为白色,即选中状态 } privatevoidsetCurView(intposition){/**设置当前的引导页*/ if(position<0||position>=data.size()){return;} vp.setCurrentItem(position); } privatevoidsetCurDot(intposition){/**设置当前引导小点的选中*/ if(position<0||position>data.size()-1||currentIndex==position){return;} dots[position].setEnabled(false); dots[currentIndex].setEnabled(true); currentIndex=position; } @Override//当前页面被滑动时调用 publicvoidonPageScrolled(intposition,floatpositionOffset,intpositionOffsetPixels){ lastValue=position; } @Override publicvoidonPageSelected(intposition){//当新的页面被选中时调用 setCurDot(position);//设置底部小点选中状态 } @Override publicvoidonPageScrollStateChanged(intstate){//当滑动状态改变时调用 if(state==0){ if(lastValue==data.size()-1){ Toast.makeText(this,"已经是最后一张了",Toast.LENGTH_SHORT).show(); } } } @Override publicvoidonClick(Viewv){ intposition=(Integer)v.getTag(); setCurView(position); setCurDot(position); } }
ViewPagerAdapter
packagecom.example.fly.viewpagerbanner; importandroid.content.Context; importandroid.support.v4.view.PagerAdapter; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.ImageView; importcom.bumptech.glide.Glide; importcom.bumptech.glide.load.engine.DiskCacheStrategy; importjava.util.List; importjava.util.Map; publicclassViewPagerAdapterextendsPagerAdapter{ Contextcontext; List<Map<String,Object>>viewLists; publicViewPagerAdapter(List<Map<String,Object>>lists,Contextcontext){ this.viewLists=lists; this.context=context; } @Override publicintgetCount(){//获得size returnviewLists.size(); } @Override publicbooleanisViewFromObject(Viewview,Objectobject){ returnview==object; } @Override publicvoiddestroyItem(ViewGroupview,intposition,Objectobject){//销毁Item ImageViewx=(ImageView)viewLists.get(position).get("view"); x.setScaleType(ImageView.ScaleType.FIT_CENTER); view.removeView(x); } @Override publicObjectinstantiateItem(ViewGroupview,intposition){//实例化Item ImageViewimageView=(ImageView)viewLists.get(position).get("view"); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); Glide.with(context) .load(viewLists.get(position).get("url").toString()) //.placeholder(R.mipmap.new_default) .error(R.mipmap.ic_launcher) .diskCacheStrategy(DiskCacheStrategy.RESULT) .into(imageView); view.addView(imageView,0); returnviewLists.get(position).get("view"); } }
是不是很简单。
最后附上demo:http://xiazai.jb51.net/201701/yuanma/ViewPager(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。