1.首先设计man.xml这里使用HorizontalScrollView结合GridView实现Gallery的左右滚动效果,并且解决了Gallery从中间开始的问题,GridView可以从最左边开始显示。代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <!-- Gallery view 图片效果图 -->   <HorizontalScrollView    android:id="@+id/galleryScroll"android:layout_width="fill_parent"android:layout_height="90dip"android:scrollbars="none"android:focusable="false"android:layout_alignParentBottom="true"android:background="@drawable/gallerybackground"><FrameLayout android:layout_width="fill_parent"android:layout_height="wrap_content"android:focusable="false"><!--注意这里 宽度一定要设置不然不能达到效果  --><LinearLayout android:layout_width="770dp"android:layout_height="wrap_content" android:orientation="horizontal"android:focusable="false"><GridView android:id="@+id/gallery" android:layout_width="fill_parent"android:gravity="center" android:layout_height="wrap_content"android:horizontalSpacing="1.0dip" android:verticalSpacing="1.0dip"android:stretchMode="spacingWidthUniform" android:numColumns="auto_fit"android:columnWidth="70dip"android:focusable="false"></GridView></LinearLayout></FrameLayout></HorizontalScrollView></LinearLayout>

2. 图片列表适配的实现,如下:

package com.src;import java.util.ArrayList;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class ImageAdapter extends BaseAdapter{private static String[] effectTitle = {"原图", "柔光", "LOMO", "复古", "古典", "黑白", "淡雅", "哥特风", "暖化", "水印", "印象"};private static int[] effectDrawable = {R.drawable.lomoeffect_01, R.drawable.lomoeffect_02, R.drawable.lomoeffect_03, R.drawable.lomoeffect_04, R.drawable.lomoeffect_05, R.drawable.lomoeffect_06, R.drawable.lomoeffect_07, R.drawable.lomoeffect_08, R.drawable.lomoeffect_09, R.drawable.lomoeffect_10, R.drawable.lomoeffect_11}; private ArrayList<GalleryInfo> list;private LayoutInflater inflater;public ImageAdapter(Context context) {        super();        list = new ArrayList<GalleryInfo>();        inflater = LayoutInflater.from(context);       // 初始化数据        for (int i=0; i<effectDrawable.length; i++) {        GalleryInfo info = new GalleryInfo();        info.title = effectTitle[i];        info.drawable = effectDrawable[i];        if (i ==0 ) {        //默认第一项问选中        info.isSelect = true;        } else {        info.isSelect = false;        }        list.add(info);        }    }        public int getCount() {        return effectTitle.length;    }        public Object getItem(int position) {        return position;    }        public long getItemId(int position) {        return position;    }        public View getView(int position, View convertView, ViewGroup parent) {        ViewHolder holder;    if (convertView == null) {    convertView = inflater.inflate(R.layout.gallery_item, null);    holder = new ViewHolder();    holder.effectTitle = (TextView)convertView.findViewById(R.id.effectTitle);    holder.effectDrawable = (ImageView)convertView.findViewById(R.id.effectDrawable);    convertView.setTag(holder);    } else {    holder = (ViewHolder)convertView.getTag();    }        holder.effectTitle.setText(list.get(position).title);    holder.effectDrawable.setImageResource(list.get(position).drawable);                if (list.get(position).isSelect) {//被选中的选项加选中的背景框        holder.effectDrawable.setBackgroundResource(R.drawable.gallery_select);        }else {//未被选中的选项设置背景透明        holder.effectDrawable.setBackgroundDrawable(null);        }        //        notifyDataSetChanged();        return convertView;    }        public void changeStatus(int select) {    for (int i=0; i<list.size(); i++) {    list.get(i).isSelect = false;    }        list.get(select).isSelect = true;    }        private class ViewHolder {    TextView effectTitle;    ImageView effectDrawable;    }        class GalleryInfo {    public String title;    public int drawable;    public boolean isSelect;    }}

3. 测试实例效果,代码如下:

package com.src;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.GridView;public class GrideToGalleryActivity extends Activity {private GridView gallery;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);             final ImageAdapter adapter = new ImageAdapter(this);        gallery = (GridView)findViewById(R.id.gallery);        gallery.setAdapter(adapter);        gallery.setNumColumns(11);               gallery.setOnItemClickListener(new OnItemClickListener() {public void onItemClick(AdapterView<?> arg0, View arg1, int select,long arg3) {adapter.changeStatus(select);//设置选中项adapter.notifyDataSetChanged();//更新列表}});                gallery.setOnTouchListener(new OnTouchListener() {public boolean onTouch(View v, MotionEvent event) {v.clearFocus();return false;}});    }}


更多相关文章

  1. Android动画效果translate、scale、alpha、rotate说明
  2. Android自定义EditText,实现分割输入内容效果
  3. Android 选项卡实现
  4. android 手势屏幕平移图片转换效果(也可以平移文本)
  5. Android关于Activity切换效果的实现加强
  6. android学习之ToggleButton实现开关效果
  7. Android动画效果translate、scale、alpha、rotate详解

随机推荐

  1. 使用achartengine开发曲线图相关的Androi
  2. Android(安卓)修改init.rc
  3. Android数据库学习123
  4. 如何打开USB调试模式(Android(安卓)所有
  5. Eclipse便于开发Android的快捷键大全
  6. 《Android内核剖析》读书笔记 第6章 应用
  7. 关于Android回调的理解
  8. Android(安卓)TextView文字太多显示的最
  9. 当我们讨论流畅度的时候,我们究竟在说什么
  10. iPhone 6 16G不够用?给iPhone 6扩容的技