GridView 使用,GirdView 方法屬性。GirdView 長按出現右上角圖示,點選圖示刪除
阿新 • • 發佈:2018-11-08
- 介面卡和 ListView 一樣。
GirdView 方法屬性
<GridView android: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"> <GridView android: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" > <LinearLayout android:layout_marginRight="4dp" android:layout_marginTop="4dp" android:gravity="center" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/img" android:layout_marginLeft="20dp" android:src="@mipmap/ic_launcher_round" android:layout_width="50dp" android:layout_height="50dp" /> <TextView android:id="@+id/tv" android:gravity="center" android:text="你好" android:textSize="28sp" android:layout_width="wrap_content" android:layout_height="50dp" /> </LinearLayout> <ImageView android: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;
}
// 子項個數
@Override
public int getCount() {
return datas.size();
}
// 返回子項物件
@Override
public Object getItem(int position) {
return datas.get(position);
}
// 返回子項下標
@Override
public long getItemId(int position) {
return position;
}
// 建立 ViewHolder 類
class ViewHolder {
ImageView animalImage,deleteImage;
TextView animalName;
}
// 返回子項檢視
@Override
public 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() {
@Override
public 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;
@Override
protected 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() {
@Override
public 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() {
@Override
public 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));
}
}
}