概述
最近在项目中用到图片轮播,试了Gallery,ViewFlipper,ViewPager,感觉Gallery最符合需求,但是Gallery的系统边框很难看,项目中要求用自己的背景图片。
接下来用viewpager来做幻灯片效果。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="100dp" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" > </android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/pointgroup" android:layout_width="match_parent" android:layout_height="10dp" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_marginBottom="5dp" android:gravity="center" > </LinearLayout> </RelativeLayout>
程序:
public class MainActivity extends Activity { //ViewPager private ViewPager viewpager; //幻灯片图片资源 private int [] imgArray={R.drawable.view1,R.drawable.view2,R.drawable.view3}; //图片数组 private ArrayList<View> viewlist; private ImageView imageView; //点点 private ViewGroup pointgroup; private ImageView[] pointarr; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_viewpage); //初始化 viewpager =(ViewPager) findViewById(R.id.viewpager); pointgroup =(ViewGroup) findViewById(R.id.pointgroup); viewlist = new ArrayList<View>(); //将图片装载到数组 for(int i=0;i<imgArray.length;i++){ imageView =new ImageView(this); //设置图片 imageView.setBackgroundResource(imgArray[i]); //设置图片id imageView.setId(imgArray[i]); viewlist.add(imageView); } /* * viewpager加载适配器 * MyPageAdapter为自定义适配器 * */ //传递上下文 Context context = MainActivity.this; MyPageAdapter pageadapter2 =new MyPageAdapter(viewlist,context); viewpager.setAdapter(pageadapter2); //点数组 pointarr=new ImageView[imgArray.length]; //把点加入布局,和设置点状态 for(int i=0;i<imgArray.length;i++){ ImageView point=new ImageView(this); //设置点大小 point.setLayoutParams(new LayoutParams(10,10)); pointarr[i]=point; //状态处理 if(i==0){ pointarr[i].setBackgroundResource(R.drawable.viewpage_point_focused); }else{ pointarr[i].setBackgroundResource(R.drawable.viewpage_point_unfocused); } //加入到容器 pointgroup.addView(pointarr[i]); } //viewpager设置监听器 pointChangeListener pointListener=new pointChangeListener(); viewpager.setOnPageChangeListener(pointListener); //点击图片 } /* * viewpage监听器 */ public class pointChangeListener implements OnPageChangeListener{ @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override //比如切换到第二张图片,相应索引的点为选择状态,其他为未选中状态 public void onPageSelected(int arg0) { // TODO Auto-generated method stub for(int i=0;i<pointarr.length;i++){ pointarr[arg0].setBackgroundResource(R.drawable.viewpage_point_focused); if(arg0 !=i){ pointarr[i].setBackgroundResource(R.drawable.viewpage_point_unfocused); } } } } }
自定义适配器:
public class MyPageAdapter extends PagerAdapter { private List<View> viewlist; private Context context; public MyPageAdapter(List<View> viewlist,Context context){ this.viewlist =viewlist; this.context=context; } @Override public int getCount() { // TODO Auto-generated method stub return viewlist.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } //以下也是必要方法,不过要自己添加 //实例化 @Override public Object instantiateItem(View container, int position) { // TODO Auto-generated method stub ((ViewPager)container).addView(viewlist.get(position)); View view =viewlist.get(position); view.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub int i =v.getId(); String t="你点击了图片的图片id为:"+i; Toast.makeText(context, t, Toast.LENGTH_LONG).show(); /*跳转页面 *Intent intent =new Intent(context,NavigationActivity.class); *context.startActivity(intent); */ } }); return viewlist.get(position); } //销毁 @Override public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(viewlist.get(position)); } }
最后
以上就是尊敬鞋垫为你收集整理的Android:ViewPager制作幻灯片的全部内容,希望文章能够帮你解决Android:ViewPager制作幻灯片所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复