标签:
layout.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:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="3" android:horizontalSpacing="2dp" android:verticalSpacing="2dp" ></GridView> </RelativeLayout>
gridview_item.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" > <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="100dp" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/image_delete" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:src="@drawable/delete" /> </RelativeLayout>
MyAdapter.java
package com.example.day09_gridview_listener; import java.util.List; import android.content.Context; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.Toast; class MyAdapter extends BaseAdapter{ private List<Integer> dataList; private Context context; private boolean isShow = false; public MyAdapter(List<Integer> dataList, Context context) { super(); this.dataList = dataList; this.context = context; } public boolean getisShow() { return isShow; } public void setisShow(boolean isShow) { this.isShow = isShow; //更新数据 notifyDataSetChanged(); } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = View.inflate(context, R.layout.gridview_item, null); ImageView image = (ImageView) view.findViewById(R.id.image); ImageView deleteImg = (ImageView) view.findViewById(R.id.image_delete); //设置删除图标是否显示 可编辑?显示:消失 Log.i("MainActivity---------", getisShow()+""); deleteImg.setVisibility(isShow?View.VISIBLE:View.GONE); image.setImageResource(dataList.get(position)); deleteImg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "点击了XX", 0).show(); dataList.remove(dataList.get(position)); notifyDataSetChanged(); } }); return view; } }
Main.java
package com.example.day09_gridview_listener; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.Toast; /** * 需求:gridView实现点击X按钮实现删除图片的功能 * * 1.初始化控件 * 2.初始化数据 * 3.实现BaseAdapter中方法将数据填充 * 1.初始页面 * 2.设置长按监听事件 * 1.普通操作--不操作 * 2,编辑操作--长按显示x图标 * 1.点击x图标删除该图片,更新view * 3.设置点击监听事件 * 1,编辑操作--不操作 * 2.普通操作--点击恢复原状,x图标消失= * 4.创建适配器 * 5.设置适配器 * @author my * */ public class MainActivity extends Activity { private int imageRes[] = {R.drawable.f000,R.drawable.f001,R.drawable.f002,R.drawable.f003,R.drawable.f004,R.drawable.f005,R.drawable.f006,R.drawable.f007,R.drawable.f008}; private List<Integer>list; private GridView gridView; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridView); //设置数据源 list = getListByArray(); myAdapter = new MyAdapter(list,MainActivity.this); //设置适配器 gridView.setAdapter(myAdapter); //设置单击事件 gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "单击", 0).show(); if(myAdapter.getisShow()){ myAdapter.setisShow(false); }else{ } } }); //设置长按事件 gridView.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "长按", 0).show(); if(myAdapter.getisShow()){ }else{ myAdapter.setisShow(true); } return true; } }); } private List<Integer> getListByArray() { list = new ArrayList<Integer>(); for (int i = 0; i < imageRes.length; i++) { list.add(imageRes[i]); } return list; } }
普通状态
长按
删除
Android_gridView_LIstener_examle
标签:
原文地址:http://www.cnblogs.com/fangg/p/5743553.html