Android实现GridView中ImageView动态变换的方法

2019-12-10 19:59:53刘景俊
  • 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);