用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

类似购物车+上拉加载上拉刷新PullToRefresh

2017-02-05 作者: cwm1996举报

[android]代码库

 <com.handmark.pulltorefresh.library.PullToRefreshExpandableListView
        android:layout_below="@id/r"
        android:id="@+id/expand_list"
        android:layout_width="match_parent"
        android:layout_height="570dp" />
///////////////////////////////////////////////////////

package com.bwie.text_a;

import android.app.ExpandableListActivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.util.Log;


import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import android.widget.TextView;
import android.widget.Toast;


import com.bwie.bean.Bean;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;

import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView;


import com.handmark.pulltorefresh.library.PullToRefreshListView;


import com.lidroid.xutils.HttpUtils;


import com.lidroid.xutils.exception.HttpException;


import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;


import java.util.ArrayList;

import java.util.List;
////////////********************/ExpandableListActivity
public
class
MainActivity extends ExpandableListActivity implements PullToRefreshBase.OnRefreshListener2, Myadapter.CheckInterface {

    private PullToRefreshExpandableListView mExpandList;
    private List<String> stringList = new ArrayList<>();
    private List<Bean.DataBean> dataBeen = new ArrayList<Bean.DataBean>();


    private TextView count;
    private
    Myadapter myadapter;

    private TextView count_price;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mExpandList = (PullToRefreshExpandableListView) findViewById(R.id.expand_list);
        count = (TextView) findViewById(R.id.count);
        count_price = (TextView) findViewById(R.id.count_price);
        Button huishou = (Button) findViewById(R.id.huishou);
        //可以刷新加载
        mExpandList.setMode(PullToRefreshBase.Mode.BOTH);
        mExpandList.setOnRefreshListener(this);
        //初始化数据
        getdate();
        //回收按钮监听
        huishou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                List<Bean.DataBean.DatasBean> datasList = new ArrayList<Bean.DataBean.DatasBean>();
                List<Bean.DataBean> dataBeanList = new ArrayList<Bean.DataBean>();
                //遍历集合所有数据
                for (int i = 0; i < dataBeen.size(); i++) {
                    //判断一级复选框是否选中
                    if (dataBeen.get(i).isGroupCheck()) {
                        //选中则存进集合
                        dataBeanList.add(dataBeen.get(i));
                    } else {
                        //如果一级复选框不是选中状态则遍历每个一级列表中的二级条目
                        List<Bean.DataBean.DatasBean> datas = dataBeen.get(i).getDatas();
                        for (int j = 0; j < datas.size(); j++) {
                            //判断每个二级条目是否选中
                            if (datas.get(j).isChildcheckBox()) {
                                //选中则添加进集合
                                datasList.add(dataBeen.get(i).getDatas().get(j));
                            }
                            //删除二级条目集合
                            dataBeen.get(i).getDatas().removeAll(datasList);
                        }
                    }
                }
                //删除数据
                dataBeen.removeAll(dataBeanList);
                //刷新适配器
                myadapter.notifyDataSetChanged();
                //计算价格
                setprice();
            }
        });
    }
    //请求数据
    public void getdate() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, "http://mock.eoapi.cn/success/9ghtceuvzUWfvffRK6pts8zaEjGYWrkK", new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                Bean bean = new Gson().fromJson(result, Bean.class);
                dataBeen.addAll(bean.getData());
                //Toast.makeText(MainActivity.this,"数据请求成功"+dataBeen.size(),Toast.LENGTH_SHORT).show();
                myadapter = new Myadapter(MainActivity.this, dataBeen);
                myadapter.setCheckInterface(MainActivity.this);
                //setListAdapter(myadapter);
                MainActivity.this.setListAdapter(myadapter);
            }
            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(MainActivity.this, "数据请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        //getdate();
        /*if(myadapter!=null){
            Toast.makeText(this,"下拉刷新数据",Toast.LENGTH_SHORT).show();
            myadapter.notifyDataSetChanged();
            mExpandList.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mExpandList.onRefreshComplete();
                }
            }, 1000);
        }*/
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        //getdate();
        /*if(myadapter!=null){
            Toast.makeText(this,"上拉加载数据",Toast.LENGTH_SHORT).show();
            myadapter.notifyDataSetChanged();
            mExpandList.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mExpandList.onRefreshComplete();
                }
            }, 1000);
        }*/
    }

    //一级条目复选框监听
    @Override
    public void groupCheck(int groupPosition, boolean isChecked) {
        //判断是否选中
        if (isChecked) {
            //则该一级条目中所有子条目选中
            List<Bean.DataBean.DatasBean> datas = dataBeen.get(groupPosition).getDatas();
            for (int i = 0; i < datas.size(); i++) {
                datas.get(i).setChildcheckBox(isChecked);
            }
        } else {
            //否则全不选中
            List<Bean.DataBean.DatasBean> datas = dataBeen.get(groupPosition).getDatas();
            for (int i = 0; i < datas.size(); i++) {
                datas.get(i).setChildcheckBox(isChecked);
            }
        }
            //刷新,计价
            myadapter.notifyDataSetChanged();
            setprice();
    }
    //二级条目复选框监听
    @Override
    public void childCheck(int groupPosition, int childPosition, boolean isChecked) {
        //获取该一级条目中子元素的集合
        List<Bean.DataBean.DatasBean> datas = dataBeen.get(groupPosition).getDatas();
        //判断该条目是否选中
        if (isChecked) {
            dataBeen.get(groupPosition).getDatas().get(childPosition).setChildcheckBox(isChecked);
        } else {
            dataBeen.get(groupPosition).getDatas().get(childPosition).setChildcheckBox(isChecked);
        }
        //如果该组元素下所有子元素全部选中则组元素选中
        int flag = 0;
        for (int i = 0; i < datas.size(); i++) {
            if (datas.get(i).isChildcheckBox()) {
                flag++;
            } else {
                flag--;
            }
        }
        if (flag == datas.size()) {
            dataBeen.get(groupPosition).setGroupCheck(true);
        } else {
            dataBeen.get(groupPosition).setGroupCheck(false);
        }
        myadapter.notifyDataSetChanged();
        setprice();
    }
    private int num = 0;
    private int price = 0;
    //计价
    public void setprice() {
        //每次重新计价
        num = 0;
        price = 0;
        for (int i = 0; i < dataBeen.size(); i++) {
            List<Bean.DataBean.DatasBean> datas = dataBeen.get(i).getDatas();
            for (int j = 0; j < datas.size(); j++) {
                Bean.DataBean.DatasBean datasBean = datas.get(j);
                if (datasBean.childcheckBox) {
                    num++;
                    price += datasBean.getPrice();
                }
            }
        }
        count.setText(num + "台旧机");
        count_price.setText("合计:¥" + price);
    }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*适配器*/
