GridView中ImageView的动态变换
使用YY影音的时候,发现点击GridView的某一个Item,Item里面的图标会在按下的时候发生变换,变成另外一个图片。
自己写了一个类似的demo,具体步骤如下:
1、创建一个包含Grid的Acitity
2、创建item.xml 里面包含一个imageview和一个textview
3、自定义一个adapter,从baseadapter继承
4、在getView中为每个imageView添加setOnTouchListener
代码入下:
MainActivity.java
package com.mygriddemo;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private GridView gv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gv = (GridView) findViewById(R.id.mygrid);
AddAdapter addadapter = new AddAdapter(this);
gv.setAdapter(addadapter);
}
public class AddAdapter extends BaseAdapter {
private final LayoutInflater mInflater;
private final ArrayList<ListItem> mItems = new ArrayList<ListItem>();
// public static final int ITEM_SHORTCUT = 0;
// public static final int ITEM_APPWIDGET = 1;
// public static final int ITEM_LIVE_FOLDER = 2;
// public static final int ITEM_WALLPAPER = 3;
/**
* Specific item in our list.
*/
public class ListItem {
public final CharSequence text;
public final Drawable image;
//public final int actionTag;
public final Drawable touchimage;
//, int actionTag
public ListItem(Resources res, int textResourceId, int imageResourceId,int touchImageResourceId) {
text = res.getString(textResourceId);
if (imageResourceId != -1) {
image = res.getDrawable(imageResourceId);
} else {
image = null;
}
if (touchImageResourceId != -1) {
touchimage = res.getDrawable(touchImageResourceId);
} else {
touchimage = null;
}
}
}
public AddAdapter(MainActivity launcher) {
super();
mInflater = (LayoutInflater) launcher.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Create default actions
Resources res = launcher.getResources();
mItems.add(new ListItem(res, R.string.item1,
R.drawable.item1, R.drawable.item1back));
mItems.add(new ListItem(res, R.string.item2,
R.drawable.item2, R.drawable.item2back));
mItems.add(new ListItem(res, R.string.item3,
R.drawable.item3, R.drawable.item3back));
mItems.add(new ListItem(res, R.string.item4,
R.drawable.item4, R.drawable.item4back));
}
public View getView(int position, View convertView, ViewGroup parent) {
final ListItem item = (ListItem) getItem(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.grid_item, parent, false);
}
final TextView textView = (TextView) convertView;//.findViewById(R.id.mygrid);
textView.setTag(item);
textView.setText(item.text);
textView.setCompoundDrawablesWithIntrinsicBounds(null, item.image, null, null);
textView.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction() == MotionEvent.ACTION_DOWN){
Log.d("WeatherWidget", MotionEvent.ACTION_DOWN+"");
textView.setCompoundDrawablesWithIntrinsicBounds(null, item.touchimage, null, null);
}
else if(arg1.getAction() == MotionEvent.ACTION_UP) {
Log.d("WeatherWidget", arg1.getAction()+"");
textView.setCompoundDrawablesWithIntrinsicBounds(null, item.image, null, null);
Intent i = new Intent(MainActivity.this,Page1Activity.class);
startActivity(i);
}
return true;
}
});
return convertView;
}
public int getCount() {
return mItems.size();
}
public Object getItem(int position) {
return mItems.get(position);
}
public long getItemId(int position) {
return position;
}
}
}
main.xml
代码
<?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"
>
<GridView android:id="@+id/mygrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:verticalSpacing="10dip"
android:horizontalSpacing="10dip"
android:stretchMode="columnWidth"
android:gravity="center"
>
</GridView>
</LinearLayout>
griditem.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_centerHorizontal="true"
android:drawablePadding="14dip"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:gravity="center_horizontal"
/>