由于需要用到“删除图片”的功能,需要写这样一个小demo:
对之前博文的修改
发现imageView监听点击事件 效果实在不敢恭维,因此换个方式:设置Touch的监听函数, 下面的Demo没有改过来哈,自行更改哈~~~
注意当你点击的时候会产生两个Touch,一个Down,一个Up,只要处理一个就可以了~~~
imageView2.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (event.getAction() == MotionEvent.ACTION_DOWN) { Log.d("dsd", "被点了"); Log.d("dsd", "data的大小: " + data.size() + ", position: " + position); data.remove(position); simpleAdapter.notifyDataSetChanged(); //gridView.setAdapter(simpleAdapter); return true; } return false; } });
效果图:点击右上方的“X”即可将图片从GridView中删除
其实每个Item的布局文件时个FrameLayout, 里面有两个ImageView,一个在底一个在上
/gridviewtest/res/layout/gridview_item.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageview1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" android:background="#F12A3A" /> <ImageView android:id="@+id/imageview2" android:layout_width="40dip" android:layout_height="40dip" android:layout_gravity="right|top" /> </FrameLayout>
主activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <GridView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/my_gridview" android:listSelector="@android:color/black" android:verticalSpacing="10dp" android:horizontalSpacing="10dip" android:numColumns="3" android:stretchMode="columnWidth" /> </RelativeLayout>
MainActivity.java
package com.example.gridviewtest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.database.DataSetObserver; import android.graphics.Bitmap; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.ListAdapter; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { private List<Map<String, Object>> data = null; private GridViewAdapter simpleAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView gridView = (GridView) findViewById(R.id.my_gridview); data = new ArrayList<Map<String,Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("imageview1", R.drawable.tt); map.put("imageview2", R.drawable.cha); data.add(map); data.add(map); data.add(map); data.add(map); data.add(map); data.add(map); data.add(map); simpleAdapter = new GridViewAdapter(getApplicationContext() , data , R.layout.gridview_item , new String[]{"imageview1","imageview2"} , new int[]{R.id.imageview1,R.id.imageview2}); gridView.setAdapter(simpleAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void addItem(Bitmap bitmap) { Map<String, Object> item = new HashMap<String, Object>(); item.put("imageview1", bitmap);//添加图像 item.put("imageview2", R.drawable.cha); data.add(item); simpleAdapter.notifyDataSetChanged(); } private class GridViewAdapter extends SimpleAdapter { private Context context = null; private int pos; public GridViewAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); this.context = context; // TODO Auto-generated constructor stub } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = super.getView(position, convertView, parent); ImageView imageView = (ImageView) view.findViewById(R.id.imageview2); imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(context, "我被点击了", Toast.LENGTH_SHORT).show(); data.remove(position); simpleAdapter.notifyDataSetChanged(); } }); return view; } } }
demo源码:http://download.csdn.net/detail/u011133213/5914887