package com.example.image;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
/**
- 音效视图的适配器,将音效视图填充到GridView中
-
@author lewa
*
*/
public class MyAdapter extends BaseAdapter {
private List list;
private Context context;public MyAdapter(List list,Context context){
this.list = list;
this.context = context;
}@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ItemView itemview;
if(convertView == null){
itemview = new ItemView(context);}else{ itemview = (ItemView) convertView; } itemview.imageView.setImageResource(list.get(position).getImageId()); Log.i("ymh","Position:"+position+"----List.size() = "+list.size()); itemview.textView.setText(list.get(position).getLable()); Log.i("ymh",itemview.textView.getText().toString()); itemview.setLayoutParams(new GridView.LayoutParams(85,85)); itemview.setPadding(8, 8, 8, 8); return itemview;
}
}
package com.example.image;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* 音效图片和音效名称的组合view
* @author lewa
*
*/
public class ItemView extends RelativeLayout {
ImageView imageView;
TextView textView;
private int state = 0;
public ItemView(Context context) {
super(context);
init(context);
}
public ItemView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context){
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.item, null);
imageView = (ImageView) view.findViewById(R.id.item_image);
textView = (TextView) view.findViewById(R.id.item_text);
}
public void OnClickListener(View.OnClickListener listener){
if(state==0){
imageView.setBackgroundColor(android.graphics.Color.GREEN);
state = 1;
}else{
imageView.setBackgroundColor(android.graphics.Color.GRAY);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/item_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher"
android:padding="5dp"/>
<TextView
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/item_image"
android:layout_alignRight="@id/item_image"
android:layout_below="@+id/item_image"
android:gravity="center"
android:textColor="#00ffaa"
android:text="textview" />