扣丁学堂——ExpandableListView随堂代码分享【源码下载】 (如链接消失请直接给我留言)
ExpandableListView代码演示效果截图
显然,这里显示的效果是类QQ好友的展开效果,二级点击这里测试使用的是单击事件,代码很简单,也基本能体现ExpendableListView功能。
与ListView的不同在于这里需要添加Group和Child的分组,也就是一级和二级的分组。也就是说代码中会有方法加载这两个分组。
直接上代码了。
activity_main.xml这里很简单就添加了一个ExpandableListView·代码略,不会可以看源码
group_layout.xml和child_layout.xml.也很简单只需要添加一个ImageView·和TextView就行了,代码略
MainActivity.java
package com.jin.administrator.myapplication1123; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ExpandableListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ExpandableListView)findViewById(R.id.expandableListView); listView.setAdapter(new MyExpandableAdapter()); //添加单击事件 listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener(){ @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity.this,childs[groupPosition][childPosition],Toast.LENGTH_SHORT).show(); return true; } }); } private String[] groups={"Android组","IOS组"}; private String[][] childs={{"宇哥","喻哥","镜堂","我"},{"坚哥","远祥"}}; //适配器 class MyExpandableAdapter extends BaseExpandableListAdapter{ @Override public int getGroupCount() { return groups.length; } @Override public int getChildrenCount(int groupPosition) { return childs[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return childs[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return childs[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } //下面这两个加载分组内部布局 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if(convertView==null){ convertView=getLayoutInflater().inflate(R.layout.group_layout,null); } ImageView icon=(ImageView)convertView.findViewById((R.id.icon)); TextView title=(TextView)convertView.findViewById(R.id.title); //icon.setImageResource(); title.setText(groups[groupPosition]); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if(convertView==null){ convertView=getLayoutInflater().inflate(R.layout.child_layout,null); } ImageView icon=(ImageView)convertView.findViewById((R.id.icon)); TextView title=(TextView)convertView.findViewById(R.id.title); //icon.setImageResource(); title.setText(childs[groupPosition][childPosition]); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } }这里代码较为简单也就是实现几个方法,技术含量也不是很高,基本就是规范化。注释也相对稍微少一些。
希望对初学者有所帮助,后期会分享类QQ的小项目,敬请期待