volley+swiperefreshlayout实现checkbox全选
来源:互联网 发布:广州烘焙培训 知乎 编辑:程序博客网 时间:2024/06/15 14:43
1.依赖 jar包
compile project(':swiperefreshlayoutlibrary')
2.布局
activity_main
<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/select_choose" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="全选" android:textSize="20sp" /></LinearLayout>item.xml
<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/select_choose" /> <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>3.代码
MainActivity
import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Adapter;import android.widget.CheckBox;import android.widget.ListView;import com.android.volley.RequestQueue;import com.android.volley.Response;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 Handler handler = new Handler(); private JSONArray jsonArray; private MyAdapter adapter; @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.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark, android.R.color.holo_blue_bright); srl.setDirection(SwipyRefreshLayoutDirection.BOTH); srl.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() { @Override public void onRefresh(int index) { checkBox.setChecked(false); startNum = 0; getData(); handler.postDelayed(new Runnable() { @Override public void run() { srl.setRefreshing(false); } }, 2000); } @Override public void onLoad(int index) { checkBox.setChecked(false); startNum++; Log.i("aaa",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; //创建StringRequest StringRequest request = new StringRequest(url, new Response.Listener<String>() { //响应成功 @Override public void onResponse(String s) { Log.i("bbb",s); 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.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: boolean checked = ((CheckBox) v).isChecked(); if (checked) { adapter.notityCheck(checked); } else { adapter.notityCheck(checked); } break; } }}MyAdapter
import android.content.Context;import android.util.Log;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;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) { setCheck(false); 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) { convertView = View.inflate(context, R.layout.item, null); CheckBox item_checkbox = (CheckBox) convertView.findViewById(R.id.item_checkbox); TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title); ImageView iv = (ImageView) convertView.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 (Exception e) { e.printStackTrace(); } return convertView; }}
0 0
- volley+swiperefreshlayout实现checkbox全选
- volley网络请求框架+CheckBox实现全选及单选
- checkbox实现全选
- javascript 实现 checkbox 全选
- Jquery 实现全选checkbox
- CheckBOx实现全选
- js实现checkbox全选
- javascript实现checkbox全选
- checkbox实现全选功能
- js实现checkbox全选
- jquery实现checkbox全选
- CheckBox全选实现
- 实现 checkbox 全选,全不选
- CheckBox全选的实现
- checkbox全选,js实现
- JS实现checkbox全选
- jq 实现CheckBox全选
- jquery实现checkbox全选、取消全选
- MATLAB实现中值滤波算法
- CATransition
- Android导入项目时应该知道的知识
- Could not open lock file/var/lib/dpkg/lock的解决办法
- Java虚拟机基础知识
- volley+swiperefreshlayout实现checkbox全选
- Android 异步消息处理机制 Handler、Looper、Message、MessageQueue之藕断丝连
- 表单验证插件:jQuery Validation Engine
- nyoj 1277 Decimal integer conversion
- LeetCode- 树的前序、中序、后序遍历(递归与迭代)
- 线程池的使用
- Could not open lock file/var/lib/dpkg/lock的解决办法
- 数据库
- python 图形pygame模板的使用