简单购物车

来源:互联网 发布:有数据分析软件吗 编辑:程序博客网 时间:2024/04/25 06:54

首先导入能上拉刷新下拉加载的listview 在这里我们用的是swiperefreshlayoutlibrary

然后导入网络获取的方法  在这里我们使用的Volley

然后获取图片  我们使用的picasso-2.4.0

-获取控件

-写适配器

-然后写点击checbox的逻辑

-通过接口传递checbox全选


Activity

import android.os.Bundle;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.CheckBox;import android.widget.ListView;import com.android.volley.Response;import com.android.volley.RequestQueue;import com.android.volley.VolleyError;import com.android.volley.toolbox.StringRequest;import com.android.volley.toolbox.Volley;import com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout;import com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayoutDirection;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private SwipyRefreshLayout srl;    private ListView lv;    private CheckBox checkbox;    private int startNum = 0;    private JSONArray jsonArray;    private MyAdapter adapter;    private Handler handler = new Handler();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //找控件        srl = (SwipyRefreshLayout) findViewById(R.id.srl);        lv = (ListView) findViewById(R.id.lv);        checkbox = (CheckBox) findViewById(R.id.checkbox);        checkbox.setOnClickListener(this);        //设置刷新机制        srl.setColorSchemeColors(R.color.colorPrimaryDark, R.color.colorPrimary, R.color.colorAccent, android.R.color.holo_blue_light);        srl.setDirection(SwipyRefreshLayoutDirection.BOTH);        srl.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {            @Override            public void onRefresh(int index) {                startNum = 0;                getData();                handler.postDelayed(new Runnable() {                    @Override                    public void run() {                        srl.setRefreshing(false);                    }                }, 2000);            }            @Override            public void onLoad(int index) {                startNum++;                getData();                handler.postDelayed(new Runnable() {                    @Override                    public void run() {                        srl.setRefreshing(false);                    }                }, 2000);            }        });        getData();    }    //获取网络数据    private void getData() {        //获取请求队列        RequestQueue queue = Volley.newRequestQueue(this);        String url = "http://www.93.gov.cn/93app/data.do?" + "channelId=" + 0 + "&startNum=" + startNum;        Log.i("xxx", url.toString());        //创建StringRequest        StringRequest request = new StringRequest(url, new Response.Listener<String>() {            //响应成功            @Override            public void onResponse(String s) {                Log.i("xxx", s.toString());                try {                    JSONObject jsonObject = new JSONObject(s);                    JSONArray data = jsonObject.getJSONArray("data");                    if (startNum == 0) {                        jsonArray = new JSONArray();                    }                    for (int i = 0; i < data.length(); i++) {                        //把数据源添加到jsonArray                        jsonArray.put(data.get(i));                    }                    initListView(jsonArray);                } catch (JSONException e) {                    e.printStackTrace();                }            }        }, new Response.ErrorListener() {            //响应错误            @Override            public void onErrorResponse(VolleyError volleyError) {            }        });        //把请求方式添加到请求队列        queue.add(request);    }    //适配器    private void initListView(JSONArray jsonArray) {        //判断适配器是否为空        if (adapter == null) {            //创建适配器 传入上下文  集合  接口            adapter = new MyAdapter(this, jsonArray, new MyAdapter.OnCheckListener() {                @Override                public void onCheck(boolean check) {                    //全选选中状态                    checkbox.setChecked(check);                }            });            //调取适配器中刷新方法            adapter.setData(jsonArray);            //添加适配器            lv.setAdapter(adapter);        } else {            adapter.setData(jsonArray);        }    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.checkbox:                //获取checkbox点击事件                boolean checked = ((CheckBox) v).isChecked();                if (checked) {                    //全选                    adapter.notityCheck(checked);                } else {                    //取消全选                    adapter.notityCheck(checked);                }                break;        }    }}

