布局文件代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".AllsectionActivity" ><ExpandableListViewandroid:id="@+id/expandableListView1"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:layout_alignParentTop="true" ></ExpandableListView></RelativeLayout>

图中的父节点和子节点的数据都是来自服务器的JSON文件。

下载json文件并解析。

设置适配器,选择BaseExpandableListAdapter(),重写几个方法,关键是在 getGroupView(),getChildView()得到父节点、子节点布局的对象,并为他们设置数据上去。

以下是详细代码:

public class AllsectionActivity extends Activity
{//初始化父节点和子节点类型Object[][] mChilds = null; //子节点为二维数组String[] mGroups = null;private ExpandableListView listView;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_allbankuai);AllformsAsyncTask task = new AllformsAsyncTask();task.execute(Contents.URL_ALLFORMS);listView = (ExpandableListView) findViewById(R.id.expandableListView1);//ExpandableListView继承listviewlistView.setOnChildClickListener(new OnChildClickListener(){//为子节点设置点击事件@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id){startActivity(new Intent(AllsectionActivity.this,BankuaitieziliebiaoActivity.class ));return false;//跳转到版块帖子列表}});}class AllformsAsyncTask extends AsyncTask<String, Void, String>{@Overrideprotected String doInString...  params){String json = HTTPutil.getJson(params[0]);//下载json文件,这里视图中的父节点和子节点的数据都是来自服务器给的JSON文件return json;}@Overrideprotected void onPostExecute(String result){if(result == null || "".equals(result)){return;}try{JSONArray groupArr = new JSONArray(result);mGroups = new String[groupArr.length()];//新建数组,静态创建,需要定义父节点数组长度mChilds =  new Object[groupArr.length()][];//新建二维数组,静态创建,需要定义子节点的数组的一维长度(子节点的长度需要定义两次)for (int i = 0; i < groupArr.length(); i++){JSONObject jsonObject = groupArr.getJSONObject(i);String group_name = jsonObject.getString("group_name");mGroups[i] = group_name;JSONArray childArr = jsonObject.getJSONArray("child");mChilds[i] =  new Object[childArr.length()];//定义每个子节点的数组的二维长度for (int j = 0; j < childArr.length(); j++){JSONObject childObject = childArr.getJSONObject(j);String title = childObject.getString("title");String today = childObject.getString("today");String post = childObject.getString("post");String all = childObject.getString("all");ChildItemdata childItemdata = new ChildItemdata(title, today, post, all);mChilds[i][j] = childItemdata;//子节点的单行数据}}}catch (JSONException e){// TODO Auto-generated catch blocke.printStackTrace();}setadapter(mGroups,mChilds);}}private void setadapter(final String[] mGroups, final Object[][] mChilds){listView.setAdapter(new BaseExpandableListAdapter(){//使用BaseExpandableListAdapter,重写几个方法@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition){//子节点是否可以点击return true;}@Overridepublic boolean hasStableIds(){// TODO Auto-generated method stubreturn false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent){//得到group的view对象TextView textview_group = (TextView) getLayoutInflater().inflate(R.layout.textview_group, null);textview_group.setText(mGroups[groupPosition]);return textview_group;}@Overridepublic long getGroupId(int groupPosition){// TODO Auto-generated method stubreturn 0;}@Overridepublic int getGroupCount(){return mGroups.length;}@Overridepublic Object getGroup(int groupPosition){// TODO Auto-generated method stubreturn null;}@Overridepublic int getChildrenCount(int groupPosition){//每个父节点下的子节点数量return mChilds[groupPosition].length;}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent){//得到child的view对象RelativeLayout child;ViewHolder holder;if(convertView == null){holder = new ViewHolder();child =  (RelativeLayout) getLayoutInflater().inflate(R.layout.listitem_allforms, null);holder.allforms_iv_shoucang = (ImageView) child.findViewById(R.id.allforms_iv_shoucang);holder.allforms_tv_title = (TextView) child.findViewById(R.id.allforms_tv_title);holder.allforms_tv_today = (TextView) child.findViewById(R.id.allforms_tv_today);holder.allforms_tv_post = (TextView) child.findViewById(R.id.allforms_tv_post);holder.allforms_tv_all = (TextView) child.findViewById(R.id.allforms_tv_all);child.setTag(holder);}else{child = (RelativeLayout) convertView;holder = (ViewHolder) child.getTag();}ChildItemdata childItemdata = (ChildItemdata) mChilds[groupPosition][childPosition];//得到子节点的单行数据holder.allforms_tv_title.setText(childItemdata.title);holder.allforms_tv_today.setText(childItemdata.today);holder.allforms_tv_post.setText(childItemdata.post);holder.allforms_tv_all.setText(childItemdata.all);return child;}@Overridepublic long getChildId(int groupPosition, int childPosition){// TODO Auto-generated method stubreturn 0;}@Overridepublic Object getChild(int groupPosition, int childPosition){// TODO Auto-generated method stubreturn null;}});}class ViewHolder{ImageView allforms_iv_shoucang;TextView allforms_tv_title;TextView allforms_tv_today;TextView allforms_tv_post;TextView allforms_tv_all;}class ChildItemdata{String title;String today;String post;String all;public ChildItemdata(String title, String today, String post, String all){super();this.title = title;this.today = today;this.post = post;this.all = all;}@Overridepublic String toString(){return "ChildItemdata [title=" + title + ", today=" + today+ ", post=" + post + ", all=" + all + "]";}}@Overridepublic boolean onCreateOptionsMenu(Menu menu){// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.allbankuai, menu);return true;}
}

