ExpendableListView 实现多选并且动态改变数据 + 输入框的动态改变数据

 


package com.basedemo.my.xi.basedemo.fragment;

import android.content.Context;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import com.basedemo.my.xi.basedemo.R;
import com.basedemo.my.xi.basedemo.activity.Home_FilialPayActivity;
import com.basedemo.my.xi.basedemo.bean.ChangGuiPayCommitBean;
import com.basedemo.my.xi.basedemo.bean.ServicePayBean;
import com.basedemo.my.xi.basedemo.bean.ServicePayCommitBean;
import com.basedemo.my.xi.basedemo.fragment.BaseFragment;
import com.basedemo.my.xi.basedemo.net.NetPath;
import com.basedemo.my.xi.basedemo.utils.LogUtils;
import com.basedemo.my.xi.basedemo.utils.SPUtils;
import com.basedemo.my.xi.basedemo.utils.ToastUtils;
import com.google.gson.Gson;
import com.yolanda.nohttp.rest.Response;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Xi
 * Created by zz.wbkj.xi on 2016/8/26.
 */
public class ServicePayPageFragment extends BaseFragment {

    private String elder_id;
    private ExpandableListView expandableListView;

    private String user_id;
    private TextView tv_older_name;
    private TextView tv_home_name;
    private TextView tv_start_time;
    private EditText et_keyongjifen;
    private TextView tv_jifen_dixiao_rmb;
    private TextView tv_need_pay;
    private String keyongjifen_number;
    private Double input_jifen;
    private double needpay = 0;
    private int rate;
    private double pay_money;
    private Button btn_pay;
    private List<String> pay_group_names = new ArrayList<>();
    private InputMethodManager imm;
    private ScrollView scrollView;

    @Override
    public void init() {
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
//        imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);

        user_id = (String) SPUtils.get(mContext,"user_id","");
        Bundle bundle = getArguments();//从activity传过来的Bundle
        if(bundle!=null){
            elder_id = bundle.getString("elder_id");
        }
    }

    @Override
    public int getLayouId() {
        return R.layout.page_servicepay;
    }

    @Override
    public void initView(View view) {
        tv_older_name = (TextView)view.findViewById(R.id.tv_older_name);
        tv_home_name = (TextView)view.findViewById(R.id.tv_home_name);
        tv_start_time = (TextView)view.findViewById(R.id.tv_start_time);
        et_keyongjifen = (EditText)view.findViewById(R.id.et_keyongjifen);
        tv_need_pay = (TextView)view.findViewById(R.id.tv_need_pay);
        tv_jifen_dixiao_rmb = (TextView)view.findViewById(R.id.tv_jifen_dixiao_rmb);
        expandableListView = (ExpandableListView)view.findViewById(R.id.expandableListView);
        scrollView = (ScrollView)view.findViewById(R.id.scrollView);
//        scrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                Rect mRect = new Rect();
//                mOuterMostCoverLl.getGlobalVisibleRect(mRect);
//                if (mOuterCoverBottom == Integer.MIN_VALUE) {
//                    mOuterCoverBottom = mRect.bottom;
//                    return;
//                }
//                if (mRect.bottom < mOuterCoverBottom) {//键盘弹出
//                    LayoutParams mEmotionsView = mEmotionsViewListLl.getLayoutParams();
//                    if (mEmotionsViewIsShow) {
//                        mEmotionsViewListLl.setVisibility(View.GONE);
//                    }
//                    if (mKeyboardHeight > 0 && mEmotionsView.height != mKeyboardHeight) {
//                        mEmotionsView.height = mKeyboardHeight;
//                        mEmotionsViewListLl.setLayoutParams(mEmotionsView);
//                    }
//                    mkeyboardIsShow = true;
//                    mEmotionsViewIBtn.setVisibility(View.VISIBLE);
//                    mKeyboardIBtn.setVisibility(View.GONE);
//                    mKeyboardHeight = mOuterCoverBottom - mRect.bottom;
//                } else {//键盘未弹出
//                    mkeyboardIsShow = false;
//                    if (mEmotionsViewIsShow) {
//                        mEmotionsViewListLl.setVisibility(View.VISIBLE);
//                        mEmotionsViewIBtn.setVisibility(View.GONE);
//                        mKeyboardIBtn.setVisibility(View.VISIBLE);
//                    } else {
//                        mCurrentRplyId = 0;
//                        mInputContentEt.setHint(getResources().getString(R.string.realnamedynamicdetailactivity_wanttocomment));
//                    }
//                }
//
//            }
//        });
        btn_pay = (Button)view.findViewById(R.id.btn_pay);
//        imm.showSoftInput(btn_pay,InputMethodManager.SHOW_IMPLICIT);
    }

