首页 > 其他分享 >Android实战简易教程-第二十枪(GridView长按显示删除)

Android实战简易教程-第二十枪(GridView长按显示删除)

时间:2022-11-11 15:37:08浏览次数:39  
标签:GridView layout myList id isShowDelete import Android 第二十 android


用过UC浏览器的都使用过如下功能:

Android实战简易教程-第二十枪(GridView长按显示删除)_java

长按图标会弹出删除的图标,我们下面研究一下如何实现类似效果。

1.配置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" >


<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4" >
</GridView>


</RelativeLayout>


2.配置GridView的填充组件布局grid_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_grid_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >


<FrameLayout
android:id="@+id/starred_item_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >


<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="4dip"
android:layout_marginTop="4dip"
android:gravity="center"
android:orientation="vertical" >


<ImageView
android:id="@+id/img"
android:layout_width="60dip"
android:layout_height="55dip" />


<TextView
android:id="@+id/name_tv"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>


<ImageView
android:id="@+id/delete_markView"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_gravity="right|top"
android:adjustViewBounds="true"
android:src="@drawable/delete"
android:visibility="gone" />
</FrameLayout>


</LinearLayout>

3.adapter适配器文件:

package com.example.longdemo;

import java.util.ArrayList;
import java.util.HashMap;

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.TextView;

public class GridViewAdapter extends BaseAdapter {
private ArrayList<HashMap<String, Object>> myList;
private Context mContext;
private TextView name_tv;
private ImageView img;
private View deleteView;
private boolean isShowDelete;// 根据这个变量来判断是否显示删除图标,true是显示,false是不显示

public GridViewAdapter(Context mContext,
ArrayList<HashMap<String, Object>> myList) {
this.mContext = mContext;
// this.names=names;
this.myList = myList;
}

public void setIsShowDelete(boolean isShowDelete) {
this.isShowDelete = isShowDelete;
notifyDataSetChanged();
}

@Override
public int getCount() {

return myList.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return myList.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item,
null);
img = (ImageView) convertView.findViewById(R.id.img);
name_tv = (TextView) convertView.findViewById(R.id.name_tv);
deleteView = convertView.findViewById(R.id.delete_markView);
deleteView.setVisibility(isShowDelete ? View.VISIBLE : View.GONE);// 设置删除按钮是否显示
img.setBackgroundResource(myList.get(position).get("image").hashCode());
name_tv.setText(myList.get(position).get("name").toString());
return convertView;
}
}


4.MainActivity.java:

package com.example.longdemo;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;

public class MainActivity extends Activity implements OnItemLongClickListener {

private GridView mGridView;
private GridViewAdapter mAdapter;
private boolean isShowDelete = false;
private ArrayList<HashMap<String, Object>> myList;
final String[] name = new String[] { "test", "testa", "testb", "testc", "testd",
"testf", "testg" };
final int[] pic = new int[] { R.drawable.pic,
R.drawable.pic, R.drawable.pic,
R.drawable.pic, R.drawable.pic,
R.drawable.pic, R.drawable.pic };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (GridView) findViewById(R.id.gridView);
mGridView.setOnItemLongClickListener(this);//长按事件监听
myList = getMenuAdapter(pic, name);

mAdapter = new GridViewAdapter(MainActivity.this, myList);
mGridView.setAdapter(mAdapter);

}

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
if (isShowDelete) {
isShowDelete = false;

} else {
isShowDelete = true;
mAdapter.setIsShowDelete(isShowDelete);
mGridView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
delete(position);//删除选中项
Log.i("------>", "进来了么");
mAdapter = new GridViewAdapter(MainActivity.this, myList);//重新绑定一次adapter
mGridView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();//刷新gridview

}

});
}
Log.i("------>", "进来了没");

mAdapter.setIsShowDelete(isShowDelete);//setIsShowDelete()方法用于传递isShowDelete值

return true;
}


private void delete(int position) {//删除选中项方法
ArrayList<HashMap<String, Object>> newList = new ArrayList<HashMap<String, Object>>();
if (isShowDelete) {
myList.remove(position);
isShowDelete = false;
}
newList.addAll(myList);
myList.clear();
myList.addAll(newList);
}

private ArrayList<HashMap<String, Object>> getMenuAdapter(//将数据装入List
int[] menuImageArray, String[] menuNameArray) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < menuImageArray.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("image", menuImageArray[i]);
map.put("name", menuNameArray[i]);
data.add(map);
}
return data;
}

}


5.运行实例如下:

Android实战简易教程-第二十枪(GridView长按显示删除)_android_02


标签:GridView,layout,myList,id,isShowDelete,import,Android,第二十,android
From: https://blog.51cto.com/u_15866446/5845018

相关文章

  • Android开发技巧四--圆角化控件,让它看起来更美
    当需要为应用程序UI控件选择背景的时候,开发者会添加自定义的颜色和形状来代替系统的默认样式,圆角边框看起来是很不错的效果,开发者只需要添加几行代码,就可以在应用程序中使用......
  • Android clean后出现很多错误解决办法
    当我们从别的项目中拷入一个布局文件进我们的项目时,可能出现在activity中无法找到控件的情况,这时如果你clean了一下项目,就会出现很多报错,这是由于在项目有错误的情况下clean......
  • Android实战简易教程-第二十一枪(GridView动态添加Item)
    本例子实现在GridView的最后显示一个增加图片,点击图片动态增加内容item。1.main.xml:<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xml......
  • Android实战简易教程-第二十五枪(基于Baas的数据表查询下拉刷新和上拉加载实现!)
    上一节我们实现了数据表的加载,但是,当数据表数据很多时,我们就要考虑数据的分页,这里我们选用了PullToRefreshListView控件,先看一下该控件的说明:效果图:        ......
  • 关于MacBook怎么更新Android SDK
    昨天公司的人给了我一个VPN,但是还是无法更新SDK,后来发现将下图:通过VPN发送所有流量勾选以后就可以连接更新了,哎,处处皆学问,特此分享一下此经验。喜欢的朋友关注我哦!多谢支持......
  • Android 找不到资源文件错误的解决办法
    有些时候我们明明在layout文件夹下面配置了布局文件,但是在activity中却报错,提示找不到该文件。解决办法:这是因为你导入的R包不对,将activity文件import中后缀为.R的一行删除,......
  • Android studio 日志输出
      Log.v("Tag","Msg");//Verbose观察值,Verbose是冗长、啰嗦的意思,任何消息都会输出Log.d("Tag","Msg");//Debug调试Log.i("Tag","Msg");//Info信息,为一般提示......
  • Android--调用百度地图API
    首先申请一个百度地图的APIKEY,具体方法网上已有,申请后获得:main.xml代码如下:<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:to......
  • Android项目实战-云词典
    前段时间在网上看到了一篇滴答词典的项目文章,实现简单的单词查找、整句翻译和生词本功能,但是该项目年代久远,所用的API已不再提供数据,我决定利用它的已有框架实现其功能,主要......
  • Android中Task任务栈的分配
    首先我们来看下Task的定义,Google是这样定义Task的:ataskiswhattheuserexperiencesasan"application."It'sagroupofrelatedactivities,arrangedinastack.......