转载于:https://blog.51cto.com/wangcuijing/1282322

android 中使用ExpandableListView控件结合服务器json文件的下载相关推荐

  1. Android中的基础控件TextView、Button、ImageView、EditText、ProgressBar

    文章目录 1 Android中的基础控件 1.1 控件的通用属性 2 TextView 2.1 TextView的继承关系 2.2 TextView的常用属性 3 EditText 3.1 常用属性 ...

  2. Android中进度条控件使用

    android中进度条控件使用 ProgressBar pb = findViewById(R.id.pb);pb.setMax(100);pb.setProgress(33); 转载于:https: ...

  3. 从零开始学android:Android中的基本控件(上)

    从零开始学android:Android中的基本控件(上) 本章内容较多,下面只贴代码,大家只需要贴到自己eclipse里就知道作用^^! View组件简介 Android中的View组件包含了几乎所 ...

  4. winform界面嵌入dwg图纸_WPF中使用WinForm控件预览DWG文件(学习笔记)

    操作环境:XP,C# ,.Net4.0和VS2010中测试 WinForm中使用DWGThumbnail不用这么麻烦,下面讨论的是在WPF中使用,WPF中无法直接引用DWGThumbnail.ocx来 ...

  5. android 中的组合控件的设计

    在开发应用程序的时候,很多时候会使用到几个重复的控件,例如Android手机的设置界面里面的位置服务里面的每一栏都是组合控件,也就是说多个控件组成一个整体,如下图所示: 红色方框里面的是由两个Text ...

  6. android 中使用TabHost控件实现微信界面的底部菜单效果

    首先,在布局文件中的代码如下:(菜单位于底部,需要在代码中设置) <TabHostandroid:id="@android:id/tabhost"android:layout ...

  7. Android中的基础控件CheckBox、RadioButton、ToggleButton、SeekBar

    文章目录 1 CheckBox 1.1 CheckBox介绍 2 RadioButton 2.1 RadioButton介绍 3 ToggleButton 3.1 ToggleButton介绍 4 S ...

  8. android中互斥的控件,Android控件之Radiobutton与RadioGroup

    RadioButton 是一个单选控件,在一个RadioGroup中,各个RadioButton是互斥的 XML文件: xmlns:tools="http://schemas.android ...

  9. Android中如何使控件保持固定宽高比

    目录 1.自定义view 2.adjustViewBounds 3.百分比布局 4.ConstraintLayout 我们在android开发过程中可能会遇到一种情况,一个组件需要保持固定的宽高比,但 ...

最新文章

  1. tomcat自动重启脚本
  2. ajax省市联动案例,AJAX案例四:省市联动(示例代码)
  3. 使用 monitor 命令查看 redis 请求日志
  4. 光纤收发器在使用过程中有哪些需要注意的事项?
  5. python numpy和pandas库的区别_python – 来自熊猫和numpy的意思不同
  6. istio入门与实战 pdf 下载_Istio入门与实战
  7. c+mysql主从切换_mysql主从配置
  8. Python数据结构中包含中文时在Windows下正常输出
  9. 苹果关掉200m限制_苹果手机200m限制取消教程 苹果怎么下载超过200m的软件
  10. ArcGIS制图之地形图制作
  11. word论文排版和写作02:插入算法的伪代码
  12. 请问肾阴虚吃什么药?饮食注意什么?还有桂附地黄丸是治肾阴虚还是治肾阳虚的?谢谢
  13. 计算机wps系统的造字程序在哪里,Winxp系统自带造字程序在哪|Winxp系统自带造字程序的使用方法...
  14. Linux的公平调度
  15. 许键树:华为云视频直播在各细分场景的体验指标优化实践
  16. 现在学 Prolog 合一和证明搜索
  17. php 抓取京东搜索页,京东商品列表页爬虫采集方法 - 八爪鱼采集器
  18. 巧用TWaver 3D 矢量图形功能
  19. 沙 棘 叶 中 多 糖 的 初 步 研 究
  20. 免费图标查询、下载的网站

热门文章

  1. 图解Oracle 12c 手动建库
  2. 分布式理论(一)CAP 理论
  3. 聊聊、Java SPI
  4. Python开发【第5节】【函数基础】
  5. [2-SAT]【学习笔记】【未完】
  6. Xcode7在运行包含HTTP协议的程序时报错解决方法 App Transport Security has blocked a cleartext HTTP (http)...
  7. 使用Windows Mobile Device Center进行手机的同步
  8. cad绘制椭圆的方法有几种_CAD新手入门教学:如何绘制矩形?
  9. 用神经网络迭代次数曲线模拟原子光谱
  10. 【Paper】2021_Robust Near-Optimal Coordination in Uncertain Multiagent Networks With Motion Const