    @Override
    public void initData() {
        LogUtils.e(mContext,"user_id为:",user_id);
        Map<String,String> map = new HashMap<>();
        map.put("elder_id",elder_id);
        map.put("user_id",user_id);
        requestNet_POST(NetPath.SERVICE_PAY,3,map);
    }

    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isVisible()){
//            LogUtils.e(mContext,"user_id为:",user_id);
            Map<String,String> map = new HashMap<>();
            map.put("elder_id",elder_id);
            map.put("user_id",user_id);
            requestNet_POST(NetPath.SERVICE_PAY,3,map);
        }else if (!isVisibleToUser&& isVisible()){
            needpay = 0;
            et_keyongjifen.setText("");
            for (int i = 0; i < ivList.size(); i++) {
                ivList.get(i).setImageResource(R.drawable.off);
            }

        }
    }

    List<ImageView> ivList = new ArrayList<>();


    @Override
    public void onSucceedPaser(int what, Gson gson, Response<String> response) {
        super.onSucceedPaser(what, gson, response);
        switch (what){
            case 3:
                try{
                    ServicePayBean bean = gson.fromJson(response.get(),ServicePayBean.class);
                    final ServicePayBean.DataBean dataBean = bean.getData();
                    tv_older_name.setText(dataBean.getElder_name());
                    tv_home_name.setText(dataBean.getBeadhouse_name());
                    tv_start_time.setText(dataBean.getStart_time());
                    expandableListView.setAdapter(new BaseExpandableListAdapter() {
                        @Override
                        public int getGroupCount() {
                            return dataBean.getCharge().size();
                        }

                        @Override
                        public int getChildrenCount(int groupPosition) {
                            return dataBean.getCharge().get(groupPosition).getDetail().size();
                        }

                        @Override
                        public Object getGroup(int groupPosition) {
                            return dataBean.getCharge().get(groupPosition);
                        }

                        @Override
                        public Object getChild(int groupPosition, int childPosition) {
                            return dataBean.getCharge().get(groupPosition).getDetail().get(childPosition);
                        }

                        @Override
                        public long getGroupId(int groupPosition) {
                            return groupPosition;
                        }

                        @Override
                        public long getChildId(int groupPosition, int childPosition) {
                            return childPosition;
                        }

                        @Override
                        public boolean hasStableIds() {
                            return false;
                        }

                        @Override
                        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
                            ParentViewHolder holder = null;
                            if (convertView == null){
                                convertView = LayoutInflater.from(mContext).inflate(R.layout.page_servicepay_expandablelistview_parent,null);
                                holder = new ParentViewHolder();
                                holder.rl_iv = (RelativeLayout) convertView.findViewById(R.id.rl_iv);
                                holder.iv = (ImageView) convertView.findViewById(R.id.iv);
                                holder.parent_name = (TextView) convertView.findViewById(R.id.tv_feiyong_name);
                                holder.parent_number = (TextView) convertView.findViewById(R.id.tv_feiyong_parent_number);
                                holder.parent_money = (TextView) convertView.findViewById(R.id.tv_feiyong_parent_money);
                                holder.iv_arrow = (ImageView) convertView.findViewById(R.id.iv_arrow);
                                convertView.setTag(holder);
                            }else {
                                holder = (ParentViewHolder) convertView.getTag();
                            }
                            holder.parent_name.setText(dataBean.getCharge().get(groupPosition).getCharge_name());
                            holder.parent_number.setText(dataBean.getCharge().get(groupPosition).getNum()+"次");
                            holder.parent_money.setText(dataBean.getCharge().get(groupPosition).getCharge_money()+"元");

                            if (isExpanded){
                                holder.iv_arrow.setImageResource(R.drawable.arrow_blue_up);
                            }else {
                                holder.iv_arrow.setImageResource(R.drawable.arrow_blue_down);
                            }
                            final ImageView fiv = holder.iv;
                            final int fgroupPosition = groupPosition;
                            final ParentViewHolder finalHolder = holder;
                            holder.rl_iv.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    ivList.add(fiv);
                                    if (finalHolder.isGouXuan){
                                        fiv.setImageResource(R.drawable.off);
                                        needpay -= Double.valueOf(dataBean.getCharge().get(fgroupPosition).getCharge_money());
                                        tv_need_pay.setText("¥"+needpay);
                                        if (pay_group_names.contains(finalHolder.parent_name.getText().toString().trim()))
                                            pay_group_names.remove(finalHolder.parent_name.getText().toString().trim());
                                        et_keyongjifen.setText("");
                                        finalHolder.isGouXuan = false;
                                    }else {
                                        fiv.setImageResource(R.drawable.duigou);
                                        needpay += Double.valueOf(dataBean.getCharge().get(fgroupPosition).getCharge_money());
                                        tv_need_pay.setText("¥"+needpay);
                                        if (!pay_group_names.contains(finalHolder.parent_name.getText().toString().trim()))
                                            pay_group_names.add(finalHolder.parent_name.getText().toString().trim());
                                        et_keyongjifen.setText("");
                                        finalHolder.isGouXuan = true;
                                    }
                                }
                            });
                            return convertView;
                        }
                        class ParentViewHolder{

                            public RelativeLayout rl_iv;
                            public ImageView iv;
                            public TextView parent_name;
                            public TextView parent_number;
                            public TextView parent_money;
                            public ImageView iv_arrow;
                            public boolean isGouXuan = false;
                        }

                        @Override
                        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
                            ChildViewHolder holder = null;
                            if (convertView == null){
                                convertView = LayoutInflater.from(mContext).inflate(R.layout.page_servicepay_expandablelistview_child,null);
                                holder = new ChildViewHolder();
                                holder.tv_time = (TextView)convertView.findViewById(R.id.tv_time);
                                holder.tv_number = (TextView)convertView.findViewById(R.id.tv_number);
                                holder.tv_money = (TextView)convertView.findViewById(R.id.tv_money);
                                convertView.setTag(holder);
                            }else {
                                holder = (ChildViewHolder) convertView.getTag();
                            }
                            holder.tv_time.setText(dataBean.getCharge().get(groupPosition).getDetail().get(childPosition).getCharge_time());
                            holder.tv_money.setText(dataBean.getCharge().get(groupPosition).getDetail().get(childPosition).getCharge_num()+"元");
                            return convertView;
                        }

                        @Override
                        public boolean isChildSelectable(int groupPosition, int childPosition) {
                            return false;
                        }
                        class ChildViewHolder{

                            public TextView tv_time;
                            public TextView tv_number;
                            public TextView tv_money;
                        }
                    });
                    keyongjifen_number = dataBean.getPay_points();
                    et_keyongjifen.setHint("可用"+ keyongjifen_number +"积分");
                    rate = dataBean.getPoints_rate();
                }catch (Exception e){

                }
                break;
            case 4:
                try {
                    ServicePayCommitBean bean = gson.fromJson(response.get(),ServicePayCommitBean.class);
                    LogUtils.e(mContext,"打印的bean",bean.toString());
                    LogUtils.e(mContext,"已经提交了","");
                    if (bean.getData()==1){
                        ToastUtils.showToast(mContext,bean.getMsg());
//                        ((Home_FilialPayActivity)mContext).finish();
                        needpay = 0;
                        tv_need_pay.setText("¥"+0.0);
                        et_keyongjifen.setText("");
                        pay_money = 0;
                        Map<String,String> map = new HashMap<>();
                        map.put("elder_id",elder_id);
                        map.put("user_id",user_id);
                        requestNet_POST(NetPath.SERVICE_PAY,3,map);
                    }else {
                        ToastUtils.showToast(mContext,bean.getMsg());
                    }
                }catch (Exception e){

                }
                break;
        }
    }

    @Override
    public void initListener() {
//        et_keyongjifen.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if (hasFocus){
//
                    scrollView.scrollTo(0,.getHeight());
//                    imm.showSoftInput(btn_pay,InputMethodManager.SHOW_IMPLICIT);
//                    scrollView.fullScroll(ScrollView.FOCUS_DOWN);//滚动到底部
//                }
//            }
//        });
        et_keyongjifen.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (TextUtils.isEmpty(et_keyongjifen.getText().toString().trim())){
                    et_keyongjifen.setHint("可用"+keyongjifen_number+"积分");
                    tv_jifen_dixiao_rmb.setText("积分充抵0.0元");
                    tv_need_pay.setText("¥"+needpay);
                }else if(Integer.valueOf(et_keyongjifen.getText().toString().trim())<=0){
                    et_keyongjifen.setText("");
                }else if(Integer.valueOf(et_keyongjifen.getText().toString().trim()) > Integer.valueOf(keyongjifen_number)){
                    ToastUtils.showToast(mContext,"输入的积分不能大于当前可用积分");
                    et_keyongjifen.setText("");
                }else {
                    input_jifen = Double.valueOf(et_keyongjifen.getText().toString().trim());
                    DecimalFormat df = new DecimalFormat("0.0");
                    String jifen_dixiao_rmb = df.format(input_jifen/rate);
                    tv_jifen_dixiao_rmb.setText("积分充抵"+jifen_dixiao_rmb+"元");
                    pay_money = Double.valueOf(needpay) - Double.valueOf(jifen_dixiao_rmb);
                    LogUtils.e(mContext,"tv_jifen_dixiao_rmb:"+jifen_dixiao_rmb,"needpay:"+needpay+"pay_money:"+pay_money);
                    if (pay_money<0){
                        ToastUtils.showToast("缴纳金额不能小于0");
                        pay_money = 0;
                        et_keyongjifen.setText("");
                    }else {
                        String str_pay_money = df.format(pay_money);
                        tv_need_pay.setText("¥"+str_pay_money);
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        btn_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 进行支付  传值给服务器
                Map<String,String> map = new HashMap<String, String>();
                map.put("elder_id",elder_id);
                map.put("user_id",user_id);
                String jifen = et_keyongjifen.getText().toString().trim();
                map.put("pay_points",TextUtils.isEmpty(jifen)? "0" : jifen);
                map.put("pay_money",tv_need_pay.getText().toString().substring(1));
                StringBuilder sb = new StringBuilder();

//               || Integer.valueOf(tv_need_pay.getText().toString().substring(1))==0
                if (pay_group_names == null || pay_group_names.size()==0){
                    ToastUtils.showToast("请选择支付的费用,再提交");
                    return;
                }else {

                    for (int i = 0; i < pay_group_names.size(); i++) {
                        if (i==0){
                            sb.append(pay_group_names.get(i));
                        }else{
                            sb.append(","+pay_group_names.get(i));
                        }
                    }
                    LogUtils.e(mContext,"提交 参数sb 为",sb.toString());
                    pay_group_names.clear();
                }


                LogUtils.e(mContext,"打印出来的请求",
                        NetPath.SERVICE_PAY_COMMIT+
                        "&elder_id="+elder_id+
                        "&user_id="+user_id+
                        "&pay_points="+(TextUtils.isEmpty(jifen)? "0" : jifen)+
                        "&pay_money="+tv_need_pay.getText().toString().substring(1)+
                        "&charge_names="+sb.toString());
                map.put("charge_names",sb.toString());
                requestNet_POST( NetPath.SERVICE_PAY_COMMIT,4,map);


            }
        });
    }
}

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f5f5f5"
    android:id="@+id/scrollView"
    android:fillViewport="true"
    >

    <LinearLayout
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:background="@color/white"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_marginTop="18dp"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:background="@drawable/kuang">
                <TextView
                    android:id="@+id/tv_older_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="15dp"
                    android:textColor="#666666"
                    android:text=""
                    />
                <TextView
                    android:id="@+id/tv_home_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:textSize="15dp"
                    android:textColor="#666666"
                    android:text=""/>
                <TextView
                    android:id="@+id/tv_start_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:textSize="15dp"
                    android:textColor="#666666"
                    android:text=""/>
            </LinearLayout>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="15dp"
                android:layout_marginBottom="15dp"
                android:textSize="13dp"
                android:textColor="#ababab"
                android:gravity="center"
                android:text="请选择要缴费明细"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_marginTop="2dp"
                android:layout_height="wrap_content">
                <com.basedemo.my.xi.basedemo.view.MyExpandableListView
                    android:id="@+id/expandableListView"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:dividerHeight="0dp"
                    android:divider="@color/transparent"
                    android:groupIndicator="@null"
                    android:layout_height="wrap_content">
                </com.basedemo.my.xi.basedemo.view.MyExpandableListView>

            </LinearLayout>


            <View
                android:layout_width="match_parent"
                android:layout_height="1.5px"
                android:layout_marginTop="18dp"
                android:layout_marginBottom="15dp"
                android:background="#d9d9d9"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginBottom="14dp"
                android:textSize="13dp"
                android:textColor="#ababab"
                android:text="本次缴费说明"/>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusableInTouchMode="true"
                android:focusable="true"
                android:layout_marginBottom="20dp">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:textSize="13dp"
                    android:textColor="#999999"
                    android:text="使用积分冲抵金额:"/>

                <EditText
                    android:id="@+id/et_keyongjifen"
                    android:layout_width="wrap_content"
                    android:layout_height="18dp"
                    android:inputType="number"
                    android:textColor="#999999"
                    android:textSize="11dp"
                    android:hint="可用0积分"
                    android:gravity="center"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/jifenkuang"
                    />

            </RelativeLayout>



            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:textSize="13dp"
                    android:textColor="#999999"
                    android:text="本次合计缴纳金额:"/>
                <TextView
                    android:id="@+id/tv_need_pay"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:textColor="@color/toolbar_bg"
                    android:text="¥0.0"/>
            </RelativeLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_marginTop="11dp"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/tv_jifen_dixiao_rmb"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:textSize="10dp"
                    android:textColor="#ff9900"
                    android:text="积分冲抵 0.0元"/>
            </RelativeLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textColor="#ff408f"
                android:textSize="13dp"
                android:layout_marginTop="24dp"
                android:layout_marginBottom="12dp"
                android:text="您的缴费会增加爱心值哦!"/>
        </LinearLayout>
        <View android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_marginTop="21dp"
            android:layout_marginBottom="21dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/btn_pay"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:background="@drawable/zhifubg"
                android:layout_centerInParent="true"
                android:textColor="@color/white"
                android:textSize="20dp"
                android:text="立即付款"/>
        </RelativeLayout>
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</ScrollView>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值