先上图片效果;
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"; Listaddrs; 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>