Android页面左右滑动浏览图片(Gallery示例)

来源:互联网 发布:域名备案和网站备案 编辑:程序博客网 时间:2024/04/27 16:56

自从滑动功能出来后人们都习惯手滑来滑去,现在我做了一个简单例子,实现页面之间的左右滑动。实现的方法有很多,我这里用的是Gallery加载适配器的方法。

有图有真像:


第一界面,内容差不多,只是前面的编号不一样。



 


滑动过程




例子结构


 

一个activity  

PageScrollActivity.java

复制代码
 1 package com.harlan.act; 2  3 import java.util.ArrayList; 4 import java.util.List; 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.widget.Gallery; 8 import com.harlan.act.adapter.PageAdapter; 9 import com.harlan.entity.Img;10 11 /**12  * 页面滑动13  * @author Harlan Song14  * @email mark_dev@163.com15  */16 public class PageScrollActivity extends Activity {17     private Gallery gallery;18 19     @Override20     public void onCreate(Bundle savedInstanceState) {21         super.onCreate(savedInstanceState);22         setContentView(R.layout.main);23         gallery = (Gallery) findViewById(R.id.gallery1);24         List<Img> list=new ArrayList<Img>();25         Img img;26         for (int i = 1; i < 11; i++) {27             img=new Img();28             img.setDes(i+".这种房子非常有创意,欢迎访问: http://www.mythroad.net 更多-精彩移动开发知识文章-分享!");29             //图片就不加载了30             list.add(img);31         }32         PageAdapter pageAdapter=new PageAdapter(PageScrollActivity.this, list);33         gallery.setAdapter(pageAdapter);34         35     }36 37 }
复制代码

 

gallery配置器

PageAdapter.java

复制代码
 1 package com.harlan.act.adapter; 2  3 import java.util.List; 4 import android.content.Context; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 import android.widget.TextView;10 import com.harlan.act.R;11 import com.harlan.entity.Img;12 13 public class PageAdapter extends BaseAdapter {14     private List<Img> list;15     LayoutInflater inflater;16     public PageAdapter(Context context,List<Img> list) {17         this.list=list;18         this.inflater=LayoutInflater.from(context);19     }20 21     @Override22     public int getCount() {23         return list.size();24     }25 26     @Override27     public Object getItem(int position) {28         return list.get(position);29     }30 31     @Override32     public long getItemId(int position) {33         return position;34     }35 36     @Override37     public View getView(int position, View convertView, ViewGroup parent) {38         CacheView cacheView;39         if(convertView==null){40             convertView=inflater.inflate(R.layout.item_page, null);41             cacheView=new CacheView();42             cacheView.tv_des=(TextView) convertView.findViewById(R.id.tv_des);43             //cacheView.imgv_img=(ImageView) convertView.findViewById(R.id.imageView1);44             convertView.setTag(cacheView);45         }else{46             cacheView=(CacheView) convertView.getTag();47         }48         cacheView.tv_des.setText(list.get(position).getDes());49         50         return convertView;51     }52     53     private static class CacheView{54         TextView tv_des;55         //ImageView imgv_img;56     }57 }
复制代码

 

Img.java

复制代码
 1 package com.harlan.entity; 2  3 /** 4  * 图片实体类 5  * @author Harlan Song 6  * @email mark_dev@163.com 7  */ 8 public class Img { 9     private String des;//描述10     private String imgurl;//图片路径11     public String getDes() {12         return des;13     }14     public void setDes(String des) {15         this.des = des;16     }17     public String getImgurl() {18         return imgurl;19     }20     public void setImgurl(String imgurl) {21         this.imgurl = imgurl;22     }23     24 }
复制代码

 main.xml

复制代码
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" 5     android:orientation="vertical" > 6  7     <Gallery 8         android:id="@+id/gallery1" 9         android:layout_width="fill_parent"10         android:layout_height="fill_parent"11         android:spacing="0dip" />12 13 </LinearLayout>
复制代码

item_page.xml

复制代码
 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" 5     android:orientation="vertical"  6     android:padding="5dip"> 7  8     <ImageView 9         android:id="@+id/imageView1"10         android:layout_width="fill_parent"11         android:layout_height="wrap_content"12         android:src="@drawable/img_page" 13        />14 15     <TextView16         android:id="@+id/tv_des"17         android:layout_width="wrap_content"18         android:layout_height="wrap_content"19         android:text="TextView" 20          />21 22 </LinearLayout>
复制代码

 


1.Gallery每次滑动翻一页效果

2. Gallery与ScrollView滑动冲突问题的处理

3.【完美解决冲突】Gallery与ScrollView每次滑动翻一页

4.Gallery与ScrollView的滑动效果优化

5.我的内核程序员之路(一):《深入理解Linux内核》带我入门



原创粉丝点击