在多图片上传的时候我们通常会选择很多图片,然后我们可以长按其中的某一个图片,这时候所有的图片都会显示一个删除的角标,我们点击其中的某一个就可以删除不要的图片,效果图:
代码很容易看懂,直接贴上来:
package zm.imageview;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private boolean isShowDelete = false;
private GridView gv;
private GridViewAdapter mGridAdapter;
private List<AreaEntity> areaEneities = new ArrayList<AreaEntity>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv = (GridView) findViewById(R.id.gv);
for (int i = 0; i < 10; i++) {
AreaEntity areaEntity = new AreaEntity(i + "", "http://img.tuku.cn/file_big/201504/024448d7ae994d4d9273ea5f3c199a5a.jpg", "风景" + i);
areaEneities.add(areaEntity);
}
mGridAdapter = new GridViewAdapter(areaEneities, MainActivity.this);
gv.setAdapter(mGridAdapter);
gv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (isShowDelete) {
isShowDelete = false;
} else {
isShowDelete = true;
}
mGridAdapter.setIsShowDelete(isShowDelete);
return true;
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
if (isShowDelete == true) {
isShowDelete = false;
mGridAdapter.setIsShowDelete(false);
} else {
finish();
}
}
class GridViewAdapter extends BaseAdapter {
private boolean isShowDelete;//根据这个布尔型变量来判断是否显示删除图标,true是显示,false是不显示
private List<AreaEntity> areaEneity;
private LayoutInflater mInflater;
private Context mContext;
public GridViewAdapter(List<AreaEntity> areaEneities, Context context) {
super();
this.areaEneity = areaEneities;
this.mInflater = LayoutInflater.from(context);
this.mContext = context;
}
public void setIsShowDelete(boolean isShowDelete) {
this.isShowDelete = isShowDelete;
notifyDataSetChanged();
}
@Override
public int getCount() {
return areaEneity.size();
}
@Override
public Object getItem(int position) {
return areaEneity.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.fragmet_grid_item, null);
holder = new ViewHolder();
holder.img = (ImageView) convertView.findViewById(R.id.img);
holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
holder.deleteView = convertView.findViewById(R.id.delete_markView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Picasso.with(mContext).load(areaEneity.get(position).getPic()).into(holder.img);
holder.name_tv.setText(areaEneity.get(position).getArea());
holder.deleteView.setVisibility(isShowDelete ? View.VISIBLE : View.GONE);
holder.deleteView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "您删除了第" + areaEneity.get(position).getId() + "张", Toast.LENGTH_SHORT).show();
areaEneity.remove(areaEneity.get(position));
notifyDataSetChanged();
}
});
return convertView;
}
class ViewHolder {
private View deleteView;
private ImageView img;
private TextView name_tv;
}
}
class AreaEntity {
private String id;
private String pic;
private String area;
public AreaEntity(String id, String pic, String area) {
this.id = id;
this.pic = pic;
this.area = area;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
}
这里面我显示的图片是从网上随便选的一张,加载框架用的是picasso,用Android Studio开发的开发者直接在dependencies里面引入一行代码即可:
compile 'com.squareup.picasso:picasso:2.5.2'
同时别忘了加上网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
好的,功能实现。