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;
			}
		});
    }
}
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()