视图

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/holo_red_light"    >    <com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout        android:id="@+id/srl"        android:layout_width="match_parent"        android:layout_height="match_parent"        >        <ListView            android:id="@+id/lv"            android:layout_width="match_parent"            android:layout_height="match_parent"></ListView>    </com.bawei.swiperefreshlayoutlibrary.SwipyRefreshLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="50dp"        android:layout_alignParentBottom="true"        android:background="@android:color/white"        android:gravity="center_vertical"        android:orientation="horizontal">        <CheckBox            android:id="@+id/checkbox"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="20dp"            android:button="@drawable/selector_checkbox"/>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:textSize="20sp"            android:text="全选"/>    </LinearLayout></RelativeLayout>

适配器

import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.TextView;import com.squareup.picasso.Picasso;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.util.HashMap;import java.util.Map;/** * 1. 类的用途 * 2. @author forever * 3. @date 2017/4/10 15:04 */public class MyAdapter extends BaseAdapter {    //上下文    private Context context;    //集合    private JSONArray jsonArray;    //存储条目id和状态    private Map<String, Boolean> map = new HashMap<String, Boolean>();    //声明接口    public OnCheckListener listener;    //定义接口    public interface OnCheckListener {        void onCheck(boolean check);    }    //保存条目id和状态    public void setCheck(boolean checkFlag) {        map.clear();        for (int i = 0; i < jsonArray.length(); i++) {            try {                String id = jsonArray.getJSONObject(i).getString("ID");                map.put(id, checkFlag);            } catch (JSONException e) {                e.printStackTrace();            }        }    }    //更新状态    public void notityCheck(boolean checkFlag) {        setCheck(checkFlag);        notifyDataSetChanged();    }    //构造器    public MyAdapter(Context context, JSONArray jsonArray, OnCheckListener listener) {        this.context = context;        this.jsonArray = jsonArray;        this.listener = listener;        //初始化状态        setCheck(false);    }    public void setData(JSONArray jsonArray) {        this.jsonArray = jsonArray;        notifyDataSetChanged();    }    @Override    public int getCount() {        return jsonArray != null ? jsonArray.length() : 0;    }    @Override    public Object getItem(int position) {        return position;    }    @Override    public long getItemId(int position) {        return 0;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        View view = View.inflate(context, R.layout.item, null);        CheckBox item_checkbox = (CheckBox) view.findViewById(R.id.item_checkbox);        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);        ImageView iv = (ImageView) view.findViewById(R.id.iv);        try {            JSONObject jsonObject = jsonArray.getJSONObject(position);            String title = jsonObject.getString("TITLE");            String imageurl = jsonObject.optString("IMAGEURL");            final String id = jsonObject.optString("ID");            tv_title.setText(title);            Picasso.with(context).load(imageurl).into(iv);            //获取checkbox选中状态            item_checkbox.setChecked(map.get(id));            item_checkbox.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    boolean checked = ((CheckBox) v).isChecked();                    map.put(id, checked);                    boolean isChecked = true;                    for (String key : map.keySet()) {                        Boolean value = map.get(key);                        if (!value) {                            isChecked = false;                            //取消全选                            listener.onCheck(isChecked);                            return;                        }                    }                    if (isChecked) {                        //全选                        listener.onCheck(isChecked);                    }                }            });        } catch (JSONException e) {            e.printStackTrace();        }        return view;    }}
适配器视图

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:gravity="center_vertical"              android:orientation="horizontal">    <CheckBox        android:id="@+id/item_checkbox"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:button="@drawable/selector_checkbox"/>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="20dp"        android:orientation="vertical">        <TextView            android:id="@+id/tv_title"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="rtyui"            android:textSize="20sp"/>        <ImageView            android:id="@+id/iv"            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@mipmap/ic_launcher"/>    </LinearLayout></LinearLayout>
图片
android:button="@drawable/selector_checkbox"

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@mipmap/add_activite" android:state_pressed="true"></item><item android:drawable="@mipmap/add_activite" android:state_checked="true"></item>    <item android:drawable="@mipmap/no_add_activite"></item></selector>












0 0