首页 > 学院 > 开发设计 > 正文

ExpandableList默认显示三条数据,点击更多显示全部(带上拉下拉)

2019-11-09 18:45:56
字体:
来源:转载
供稿:网友

因为项目需求,所以要做此功能,我的好朋友肖凯直接扔给我一个demo,代码很简单,初学者也可以看懂,demo下载改成自己需要的界面即可

一、首先展示一下预览图

这里写图片描述

点击更多后

这里写图片描述

二、首先展示一下项目结构(开发工具Android studio)

这里写图片描述

三、部分代码(详细请下载demo,本人所有上传资源全部0分)

mainActivitypackage com.szabh.boge;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.ExpandableListView;import com.szabh.boge.listview_pulltorefresh.PullToRefreshBase;import com.szabh.boge.listview_pulltorefresh.PullToRefreshListView;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;public class MainActivity extends Activity implements Handler.Callback,PullToRefreshBase.OnRefreshListener{ PRivate ArrayList<Company> mCompanies = new ArrayList<>(); private ExpandableListView Ex_listview; private MyAdapter adapter; private PullToRefreshListView mPullListView; private Context mContext; private boolean isPullDown = true;// 标记上拉或下拉 private int mQueryPage=0;//每次请求的页码 private int mPorCount=20;//每次返回的条数 private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext =this; initData(); initView(); } private void initView() { Ex_listview = (ExpandableListView) findViewById(R.id.lv); mPullListView = (PullToRefreshListView) findViewById(R.id.mPullListView_id); Ex_listview = mPullListView.getRefreshableView(); mPullListView.setOnRefreshListener(this); adapter = new MyAdapter(MainActivity.this,mCompanies); Ex_listview.setAdapter(adapter); for (int i = 0, l = mCompanies.size(); i < l; i++) { Ex_listview.expandGroup(i); } Ex_listview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { return true; } }); } private void initData() { for (int i = 1; i < 10; i++) { Company c = new Company(); c.name = "我的主布局数据" + i; ArrayList<String> employees = new ArrayList<>(); for (int j = 1; j < i + 1; j++) { employees.add("子的" + j); } c.employees = employees; mCompanies.add(c); } } private void setLastUpdateTime() { String text = formatDateTime(System.currentTimeMillis()); mPullListView.setLastUpdatedLabel(text); } private String formatDateTime(long time) { if (0 == time) { return ""; } return mDateFormat.format(new Date(time)); } @Override public void onPullDownToRefresh(PullToRefreshBase refreshView) { isPullDown=true; mQueryPage=0;// 加载网络数据 } @Override public void onPullUpToRefresh(PullToRefreshBase refreshView) { isPullDown=false; mQueryPage = mQueryPage+5 ;// 加载网络数据 } @Override public boolean handleMessage(Message message) { return false; }}MyAdapter适配器package com.szabh.boge;import android.app.Activity;import android.content.Context;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.TextView;import java.util.ArrayList;import java.util.List;/** * Created by XianBo.Geng on 2017/1/12. */public class MyAdapter extends BaseExpandableListAdapter{ private Context context; private List<Company> mdata; private LayoutInflater inflater; public MyAdapter(Activity Activity, ArrayList<Company> mCompanies) { context =Activity; mdata = mCompanies; inflater =LayoutInflater.from(context); } @Override public int getGroupCount() { return mdata.size(); } @Override public int getChildrenCount(int groupPosition) { int size = mdata.get(groupPosition).employees.size(); if (size < 4) { return size; } else { if (mdata.get(groupPosition).isMore) { return size + 1; } else { return 4; } } } @Override public Object getGroup(int groupPosition) { return null; } @Override public Object getChild(int groupPosition, int childPosition) { return null; } @Override public long getGroupId(int groupPosition) { return 0; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {// LayoutInflater inflater = LayouInflater.from(parent.getContext()); View v = inflater.inflate(R.layout.item_company, null); TextView tv = (TextView) v.findViewById(R.id.tv); tv.setText(mdata.get(groupPosition).name); TextView tvs = (TextView) v.findViewById(R.id.group_service_name); tvs.setText(mdata.get(groupPosition).name+"啦啦啦啦"); return v; } @Override public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { int size = mdata.get(groupPosition).employees.size(); Log.i("Text","111111111111111111111111111"+size); if (size < 4) { View v = View.inflate(parent.getContext(), R.layout.item_employee, null); TextView tv = (TextView) v.findViewById(R.id.tv); tv.setText(mdata.get(groupPosition).employees.get(childPosition)); return v; } else { if (mdata.get(groupPosition).isMore) { if (childPosition == mdata.get(groupPosition).employees.size()) { View v = View.inflate(parent.getContext(), R.layout.item_more, null); TextView tv = (TextView) v.findViewById(R.id.tv); tv.setText("点击收起"); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mdata.get(groupPosition).isMore = false; notifyDataSetChanged(); } }); return tv; } else { View v = View.inflate(parent.getContext(), R.layout.item_employee, null); TextView tv = (TextView) v.findViewById(R.id.tv); tv.setText(mdata.get(groupPosition).employees.get(childPosition)); return v; } } else { if (childPosition == 3) { View v = View.inflate(parent.getContext(), R.layout.item_more, null); TextView tv = (TextView) v.findViewById(R.id.tv); tv.setText("点击查看更多"); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mdata.get(groupPosition).isMore = true; notifyDataSetChanged(); } }); return v; } else { View v = View.inflate(parent.getContext(), R.layout.item_employee, null); TextView tv = (TextView) v.findViewById(R.id.tv); tv.setText(mdata.get(groupPosition).employees.get(childPosition)); return v; } } } } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; }}实体类package com.szabh.boge;import java.util.ArrayList;public class Company { public String name; public ArrayList<String> employees; public boolean isMore;}

四、demo地址http://download.csdn.net/detail/bobo89455100/9747002


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表