热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

带添加按钮的GridView,item的删除事件

先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve

先上图片效果;



gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:

布局文件:addr_manage.xml

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_match_parent"  android:layout_match_parent"  android:background="@color/black"  android:paddingBottom="@dimen/px_30"  android:paddingLeft="@dimen/px_30"  android:paddingRight="@dimen/px_30"  android:paddingTop="@dimen/px_100"  tools:cOntext="com.haier.mine_wine.address.addrmanage.AddrManageActivity">

    <RelativeLayout  android:id="@+id/rl_manageBottom"  android:gravity="center_vertical"  android:layout_marginTop="@dimen/dp_20"  android:layout_alignParentBottom="true"  android:layout_match_parent"  android:layout_@dimen/px_92">
        <ImageView  android:id="@+id/iv_manaBack"  android:src="@drawable/common_back"  android:layout_@dimen/px_92"  android:layout_@dimen/px_92" />
        <ImageView  android:id="@+id/iv_backTohome"  android:src="@drawable/home"  android:layout_toRightOf="@+id/iv_manaBack"  android:layout_marginLeft="@dimen/px_10"  android:layout_@dimen/px_92"  android:layout_@dimen/px_92" />
    RelativeLayout>
    <LinearLayout  android:background="@drawable/bg"  android:orientation="vertical"  android:layout_above="@id/rl_manageBottom"  android:layout_match_parent"  android:layout_match_parent">
        <TextView  android:text="地址管理"  android:gravity="center"  color:#008000;">"@style/addr_item_style"  android:layout_match_parent"  android:layout_@dimen/px_130" />
        <GridView  android:id="@+id/gv_addrs"  android:horizOntalSpacing="@dimen/px_30"  android:verticalSpacing="@dimen/px_30"  android:numColumns="3"  android:layout_marginLeft="5px"  android:layout_marginRight="8px"  android:listSelector="@color/color_trans"  android:layout_match_parent"  android:layout_match_parent">
        GridView>
        <RelativeLayout  android:id="@+id/rl_add"  android:visibility="gone"  android:gravity="center"  android:layout_@dimen/addr_item_width"  android:layout_@dimen/addr_height">
            <ImageView  android:id="@+id/iv_add"  android:src="@drawable/addfood"  android:layout_wrap_content"  android:layout_wrap_content" />
            <TextView  android:layout_below="@+id/iv_add"  android:text="添加新地址"  android:layout_marginTop="@dimen/px_60"  color:#008000;">"@style/addr_item_style"  android:layout_wrap_content"  android:layout_wrap_content" />
        RelativeLayout>
    LinearLayout>


RelativeLayout>
 activity页面数据显示: 
 

public void showAddrList(SelDataBean bean) {
    DialogUtil.cancelRoundDialog();
    //  if(bean.getAddress_list() != null && bean.getAddress_list().size() != 0){
        rlAdd.setVisibility(View.GONE);
        gvAddrs.setVisibility(View.VISIBLE);
        addressInfoList = bean.getAddress_list();
        adapter = new AddrGridAdapter(mContext,addressInfoList,addrManagePresenter);
        gvAddrs.setAdapter(adapter);

    }else{
        if(bean.getError()!= null){
            Toast.makeText(mContext,bean.getError().toString(),Toast.LENGTH_LONG).show();
        }
        //如果没有添加任何地址信息,则只显示添加按钮  rlAdd.setVisibility(View.VISIBLE);
        gvAddrs.setVisibility(View.GONE);
        rlAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent addIntent = new Intent(mContext, AddAddrActivity.class);
                addIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(addIntent);
            }
        });
    }
}

GridView的adapter:

