Expandablelistview用后详解
2016-01-20 21:09
337 查看
一、需要实现的效果
PullToRefreshExpandableListView配合Expandablelistview使用,实现下拉刷新效果,实现折叠显示。
重要知识点
判断某一条是否展开
源码
PullToRefreshExpandableListView配合Expandablelistview使用,实现下拉刷新效果,实现折叠显示。
重要知识点
lvKecheng = (PullToRefreshExpandableListView) view.findViewById(R.id.lv_kecheng); lvKecheng.setMode(PullToRefreshBase.Mode.PULL_FROM_START);//向下拉刷新 lvKecheng.setOnRefreshListener(this); lv_kecheng = (ExpandableListView) lvKecheng.getRefreshableView(); lv_kecheng.setGroupIndicator(null);//去除ExpandableListView自带箭头 //lv_kecheng.setDivider(null);//去除ExpandableListView分割线 lv_kecheng.setSelector(android.R.color.transparent);//选中时的颜色(transparent-透明) lv_kecheng.setOnGroupClickListener(this);//父级监听 lv_kecheng.setOnChildClickListener(this);//子级监听 //点开正在点的分组关闭其他分组 lv_kecheng.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { for (int i = 0, count =lv_kecheng .getExpandableListAdapter().getGroupCount(); i < count; i++) { if (groupPosition != i) {// 关闭其他分组 lv_kecheng.collapseGroup(i); } } } });
@Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { //lv_kecheng.expandGroup(groupPosition);//展开 //lv_kecheng.collapseGroup(groupPosition);//关闭 return true;//false为展开--true为关闭 }
判断某一条是否展开
v_kecheng.isGroupExpanded(groupPosition)
源码
package com.dearedu.cloudroom.fragment; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import com.dearedu.cloudroom.R; import com.dearedu.cloudroom.base.BaseContentFragment; import com.dearedu.cloudroom.bean.KechengBean; import com.dearedu.cloudroom.ui.ContentActivity; import com.dearedu.cloudroom.ui.LoginActivity; import com.dearedu.cloudroom.ui.PlayActivity; import com.dearedu.cloudroom.utils.Constants; import com.dearedu.cloudroom.utils.L; import com.dearedu.cloudroom.utils.UIUtils; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import com.handmark.pulltorefresh.library.PullToRefreshBase; import com.handmark.pulltorefresh.library.PullToRefreshExpandableListView; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.ViewUtils; 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; /** * 课程-Fragment */ public class KeChengContentFragment extends BaseContentFragment implements RadioGroup.OnCheckedChangeListener, PullToRefreshBase.OnRefreshListener<ExpandableListView>, ExpandableListView.OnChildClickListener { private RadioButton kechengYuwen; private RadioButton kechengShuxue; private RadioButton kechengYingyu; private RadioButton kechengWuli; private RadioButton kechengHuaxue; private RadioButton kechengLishi; private RadioButton kechengDili; private RadioButton kechengShengwu; private RadioButton kechengZhengzhi; private RadioGroup kechengRg; private String xueke; private ContentActivity mActivity; private PullToRefreshExpandableListView lvKecheng; private ExpandableListView lv_kecheng; private List<KechengBean.Kecheng> dataList; private ExpandableListAdapter kcAdapter; private String state; private KechengBean bean; private String kechengUrl; List<KechengBean.Kecheng> parentList = new ArrayList<KechengBean.Kecheng>();//父级 List<List<KechengBean.Kecheng>> allChildList = new ArrayList<List<KechengBean.Kecheng>>();//子级 @Override public View initView() { System.out.println("mContext_content : " + mContext); View view = View.inflate(mContext, R.layout.fragment_kecheng_content, null); kechengRg = (RadioGroup) view.findViewById(R.id.rg_kecheng); kechengYuwen = (RadioButton) view.findViewById(R.id.rb_kecheng_yuwen); kechengShuxue = (RadioButton) view.findViewById(R.id.rb_kecheng_shuxue); kechengYingyu = (RadioButton) view.findViewById(R.id.rb_kecheng_yingyu); kechengWuli = (RadioButton) view.findViewById(R.id.rb_kecheng_wuli); kechengHuaxue = (RadioButton) view.findViewById(R.id.rb_kecheng_huaxue); kechengLishi = (RadioButton) view.findViewById(R.id.rb_kecheng_lishi); kechengDili = (RadioButton) view.findViewById(R.id.rb_kecheng_dili); kechengShengwu = (RadioButton) view.findViewById(R.id.rb_kecheng_shengwu); kechengZhengzhi = (RadioButton) view.findViewById(R.id.rb_kecheng_zhengzhi); lvKecheng = (PullToRefreshExpandableListView) view.findViewById(R.id.lv_kecheng); lvKecheng.setMode(PullToRefreshBase.Mode.PULL_FROM_START);//向下拉刷新 lvKecheng.setOnRefreshListener(this); lv_kecheng = (ExpandableListView) lvKecheng.getRefreshableView(); lv_kecheng.setGroupIndicator(null);//去除ExpandableListView自带箭头 //lv_kecheng.setDivider(null);//去除ExpandableListView分割线 lv_kecheng.setSelector(android.R.color.transparent);//选中时的颜色(transparent-透明) lv_kecheng.setOnChildClickListener(this);//子级监听 //点开正在点的分组关闭其他分组 lv_kecheng.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { for (int i = 0, count =lv_kecheng .getExpandableListAdapter().getGroupCount(); i < count; i++) { if (groupPosition != i) {// 关闭其他分组 lv_kecheng.collapseGroup(i); } } } }); ViewUtils.inject(this, view); return view; } @Override public void initData() { mActivity = (ContentActivity) mContext; xueke = ""; kechengRg.setOnCheckedChangeListener(this); SharedPreferences preferences = getActivity().getSharedPreferences( LoginActivity.USER_FILE_NAME, Context.MODE_PRIVATE); String mid = preferences.getString("mid", ""); //String kechengUrl = "http://client.app.dearedu.com/client.php?m=course&c=get_course_list&id=2268841"; //kechengUrl = "http://client.app.dearedu.com/client.php?m=course&c=get_course_list&id=" + mid; kechengUrl = Constants.Kc_Zx; L.s("-------kechengUrl-----------" + kechengUrl); getDataFromNet(kechengUrl); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.rb_kecheng_yuwen: xueke = "1"; break; case R.id.rb_kecheng_shuxue: xueke = "2"; break; case R.id.rb_kecheng_yingyu: xueke = "3"; break; case R.id.rb_kecheng_wuli: xueke = "4"; break; case R.id.rb_kecheng_huaxue: xueke = "5"; break; case R.id.rb_kecheng_shengwu: xueke = "6"; break; case R.id.rb_kecheng_dili: xueke = "7"; break; case R.id.rb_kecheng_lishi: xueke = "8"; break; case R.id.rb_kecheng_zhengzhi: xueke = "9"; break; } mActivity.getKechengXuekeFragment(xueke); if (kechengRg.getCheckedRadioButtonId() != -1) { kechengRg.clearCheck(); } } /** * 从网络获取数据 */ private void getDataFromNet(String url) { HttpUtils utils = new HttpUtils(); utils.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() { @Override public void onSuccess(ResponseInfo<String> responseInfo) { parseData(responseInfo.result); } @Override public void onFailure(HttpException e, String s) { Toast.makeText(getActivity(), "访问网络失败,请检查网络", Toast.LENGTH_SHORT).show(); } }); } /** * 解析课程接口数据 * * @param json */ public void parseData(String json) { try { Gson gson = new Gson(); bean = gson.fromJson(json, KechengBean.class); state = bean.status; String msg = bean.msg; } catch (JsonSyntaxException e) { e.printStackTrace(); } if (state.equals("400")) { UIUtils.runInMainThread(new Runnable() { @Override public void run() { Toast.makeText(mContext, "老师尚未推荐课程", Toast.LENGTH_LONG).show(); } }); } else if (state.equals("200")) { dataList = bean.data; if (kcAdapter == null) { kcAdapter = new ExpandableListAdapter(); lv_kecheng.setAdapter(kcAdapter); // for (int i = 0; i < parentList.size(); i++) { // if (parentList.get(i).subject_id.equals("5")) {// 传过来的值 // lv_kecheng.expandGroup(i); // } // } } else { kcAdapter.notifyDataSetChanged(); } } else { Toast.makeText(mContext, "老师尚未推荐课程", Toast.LENGTH_LONG).show(); } } @Override public void onRefresh(PullToRefreshBase<ExpandableListView> refreshView) { HttpUtils utils = new HttpUtils(); L.s("zztest", kechengUrl); utils.send(HttpRequest.HttpMethod.POST, kechengUrl, new RequestCallBack<String>() { @Override public void onSuccess(ResponseInfo<String> responseInfo) { try { Gson gson = new Gson(); KechengBean bean = gson.fromJson(responseInfo.result, KechengBean.class); String state = bean.status; String msg = bean.msg; if (state.equals("400")) { UIUtils.runInMainThread(new Runnable() { @Override public void run() { Toast.makeText(mContext, "请求异常,请稍后重试", Toast.LENGTH_SHORT).show(); } }); } else if (state.equals("200")) { //给list赋值 dataList = bean.data; kcAdapter.notifyDataSetChanged(); lvKecheng.onRefreshComplete(); } else { Toast.makeText(mContext, "请求异常,请稍后重试", Toast.LENGTH_SHORT).show(); } } catch (JsonSyntaxException e) { e.printStackTrace(); } } @Override public void onFailure(HttpException e, String s) { Toast.makeText(getActivity(), "访问网络失败,请检查网络", Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { String subjectId = allChildList.get(groupPosition).get(childPosition).subject_id; String courseId = allChildList.get(groupPosition).get(childPosition).course_id; Intent intent = new Intent(getActivity(), PlayActivity.class); Bundle bundle = new Bundle(); bundle.putString("id", courseId); bundle.putString("subject_id", subjectId); intent.putExtras(bundle); intent.setAction("kecheng"); startActivity(intent); return false;//false为展开--true为关闭 } //-------------------------------------------------------- public class ExpandableListAdapter extends BaseExpandableListAdapter { List<KechengBean.Kecheng> childList1 = new ArrayList<KechengBean.Kecheng>(); List<KechengBean.Kecheng> childList2 = new ArrayList<KechengBean.Kecheng>(); List<KechengBean.Kecheng> childList3 = new ArrayList<KechengBean.Kecheng>(); List<KechengBean.Kecheng> childList4 = new ArrayList<KechengBean.Kecheng>(); List<KechengBean.Kecheng> childList5 = new ArrayList<KechengBean.Kecheng>(); List<KechengBean.Kecheng> childList6 = new ArrayList<KechengBean.Kecheng>(); List<KechengBean.Kecheng> childList7 = new ArrayList<KechengBean.Kecheng>(); List<KechengBean.Kecheng> childList8 = new ArrayList<KechengBean.Kecheng>(); List<KechengBean.Kecheng> childList9 = new ArrayList<KechengBean.Kecheng>(); public ExpandableListAdapter() { for (int i = 0; i < dataList.size(); i++) { if (dataList.get(i).kc_level.equals("1")) { KechengBean.Kecheng parentData = new KechengBean().new Kecheng(); parentData = dataList.get(i); parentList.add(parentData); } if (!dataList.get(i).kc_level.equals("1")) { if (dataList.get(i).subject_id.equals("1")) { childList1.add(dataList.get(i)); } if (dataList.get(i).subject_id.equals("2")) { childList2.add(dataList.get(i)); } if (dataList.get(i).subject_id.equals("3")) { childList3.add(dataList.get(i)); } if (dataList.get(i).subject_id.equals("4")) { childList4.add(dataList.get(i)); } if (dataList.get(i).subject_id.equals("5")) { childList5.add(dataList.get(i)); } if (dataList.get(i).subject_id.equals("6")) { childList6.add(dataList.get(i)); } if (dataList.get(i).subject_id.equals("7")) { childList7.add(dataList.get(i)); } if (dataList.get(i).subject_id.equals("8")) { childList8.add(dataList.get(i)); } if (dataList.get(i).subject_id.equals("9")) { childList9.add(dataList.get(i)); } } } for (int i = 0; i < parentList.size(); i++) { if (parentList.get(i).subject_id.equals("1")) { allChildList.add(childList1); } if (parentList.get(i).subject_id.equals("2")) { allChildList.add(childList2); } if (parentList.get(i).subject_id.equals("3")) { allChildList.add(childList3); } if (parentList.get(i).subject_id.equals("4")) { allChildList.add(childList4); } if (parentList.get(i).subject_id.equals("5")) { allChildList.add(childList5); } if (parentList.get(i).subject_id.equals("6")) { allChildList.add(childList6); } if (parentList.get(i).subject_id.equals("7")) { allChildList.add(childList7); } if (parentList.get(i).subject_id.equals("8")) { allChildList.add(childList8); } if (parentList.get(i).subject_id.equals("9")) { allChildList.add(childList9); } } } @Override public int getGroupCount() { // TODO Auto-generated method stub return parentList.size(); } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return allChildList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return parentList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return allChildList.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, ViewGroup parent) { KechengViewHolder mHolder = null; if (convertView == null) { convertView = View.inflate(mContext, R.layout.fragment_kecheng_content_item_group, null); mHolder = new KechengViewHolder(); mHolder.lv_kc= (LinearLayout) convertView.findViewById(R.id.lv_kc); mHolder.tv_kecheng_item_chakan = (ImageView) convertView .findViewById(R.id.tv_kecheng_item_chakan); mHolder.tv_kecheng_item_chakan2 = (ImageView) convertView .findViewById(R.id.tv_kecheng_item_chakan2); mHolder.tv_kecheng_item_kemu = (TextView) convertView .findViewById(R.id.tv_kecheng_item_kemu); mHolder.tv_kecheng_item_kecheng = (TextView) convertView .findViewById(R.id.tv_kecheng_item_kecheng); mHolder.tv_kecheng_item_sucai = (TextView) convertView .findViewById(R.id.tv_kecheng_item_sucai); mHolder.tv_kecheng_item_weike = (TextView) convertView.findViewById(R.id.tv_kecheng_item_weike); mHolder.tv_kecheng_item_lianxi = (TextView) convertView.findViewById(R.id.tv_kecheng_item_lianxi); convertView.setTag(mHolder); } else { mHolder = (KechengViewHolder) convertView.getTag(); } //KechengBean.Kecheng dataItem = parentList.get(groupPosition); KechengBean.Kecheng dataItem = (KechengBean.Kecheng) getGroup(groupPosition); String subName = dataItem.sub_name; String title = dataItem.title; String suTotal = dataItem.su_total; String vTotal = dataItem.v_total; String testTotal = dataItem.test_total; mHolder.tv_kecheng_item_kemu.setText(subName); mHolder.tv_kecheng_item_kecheng.setText(title); mHolder.tv_kecheng_item_sucai.setText(suTotal); mHolder.tv_kecheng_item_weike.setText(vTotal); mHolder.tv_kecheng_item_lianxi.setText(testTotal); mHolder.tv_kecheng_item_chakan.setTag(groupPosition); if (isExpanded) { mHolder.tv_kecheng_item_chakan2.setImageDrawable(mContext.getResources().getDrawable(R.drawable.a)); } else { mHolder.tv_kecheng_item_chakan2.setImageDrawable(mContext.getResources().getDrawable(R.drawable.b)); } mHolder.lv_kc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String subjectId = parentList.get(groupPosition).subject_id; String courseId = parentList.get(groupPosition).course_id; Intent intent = new Intent(getActivity(), PlayActivity.class); Bundle bundle = new Bundle(); bundle.putString("id", courseId); bundle.putString("subject_id", subjectId); intent.putExtras(bundle); intent.setAction("kecheng"); startActivity(intent); } }); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { KechengViewHolder mHolder = null; if (convertView == null) { convertView = View.inflate(mContext, R.layout.fragment_kecheng_content_item_view, null); mHolder = new KechengViewHolder(); mHolder.tv_kecheng_item_chakan = (ImageView) convertView .findViewById(R.id.tv_kecheng_item_chakan); mHolder.tv_kecheng_item_kemu = (TextView) convertView .findViewById(R.id.tv_kecheng_item_kemu); mHolder.tv_kecheng_item_kecheng = (TextView) convertView .findViewById(R.id.tv_kecheng_item_kecheng); mHolder.tv_kecheng_item_sucai = (TextView) convertView .findViewById(R.id.tv_kecheng_item_sucai); mHolder.tv_kecheng_item_weike = (TextView) convertView.findViewById(R.id.tv_kecheng_item_weike); mHolder.tv_kecheng_item_lianxi = (TextView) convertView.findViewById(R.id.tv_kecheng_item_lianxi); convertView.setTag(mHolder); } else { mHolder = (KechengViewHolder) convertView.getTag(); } //KechengBean.Kecheng dataItem = allChildList.get(groupPosition).get(childPosition); KechengBean.Kecheng dataItem = (KechengBean.Kecheng) getChild(groupPosition, childPosition); String subName = dataItem.sub_name; String title = dataItem.title; String suTotal = dataItem.su_total; String vTotal = dataItem.v_total; String testTotal = dataItem.test_total; mHolder.tv_kecheng_item_kemu.setText(subName); mHolder.tv_kecheng_item_kecheng.setText(title); mHolder.tv_kecheng_item_sucai.setText(suTotal); mHolder.tv_kecheng_item_weike.setText(vTotal); mHolder.tv_kecheng_item_lianxi.setText(testTotal); mHolder.tv_kecheng_item_chakan.setTag(childPosition); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } } //======================================================== class KechengViewHolder { public TextView tv_kecheng_item_kemu; public TextView tv_kecheng_item_kecheng; public TextView tv_kecheng_item_sucai; public TextView tv_kecheng_item_weike; public TextView tv_kecheng_item_lianxi; public ImageView tv_kecheng_item_chakan; public ImageView tv_kecheng_item_chakan2; public LinearLayout lv_kc; } }
相关文章推荐
- 移动 摇杆的创建
- ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(二) 实现聊天室连接
- 1、什么是Lucene,Lucene能干什么
- poj3087
- Gradle 基本用法
- BOF——Bag-of-Featrures
- HDU 1565:最大点权独立集(网络流)
- android:scaleType属性
- BP神经网络算法
- 使用谷歌Volley和gson序列化,反序列
- 蓝桥杯:入门训练 Fibonacci数列
- 解决Ora-28056问题
- 【Android】intent action 跳转到系统使用,调用系统功能
- Picture(HDOJ-2052)
- 第一天
- 数据结构之递归算法
- HDU 2035 人见人爱A^B
- c语言成绩统计系统
- 【Jan.Challenge】总结
- Linux文件系统管理