Android ExpandableListView实现仿好友列表
来源:互联网 发布:生命世界网络构图 编辑:程序博客网 时间:2024/05/16 23:35
效果图:
实现这一效果我们需要用到的类是ExpandableListView
xml文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ExpandableListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/elv_score_view" ></ExpandableListView> </LinearLayout></LinearLayout>
Activity文件:
package com.example.a;import android.app.ExpandableListActivity;import android.content.Context;import android.database.Cursor;import android.database.DataSetObserver;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.AdapterView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.SimpleExpandableListAdapter;import android.widget.TextView;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Created by Administrator on 2017/6/18 0018. */public class ScoreFragment extends Fragment { private ExpandableListView elv_score_view; private SQLiteDatabase sqLiteDatabase; private List<String> Group; private List<List<String>> Child; private List<String> ChildFirst; private List<String> ChildSecond; private List<String> ChildThird; private List<String> ChildFour; private List<String> ChildFive; private List<String> ChildSix; private List<List<Integer>> ChildPicture; private Cursor cursor_sztl,cursor_sztl_true,cursor_sztl_false,cursor_njjzw,cursor_njjzw_true,cursor_njjzw_false, cursor_wxcs,cursor_wxcs_true,cursor_wxcs_false,cursor_dlzjwd,cursor_dlzjwd_true, cursor_dlzjwd_false,cursor_bkzs,cursor_bkzs_true,cursor_bkzs_false,cursor_shcs, cursor_shcs_true,cursor_shcs_false; public ScoreFragment(SQLiteDatabase sqLiteDatabase){ this.sqLiteDatabase=sqLiteDatabase; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_score,null); cursor_wxcs = sqLiteDatabase.rawQuery("select * from topic where classify=?",new String[]{"文学常识"}); cursor_wxcs_true=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"文学常识",1+""}); cursor_wxcs_false=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"文学常识",2+""}); cursor_sztl = sqLiteDatabase.rawQuery("select * from topic where classify=?",new String[]{"数字推理"}); cursor_sztl_true=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"数字推理",1+""}); cursor_sztl_false=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"数字推理",2+""}); cursor_njjzw = sqLiteDatabase.rawQuery("select * from topic where classify=?",new String[]{"脑经急转弯"}); cursor_njjzw_true=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"脑经急转弯",1+""}); cursor_njjzw_false=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"脑经急转弯",2+""}); cursor_dlzjwd = sqLiteDatabase.rawQuery("select * from topic where classify=?",new String[]{"地理知识问答"}); cursor_dlzjwd_true=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"地理知识问答",1+""}); cursor_dlzjwd_false=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"地理知识问答",2+""}); cursor_bkzs = sqLiteDatabase.rawQuery("select * from topic where classify=?",new String[]{"百科知识"}); cursor_bkzs_true=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"百科知识",1+""}); cursor_bkzs_false=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"百科知识",2+""}); cursor_shcs = sqLiteDatabase.rawQuery("select * from topic where classify=?",new String[]{"生活常识"}); cursor_shcs_true=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"生活常识",1+""}); cursor_shcs_false=sqLiteDatabase.rawQuery("select * from topic where classify=? and tisboolean=?",new String[]{"生活常识",2+""}); Group = new ArrayList<String>(); Group.add("文学常识"); Group.add("数字推理"); Group.add("脑经急转弯"); Group.add("地理知识问答"); Group.add("百科知识"); Group.add("生活常识"); ChildThird= new ArrayList<String>(); ChildThird.add("正确题数:"+cursor_wxcs_true.getCount()); ChildThird.add("错误题数:"+cursor_wxcs_false.getCount()); ChildThird.add("未完成题数:"+(cursor_wxcs.getCount()-cursor_wxcs_false.getCount()-cursor_wxcs_true.getCount())); ChildFirst = new ArrayList<String>(); ChildFirst.add("正确题数:"+cursor_sztl_true.getCount()); ChildFirst.add("错误题数:"+cursor_sztl_false.getCount()); ChildFirst.add("未完成题数:"+(cursor_sztl.getCount()-cursor_sztl_false.getCount()-cursor_sztl_true.getCount())); ChildSecond= new ArrayList<String>(); ChildSecond.add("正确题数:"+cursor_njjzw_true.getCount()); ChildSecond.add("错误题数:"+cursor_njjzw_false.getCount()); ChildSecond.add("未完成题数:"+(cursor_njjzw.getCount()-cursor_njjzw_false.getCount()-cursor_njjzw_true.getCount())); ChildFour= new ArrayList<String>(); ChildFour.add("正确题数:"+cursor_dlzjwd_true.getCount()); ChildFour.add("错误题数:"+cursor_dlzjwd_false.getCount()); ChildFour.add("未完成题数:"+(cursor_dlzjwd.getCount()-cursor_dlzjwd_false.getCount()-cursor_dlzjwd_true.getCount())); ChildFive= new ArrayList<String>(); ChildFive.add("正确题数:"+cursor_bkzs_true.getCount()); ChildFive.add("错误题数:"+cursor_bkzs_false.getCount()); ChildFive.add("未完成题数:"+(cursor_bkzs.getCount()-cursor_bkzs_false.getCount()-cursor_bkzs_true.getCount())); ChildSix= new ArrayList<String>(); ChildSix.add("正确题数:"+cursor_shcs_true.getCount()); ChildSix.add("错误题数:"+cursor_shcs_false.getCount()); ChildSix.add("未完成题数:"+(cursor_shcs.getCount()-cursor_shcs_false.getCount()-cursor_shcs_true.getCount())); Child = new ArrayList<List<String>>(); Child.add(ChildThird); Child.add(ChildFirst); Child.add(ChildSecond); Child.add(ChildFour); Child.add(ChildFive); Child.add(ChildSix); List<Integer> tmp_list = new ArrayList<Integer>(); tmp_list.add(R.drawable.p_true); tmp_list.add(R.drawable.p_false); tmp_list.add(R.drawable.bz); ChildPicture = new ArrayList<List<Integer>>(); ChildPicture.add(tmp_list); ChildPicture.add(tmp_list); ChildPicture.add(tmp_list); ChildPicture.add(tmp_list); ChildPicture.add(tmp_list); ChildPicture.add(tmp_list); elv_score_view = (ExpandableListView) view.findViewById(R.id.elv_score_view); elv_score_view.setAdapter(new MyExpandableListViewAdapter(getActivity())); elv_score_view.setGroupIndicator(null); return view; } class MyExpandableListViewAdapter extends BaseExpandableListAdapter{ private Context context; public MyExpandableListViewAdapter(Context context) { this.context = context; } @Override public int getGroupCount() { return Group.size(); } @Override public int getChildrenCount(int groupPosition) { return Child.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return Group.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return Child.get(groupPosition).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 true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupHolder groupHolder=null; if (convertView==null){ convertView=getActivity().getLayoutInflater().from(context).inflate(R.layout.group,null); groupHolder = new GroupHolder(); groupHolder.txt= (TextView) convertView.findViewById(R.id.notice_item_date); convertView.setTag(groupHolder); }else{ groupHolder= (GroupHolder) convertView.getTag(); } groupHolder.txt.setText(Group.get(groupPosition)); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ItemHolder itemHolder = null; if (convertView == null) { convertView = (View) getActivity().getLayoutInflater().from(context).inflate( R.layout.child, null); itemHolder = new ItemHolder(); itemHolder.txt = (TextView) convertView.findViewById(R.id.group); itemHolder.img = (ImageView) convertView.findViewById(R.id.iv); convertView.setTag(itemHolder); } else { itemHolder = (ItemHolder) convertView.getTag(); } itemHolder.txt.setText(Child.get(groupPosition).get( childPosition)); itemHolder.img.setBackgroundResource(ChildPicture.get(groupPosition).get( childPosition)); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } class GroupHolder { public TextView txt; public ImageView img; } class ItemHolder { public ImageView img; public TextView txt; }}
只需要写好上述代码就可以实现图中的效果了。
阅读全文
0 0
- Android ExpandableListView实现仿好友列表
- ExpandableListView实现仿QQ好友列表
- ExpandableListView仿QQ好友列表
- ExpandableListView仿QQ好友列表
- 仿QQ好友列表 ExpandableListView
- Android ExpandableListView实现QQ好友列表效果
- 在Fragment里面使用ExpandableListView实现仿qq好友列表
- Android 仿QQ好友分组列表、ExpandableListView的使用详解
- 完全仿QQ好友列表,自定义ExpandableListView!
- 完全仿QQ好友列表,自定义ExpandableListView!
- 完全仿QQ好友列表,自定义ExpandableListView!
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- Android初级教程_用ExpandableListView实现类似QQ好友列表
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- 65 Android ExpandableListView (QQ好友列表的实现)
- Android初级教程_用ExpandableListView实现类似QQ好友列表
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- android 实现QQ好友列表(扩展listview:ExpandableListView)
- 常用的Hql语句
- Centos7 关闭防火墙
- 水题 第一站 关于521
- 牛客网_华为机试_021_简单密码破解(字符映射)
- Android动画
- Android ExpandableListView实现仿好友列表
- STL的部分实现
- linux下shell script学习(一)
- 解决空指针异常的一些经验
- HDU 1024 (DP,有空在写一遍)
- java小爬虫智联保存到excel
- 前端富文本编辑器ckeditor的使用
- 列表及表单知识点
- css画圣诞树