首页 > 代码库 > Android:ViewPager制作幻灯片
Android: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制作幻灯片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。