当前位置: 代码迷 >> 综合 >> GridView 使用,GirdView 方法属性。GirdView 长按出现右上角图标,点击图标删除
  详细解决方案

GridView 使用,GirdView 方法属性。GirdView 长按出现右上角图标,点击图标删除

热度:30   发布时间:2023-12-16 06:00:57.0
  • 适配器和 ListView 一样。

GirdView 方法属性在这里插入图片描述

    <GridViewandroid:id="@+id/gv"android:horizontalSpacing="5dp"android:numColumns="3"android:stretchMode="columnWidth"android:verticalSpacing="5dp"android:layout_width="match_parent"android:layout_height="match_parent"/>

最终效果
在这里插入图片描述

使用 如: 长按出现右上角图标,点击图标删除

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:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><GridViewandroid:id="@+id/gv"android:horizontalSpacing="5dp"android:numColumns="3"android:stretchMode="columnWidth"android:verticalSpacing="5dp"android:layout_width="match_parent"android:layout_height="match_parent"/>
</LinearLayout>

item_layout.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">
<LinearLayoutandroid:layout_marginRight="4dp"android:layout_marginTop="4dp"android:gravity="center"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:id="@+id/img"android:layout_marginLeft="20dp"android:src="@mipmap/ic_launcher_round"android:layout_width="50dp"android:layout_height="50dp" /><TextViewandroid:id="@+id/tv"android:gravity="center"android:text="你好"android:textSize="28sp"android:layout_width="wrap_content"android:layout_height="50dp" />
</LinearLayout><ImageViewandroid:id="@+id/delete_markView"android:layout_width="10dp"android:layout_height="wrap_content"android:layout_gravity="right|top"android:adjustViewBounds="true"android:src="@mipmap/ic_launcher_round"android:visibility="gone" /></FrameLayout>

Animal.java

public class Animal {public Animal(String animal, int imgId) {this.animal = animal;this.imgId = imgId;}private String animal;private int imgId;public int getImgId() {return imgId;}public void setImgId(int imgId) {this.imgId = imgId;}public String getAnimal() {return animal;}public void setAnimal(String animal) {this.animal = animal;}
}

GirdAdapter.java

public class GirdAdapter extends BaseAdapter {private Context context;private List<Animal> datas;final int position = 0;private boolean mIsShowDelete;public GirdAdapter(Context context, List<Animal> datas){this.context = context;this.datas = datas;}// 子项个数@Overridepublic int getCount() {return datas.size();}// 返回子项对象@Overridepublic Object getItem(int position) {return datas.get(position);}// 返回子项下标@Overridepublic long getItemId(int position) {return position;}// 创建 ViewHolder 类class ViewHolder {ImageView animalImage,deleteImage;TextView animalName;}// 返回子项视图@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {Animal animal = (Animal) getItem(position);View view;ViewHolder viewHolder;if(convertView == null){view = LayoutInflater.from(context).inflate(R.layout.item_layout,null);viewHolder = new ViewHolder();viewHolder.animalImage = view.findViewById(R.id.img);viewHolder.animalName = view.findViewById(R.id.tv);viewHolder.deleteImage = view.findViewById(R.id.delete_markView);view.setTag(viewHolder);}else{view = convertView;viewHolder = (ViewHolder) view.getTag();}viewHolder.animalName.setText(animal.getAnimal());viewHolder.animalImage.setImageResource(animal.getImgId());viewHolder.deleteImage.setVisibility(mIsShowDelete ? View.VISIBLE :View.GONE);if(mIsShowDelete){viewHolder.deleteImage.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {datas.remove(position);setmIsShowDelete(false);}});}return view;}public void setmIsShowDelete(boolean mIsShowDelete){this.mIsShowDelete = mIsShowDelete;notifyDataSetChanged();}}

MainActivity

public class MainActivity extends AppCompatActivity{private GridView gridView;private List<Animal> datas = new ArrayList<>();private GirdAdapter girdAdapter;private  boolean isShowDelete;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initialData();gridView = findViewById(R.id.gv);girdAdapter = new GirdAdapter(this,datas);gridView.setAdapter(girdAdapter);gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(MainActivity.this, datas.get(position).getAnimal(), Toast.LENGTH_SHORT).show();}});// 删除gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {if(isShowDelete){isShowDelete = false;girdAdapter.setmIsShowDelete(isShowDelete);}else{ // 删除图片隐藏长时按显示isShowDelete = true;girdAdapter.setmIsShowDelete(isShowDelete);}return false;}});}private void initialData() {for (int i = 0; i < 20; i++) {datas.add(new Animal("安卓"+i,R.drawable.ic_launcher_round));}}
}