<uses-permission android:name="android.permission.INTERNET">uses-permission>
3在activity中
package com.zhh.android;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private GridView gdDelete;
GridViewAdapter gridViewAdapter;
List list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
myOnclick();
}
private void initView() {
gdDelete = (GridView)findViewById(R.id.gdDelete);
list = getData();
Log.e("111","list>>>"+list.size());
gridViewAdapter = new GridViewAdapter(list,MainActivity.this);
gdDelete.setAdapter(gridViewAdapter);
}
private void myOnclick() {
gridViewAdapter.delete(new GridViewAdapter.Det() {
@Override
public void del(int position) {
// 删除list中的数据,重新绑定数据
list.remove(position);
gridViewAdapter.notifyDataSetChanged();
}
});
}
/**
*造的数据源
* @return
*/
private ListgetData(){
String url1="http://www.taopic.com/uploads/allimg/140320/235013-14032020515270.jpg";
String url2="http://img.taopic.com/uploads/allimg/110729/1830-110H916410651.jpg";
String url3="http://pic12.photophoto.cn/20090925/0018032186087872_b.jpg";
String url4="http://pic33.photophoto.cn/20141130/0036036820486922_b.jpg";
String url5="http://pic28.photophoto.cn/20130806/0036036825706706_b.jpg";
String url6="http://img02.tooopen.com/images/20160614/tooopen_sy_165053819689.jpg";
String url7="http://image.tianjimedia.com/uploadImages/2014/348/58/69HYFVT5E6TE.jpg";
String url8="http://image.tianjimedia.com/uploadImages/2014/064/5K0GO577YBSU.jpg";
Listlist = new ArrayList<>();
list.add(url1);
list.add(url2);
list.add(url3);
list.add(url4);
list.add(url5);
list.add(url6);
list.add(url7);
list.add(url8);
return list;
}
}
4activity对应的布局activity_main
<LinearLayout 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:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<GridView
android:id="@+id/gdDelete"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
>GridView>
LinearLayout>
5GridView对应的适配器GridViewAdapter
package com.zhh.android;
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.Toast;
import com.bumptech.glide.Glide;
import java.util.List;
/**
* Created by 16838 on 2018/5/17.
*/
public class GridViewAdapter extends BaseAdapter {
private Det det;
// 把按钮回调出去
public void delete(Det det){
this.det=det;
}
public interface Det{
public void del(int position);
}
List imagesList;
Context context;
public GridViewAdapter(List imagesList, Context context){
this.imagesList=imagesList;
this.context=context;
}
@Override
public int getCount() {
return imagesList.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHoder viewHoder;
if(convertView==null) {
viewHoder=new ViewHoder();
convertView= LayoutInflater.from(context).inflate(R.layout.item_gridview_layout,null);
viewHoder.ivImage= (ImageView) convertView.findViewById(R.id.ivImage);
viewHoder.ivDelete=(ImageView) convertView.findViewById(R.id.ivDelete);
convertView.setTag(viewHoder);
}else{
viewHoder= (ViewHoder) convertView.getTag();
}
Glide.with(context).load(imagesList.get(position)).into(viewHoder.ivImage);
viewHoder.ivDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(det!=null) {
det.del(position);
}
}
});
return convertView;
}
private class ViewHoder{
ImageView ivImage;
ImageView ivDelete;
}
@Override
public Object getItem(int position) {
return imagesList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
6适配器对应的布局item_gridview_layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="140dp"
android:layout_height="140dp"
android:orientation="vertical"
>
<ImageView
android:id="@+id/ivImage"
android:layout_width="140dp"
android:layout_height="140dp"
android:scaleType="fitXY"
android:background="@mipmap/ic_launcher"
/>
<ImageView
android:id="@+id/ivDelete"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:background="@mipmap/delete"
android:layout_alignParentRight="true"
/>
RelativeLayout>
源码下载:TestImgDelete—-app
https://download.csdn.net/download/zhaihaohao1/10427899