android之横向滚动图

问了下做android的老湿们,原来横向滚动的玩意用viewPager,我一直以为和ios一样用scrollview,蛋疼了下。。。。

模式也是控件+适配器,直接上代码

MainActivity.java

 1 package com.example.testviewpager;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import android.app.Activity;
 6 import android.os.Bundle;
 7 import android.support.v4.view.ViewPager;
 8 import android.view.LayoutInflater;
 9 import android.view.Menu;
10 import android.view.View;
11 import android.widget.ImageView;
12 
13 public class MainActivity extends Activity {
14 
15     private List<ObjectEntity> dataSource;
16     private List<View>             itemViews;
17     
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         
23         initItems();
24         
25         ViewPager viewPager = (ViewPager)findViewById(R.id.mainViewPager);
26         MyAdapter viewPageAdapter = new MyAdapter(this,dataSource,itemViews);
27         
28         viewPager.setAdapter(viewPageAdapter);
29     }
30 
31     private void initItems() {
32         // TODO Auto-generated method stub
33 
34         dataSource = new ArrayList<ObjectEntity>();
35         for (int i = 0; i < 5; i++) {
36             
37             ObjectEntity objectEntity = new ObjectEntity();
38             
39             objectEntity.resId = R.drawable.guangzhou;
40             objectEntity.imgUrl = "http://b.hiphotos.baidu.com/album/w%3D2048/sign=dd21c55132fa828bd1239ae3c9274034/d31b0ef41bd5ad6ebcdb25be80cb39dbb6fd3c47.jpg";
41         
42             dataSource.add(objectEntity);
43         }
44         
45         itemViews = new ArrayList<View>();
46         for (int i = 0; i < dataSource.size(); i++) {
47             
48             LayoutInflater inflater = getLayoutInflater();
49             View view = inflater.inflate(R.layout.item, null);
50             
51             itemViews.add(view);
52         }
53     }
54 }

对象实体ObjectEntity.java

1 package com.example.testviewpager;
2 
3 import android.R.string;
4 
5 public class ObjectEntity {
6 
7     public int    resId;
8     public String imgUrl;
9 }

适配器Mydapter.java,命名不是很好,不过仅仅用来学习足够~

 1 package com.example.testviewpager;
 2 
 3 import java.util.List;
 4 
 5 import net.tsz.afinal.FinalBitmap;
 6 import android.content.Context;
 7 import android.support.v4.view.PagerAdapter;
 8 import android.view.View;
 9 import android.view.ViewGroup;
10 import android.widget.ImageView;
11 
12 public class MyAdapter extends PagerAdapter {
13 
14     private Context                _context;
15     private List<ObjectEntity> _dataSource;
16     private List<View>             _views;
17     
18     private FinalBitmap         _finalMab;
19     
20     public MyAdapter(Context context,List<ObjectEntity> ds,List<View> views) {
21 
22         _context = context;
23         _dataSource = ds;
24         _views = views;
25     }
26 
27     @Override
28     public int getCount() {
29         // TODO Auto-generated method stub
30         return _views.size();
31     }
32 
33     @Override
34     public void destroyItem(ViewGroup container, int position, Object object) {
35         // TODO Auto-generated method stub
36         
37         container.removeView(_views.get(position));
38     }
39 
40     @Override
41     public Object instantiateItem(ViewGroup container, int position) {
42         // TODO Auto-generated method stub
43         
44         View itemView = _views.get(position);
45         ObjectEntity objectEntity = _dataSource.get(position);
46         
47         ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView1);
48         
49         _finalMab = FinalBitmap.create(_context);//初始化FinalBitmap模块
50         _finalMab.configLoadingImage(objectEntity.resId);
51         _finalMab.display(imageView,objectEntity.imgUrl);
52         
53         container.addView(itemView);
54         
55         return itemView;
56     }
57 
58     @Override
59     public boolean isViewFromObject(View arg0, Object arg1) {
60         // TODO Auto-generated method stub
61         return arg0 == arg1;
62     }
63 
64 }

下面是布局文件activity_main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10     
11     <android.support.v4.view.ViewPager
12         android:id="@+id/mainViewPager"
13         android:layout_width="fill_parent"
14         android:layout_height="fill_parent" >
15 
16     </android.support.v4.view.ViewPager>
17     
18 </RelativeLayout>

作为子项加载到ViewPager里面的item.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="fill_parent"
 3     android:layout_height="fill_parent" >
 4 
 5     <LinearLayout
 6         android:layout_width="fill_parent"
 7         android:layout_height="fill_parent" >
 8 
 9         <ImageView
10             android:id="@+id/imageView1"
11             android:layout_width="fill_parent"
12             android:layout_height="fill_parent"
13             android:scaleType="fitXY" />
14 
15     </LinearLayout>
16 
17 </LinearLayout>

 

你可能感兴趣的:(android)