package com.bwie.text_a;

import android.content.Context;


import android.util.Log;
import android.view.View;


import android.view.ViewGroup;
import android.widget.BaseAdapter;



import android.widget.BaseExpandableListAdapter;

import android.widget.CheckBox;

import android.widget.CompoundButton;
import android.widget.TextView;


import android.widget.Toast;


import com.bwie.bean.Bean;

import java.util.List;

public
class
Myadapter extends BaseExpandableListAdapter {


    private Context context;
    private List<Bean.DataBean> dataBeen;
    private CheckInterface checkInterface;

    public Myadapter(Context context, List<Bean.DataBean> dataBeen) {
        this.context = context;
        this.dataBeen = dataBeen;
    }

    public void setCheckInterface(CheckInterface checkInterface) {
        this.checkInterface = checkInterface;

    }

    @Override
    public int getGroupCount() {
        return dataBeen.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return dataBeen.get(groupPosition).getDatas().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return dataBeen.get(groupPosition).getTitle();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return dataBeen.get(groupPosition).getDatas().get(childPosition).getType_name();
    }

    @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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        //GroupViewHolder groupViewHolder=null;
        //if(convertView==null){
         //   groupViewHolder=new GroupViewHolder();
            convertView=View.inflate(context,R.layout.group_item,null);
            TextView group_title= (TextView) convertView.findViewById(R.id.group_title);
            TextView group_title_id= (TextView) convertView.findViewById(R.id.group_title_id);
            CheckBox group_check= (CheckBox) convertView.findViewById(R.id.group_check);
       //     convertView.setTag(groupViewHolder);
       // }else {
       // groupViewHolder= (GroupViewHolder) convertView.getTag();
       // }
        group_title.setText(dataBeen.get(groupPosition).getTitle());
        group_title_id.setText(dataBeen.get(groupPosition).getTitle_id());
        group_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(checkInterface!=null){
                    dataBeen.get(groupPosition).setGroupCheck(isChecked);
                checkInterface.groupCheck(groupPosition,isChecked);
                }
            }
        });
        group_check.setChecked(dataBeen.get(groupPosition).isGroupCheck());
        return convertView;
    }

    class GroupViewHolder{

        TextView group_title,group_title_id;
        CheckBox group_check;

    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

       // ChildViewHolder childViewHolder=null;
       // if(convertView==null){
        //    childViewHolder=new ChildViewHolder();
            convertView=View.inflate(context,R.layout.child_item,null);
        CheckBox child_check= (CheckBox) convertView.findViewById(R.id.child_check);
        TextView miaoshu= (TextView) convertView.findViewById(R.id.miaoshu);
        TextView time= (TextView) convertView.findViewById(R.id.time);
        TextView child_title= (TextView) convertView.findViewById(R.id.child_title);
        TextView price= (TextView) convertView.findViewById(R.id.price);
      //
      //
        //convertView.setTag(childViewHolder);
       // }else {

        //   childViewHolder= (ChildViewHolder) convertView.getTag();
       // }
        child_title.setText(dataBeen.get(groupPosition).getDatas().get(childPosition).getType_name());
        miaoshu.setText(dataBeen.get(groupPosition).getDatas().get(childPosition).getMsg());
        time.setText(dataBeen.get(groupPosition).getDatas().get(childPosition).getAdd_time());
        price.setText("¥"+dataBeen.get(groupPosition).getDatas().get(childPosition).getPrice());
        child_check.setChecked(dataBeen.get(groupPosition).getDatas().get(childPosition).isChildcheckBox());
        child_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(checkInterface!=null){

                checkInterface.childCheck(groupPosition,childPosition,isChecked);
                }
            }
        });
        return convertView;
    }

    class ChildViewHolder{

        TextView child_title,miaoshu,time,price;
        CheckBox child_check;

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    //复选框接口
    public interface CheckInterface{

         void groupCheck(int groupPosition, boolean isChecked);
         void childCheck(int groupPosition, int childPosition, boolean isChecked);

    }

}



网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...