package com.haier.mine_wine.address.addrmanage.adapter;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.haier.mine_wine.R;
import com.haier.mine_wine.R2;
import com.haier.mine_wine.address.addAddr.AddAddrActivity;
import com.haier.mine_wine.address.addrmanage.AddrManageActivity;
import com.haier.mine_wine.address.addrmanage.bean.AddrDetail;
import com.haier.mine_wine.address.addrmanage.bean.AddressInfo;
import com.haier.mine_wine.address.addrmanage.presenter.AddrManagePresenter;
import com.haier.wine_commen.html.ServiceAddr;
import com.haier.wine_commen.util.UserLoginConfig;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**  * Created by wangchm on 2016/9/27 0027.  * 地址管理adapter  */ public class AddrGridAdapter extends BaseAdapter {

// private static final String KEY = "d653b22472df13b7c9d36df4684ad711";  List addrs;
    Context mContext;
    AddrManagePresenter addrManagePresenter;

    public AddrGridAdapter(Context mContext, List list,AddrManagePresenter addrManagePresenter) {
        this.addrs = list;
        this.mContext = mContext;
        this.addrManagePresenter = addrManagePresenter;
    }

    @Override
    public int getCount() {
        return addrs == null ? 0 : addrs.size()+1;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        AddrHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.addr_grid_item, null);
            holder = new AddrHolder(view);
            view.setTag(holder);
        }else {
            holder = (AddrHolder) view.getTag();
        }

        //第一项显示添加按钮  if(i == 0){
            holder.rlAdd.setVisibility(View.VISIBLE);
            holder.rlInfo.setVisibility(View.GONE);
        }else{
            final AddressInfo addrDetail = addrs.get(i-1);
            holder.tvUserName.setText(addrDetail.getTrue_name());
            holder.tvPhone.setText(addrDetail.getMob_phone());
            holder.tvAddr.setText(addrDetail.getArea_info()+" "+addrDetail.getAddress());
            if(addrDetail.getIs_default().equals("1")){
                holder.ivSetDefault.setSelected(true);
                holder.tvSetDetault.setText("默认地址");
            }else{
                holder.ivSetDefault.setSelected(false);
            }

            holder.llDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                    builder.setMessage("是否确认删除该地址?");
                    final Dialog dialog = new AlertDialog.Builder(mContext).create();
                    //自定义布局  View layout =LayoutInflater.from(mContext).inflate(R.layout.mydialog,null);
                    dialog.setCancelable(false);
                    dialog.show();
                    dialog.getWindow().setContentView(layout);
                    TextView tv_title =(TextView) layout.findViewById(R.id.tv_msg);
                    tv_title.setText("是否确认删除该地址?");
                    //确认按钮  Button btn_cOnfirm= (Button) layout.findViewById(R.id.iv_submit);
                    btn_confirm.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            HashMap map = new HashMap();
                            map.put("key", UserLoginConfig.getAccessToken());
                            map.put("address_id",addrDetail.getAddress_id());
                            addrManagePresenter.delAddr(ServiceAddr.BASE_URL,map);
                            dialog.dismiss();
                        }
                    });

                    //取消按钮  Button btn_cancel = (Button) layout.findViewById(R.id.iv_cancel);
                    btn_cancel.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();
                        }
                    });  }
            });
        }



        return view;
    }

    public class AddrHolder {
        @BindView(R2.id.tv_userName)
        TextView tvUserName;
        @BindView(R2.id.tv_phone)
        TextView tvPhone;
        @BindView(R2.id.tv_addr)
        TextView tvAddr;
        @BindView(R2.id.iv_setDefault)
        ImageView ivSetDefault;
        @BindView(R2.id.tv_setDetault)
        TextView tvSetDetault;
        @BindView(R2.id.ll_edit)
        LinearLayout llEdit;
        @BindView(R2.id.ll_delete)
        LinearLayout llDelete;

        @BindView(R2.id.rl_add)
        RelativeLayout rlAdd;

        @BindView(R2.id.rl_info)
        RelativeLayout rlInfo;

        AddrHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

adapter的布局文件:

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:background="@drawable/ll_trans_border"  android:layout_match_parent"  android:layout_match_parent">
    <RelativeLayout  android:id="@+id/rl_add"  android:visibility="gone"  android:gravity="center"  android:layout_match_parent"  android:layout_@dimen/addr_height">
        <ImageView  android:id="@+id/iv_add"  android:src="@drawable/addfood"  android:layout_wrap_content"  android:layout_wrap_content" />
        <TextView  android:layout_below="@+id/iv_add"  android:text="添加新地址"  android:layout_marginTop="@dimen/px_60"  color:#008000;">"@style/addr_item_style"  android:layout_wrap_content"  android:layout_wrap_content" />
    RelativeLayout>

    <RelativeLayout  android:id="@+id/rl_info"  android:orientation="vertical"  android:padding="@dimen/px_60"  android:layout_match_parent"  android:layout_@dimen/addr_height">
        <RelativeLayout  android:id="@+id/rl_addr_user"  android:layout_match_parent"  android:layout_wrap_content">
            <TextView  android:id="@+id/tv_userName"  color:#008000;">"@style/addr_item_style"  android:layout_wrap_content"  android:layout_wrap_content" />
            <TextView  android:id="@+id/tv_phone"  color:#008000;">"@style/addr_item_style"  android:layout_alignParentRight="true"  android:layout_wrap_content"  android:layout_wrap_content" />
        RelativeLayout>

        <TextView  android:id="@+id/tv_addr"  android:layout_below="@+id/rl_addr_user"  color:#008000;">"@style/addr_item_style"  android:layout_marginBottom="@dimen/px_60"  android:layout_marginTop="@dimen/px_30"  android:maxLines="2"  android:lines="2"  android:layout_match_parent"  android:layout_wrap_content" />
        <RelativeLayout  android:layout_alignParentBottom="true"  android:layout_match_parent"  android:layout_wrap_content">
            <ImageView  android:id="@+id/iv_setDefault"  android:src="@drawable/addr_default"  android:layout_@dimen/dp_20"  android:layout_@dimen/dp_20" />
            <TextView  android:id="@+id/tv_setDetault"  android:text="设为默认"  color:#008000;">"@style/addr_sub_item_style"  android:layout_marginLeft="@dimen/px_16"  android:layout_toRightOf="@+id/iv_setDefault"  android:layout_wrap_content"  android:layout_wrap_content" />
            <LinearLayout  android:layout_alignParentRight="true"  android:layout_wrap_content"  android:layout_wrap_content">
                <LinearLayout  android:id="@+id/ll_edit"  android:layout_marginRight="@dimen/px_50"  android:layout_wrap_content"  android:layout_wrap_content">
                    <ImageView  android:src="@drawable/edit_icon"  android:layout_marginRight="@dimen/px_8"  android:layout_@dimen/dp_20"  android:layout_@dimen/dp_20" />
                    <TextView  android:text="编辑"  color:#008000;">"@style/addr_sub_item_style"  android:layout_wrap_content"  android:layout_wrap_content" />
                LinearLayout>
                <LinearLayout  android:id="@+id/ll_delete"  android:focusable="false"  android:clickable="true"  android:layout_wrap_content"  android:layout_wrap_content">
                    <ImageView  android:src="@drawable/delete_icon"  android:layout_marginRight="@dimen/px_8"  android:layout_@dimen/dp_20"  android:layout_@dimen/dp_20" />
                    <TextView  android:text="删除"  color:#008000;">"@style/addr_sub_item_style"  android:layout_wrap_content"  android:layout_wrap_content" />
                LinearLayout>
            LinearLayout>
        RelativeLayout>
    RelativeLayout>


LinearLayout>

删除按钮的自定义Dialog:

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:background="@color/color_trans"  android:layout_match_parent"  android:layout_match_parent">
    <LinearLayout  android:orientation="vertical"  android:background="@drawable/dialog_bg_style"  android:layout_@dimen/addr_dialog_width"  android:layout_wrap_content">
        <TextView  android:id="@+id/tv_title"  android:text="提示"  android:visibility="gone"  android:layout_match_parent"  android:layout_wrap_content" />
        <TextView  android:id="@+id/tv_msg"  android:gravity="center_horizontal"  android:textColor="@color/color_orange"  android:textSize="@dimen/px_28"  android:layout_marginTop="@dimen/px_70"  android:layout_marginBottom="@dimen/px_72"  android:layout_match_parent"  android:layout_wrap_content" />
        <LinearLayout  android:gravity="center_horizontal"  android:layout_marginBottom="@dimen/px_20"  android:layout_match_parent"  android:layout_wrap_content">

            <Button  android:id="@+id/iv_cancel"  android:text="取消"  android:textSize="@dimen/px_24"  android:textColor="@color/white"  android:background="@drawable/corner_orange_button"  android:layout_marginRight="@dimen/px_78"  android:layout_@dimen/px_142"  android:layout_@dimen/px_52" />
            <Button  android:id="@+id/iv_submit"  android:text="确认"  android:textSize="@dimen/px_24"  android:textColor="@color/white"  android:background="@drawable/corner_orange_button"  android:layout_@dimen/px_142"  android:layout_@dimen/px_52" />


        LinearLayout>
    LinearLayout>

RelativeLayout>


推荐阅读
  • 本篇文章笔者在上海吃饭的时候突然想到的这段时间就有想写几篇关于返回系统的笔记,所以回家到之后就奋笔疾书的写出来发布了事先在网上找了很多方法,发现有 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文介绍了Python字典视图对象的示例和用法。通过对示例代码的解释,展示了字典视图对象的基本操作和特点。字典视图对象可以通过迭代或转换为列表来获取字典的键或值。同时,字典视图对象也是动态的,可以反映字典的变化。通过学习字典视图对象的用法,可以更好地理解和处理字典数据。 ... [详细]
  • Tkinter Frame容器grid布局并使用Scrollbar滚动原理
    本文介绍了如何使用Tkinter实现Frame容器的grid布局,并通过Scrollbar实现滚动效果。通过将Canvas作为父容器,使用滚动Canvas来滚动Frame,实现了在Frame中添加多个按钮,并通过Scrollbar进行滚动。同时,还介绍了更新Frame大小和绑定滚动按钮的方法,以及配置Scrollbar的相关参数。 ... [详细]
  • 如何用Matlab快速画出带有3D渲染效果的复杂曲面
    简要地介绍了一下如何用Matlab快速画出带有3D渲染效果的复杂曲面图,包括三维曲面绘制、光线、材质、着色等等控制,以及如何 ... [详细]
  • ExcelApp#启动excel程序ExcelAppCreateOleObject(“Excel.Application”);#加载文件但不显示文件内容(true表 ... [详细]
author-avatar
min-章_998
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有