GridView动态删除Item
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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:orientation="vertical" tools:context=".activity.MainActivity"> <GridView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/gridView" android:numColumns="4"></GridView> </LinearLayout>
Article.java
package com.pingyijinren.helloworld.model; /** * Created by Administrator on 2016/6/3 0003. */ public class Article { private int imageId; private String name; public Article(int imageId,String name){ this.imageId=imageId; this.name=name; } public int getImageId() { return imageId; } public String getName() { return name; } }
grid_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"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/image"/> <TextView android:layout_width="match_parent" android:layout_height="20dp" android:id="@+id/name"/> </LinearLayout> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:layout_gravity="right|top" android:src="@drawable/cancel" android:id="@+id/delete"/> </FrameLayout>
ArticleAdapter.java
package com.pingyijinren.helloworld.adapter; import android.content.Context; import android.text.Layout; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import com.pingyijinren.helloworld.R; import com.pingyijinren.helloworld.model.Article; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2016/6/3 0003. */ public class ArticleAdapter extends ArrayAdapter<Article>{ private boolean ifShowDelete=false; private ArrayList<Article> arrayListArticle; private int index; public ArticleAdapter(Context context, int resource, List<Article> objects) { super(context, resource, objects); arrayListArticle=(ArrayList<Article>)objects; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if(convertView==null){ convertView= LayoutInflater.from(getContext()).inflate(R.layout.grid_item,null); viewHolder=new ViewHolder(); viewHolder.image=(ImageView) convertView.findViewById(R.id.image); viewHolder.name=(TextView)convertView.findViewById(R.id.name); viewHolder.delete=(ImageView)convertView.findViewById(R.id.delete); convertView.setTag(viewHolder); } else{ viewHolder=(ViewHolder) convertView.getTag(); } Article article=getItem(position); viewHolder.image.setImageResource(article.getImageId()); viewHolder.name.setText(article.getName()); index=position; if(ifShowDelete){ viewHolder.delete.setVisibility(View.VISIBLE); viewHolder.delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { arrayListArticle.remove(index); notifyDataSetChanged(); } }); } else{ viewHolder.delete.setVisibility(View.GONE); } return convertView; } private class ViewHolder{ public ImageView image; public TextView name; public ImageView delete; } public void setIfShowDelete(boolean ifShowDelete) { this.ifShowDelete = ifShowDelete; } }
MainActivity.java
package com.pingyijinren.helloworld.activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import com.pingyijinren.helloworld.R; import com.pingyijinren.helloworld.adapter.ArticleAdapter; import com.pingyijinren.helloworld.model.Article; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private GridView gridView; private ArrayList<Article> arrayListArticle; private ArticleAdapter articleAdapter; private boolean ifShowDelete=false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView=(GridView)findViewById(R.id.gridView); arrayListArticle=new ArrayList<Article>(); arrayListArticle.add(new Article(R.drawable.first_image,"frist")); arrayListArticle.add(new Article(R.drawable.second_image,"second")); articleAdapter=new ArticleAdapter(MainActivity.this,R.layout.grid_item,arrayListArticle); gridView.setAdapter(articleAdapter); gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { articleAdapter.setIfShowDelete(true); articleAdapter.notifyDataSetChanged(); ifShowDelete=true; return false; } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK){ if(ifShowDelete==true){ if(arrayListArticle.size()==0){ finish(); } else{ articleAdapter.setIfShowDelete(false); articleAdapter.notifyDataSetChanged(); ifShowDelete=false; } } else{ finish(); } } return false; } }