十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本文实例为大家分享了ExpandableListView二级分栏效果的具体代码,供大家参考,具体内容如下
创新互联公司于2013年创立,是专业互联网技术服务公司,拥有项目网站制作、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元通许做网站,已为上家服务,为通许各地企业和个人服务,联系电话:18980820575
对ExpandableListView控件进行封装(未自定义)直接上代码:
通用ViewHolder类,仅在setImageResource中添加代码
package com.svp.haoyan.expandablelistview.tool; import android.content.Context; import android.graphics.Bitmap; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; /** * Created by haoyan on 2016/3/24. */ public class ViewHolder { //存储数据 private SparseArraymViews; private int mPosition; private View mConvertView; private boolean misExpanded; public ViewHolder(Context context, ViewGroup parent, int layoutId, int position,boolean isExpanded) { this.mPosition=position; this.mViews= new SparseArray (); this.misExpanded=isExpanded; //三 mConvertView= LayoutInflater.from(context).inflate(layoutId,parent,false); //一 mConvertView.setTag(this); } public static ViewHolder get(Context context,View convertView, ViewGroup parent,int layoutId,int position,boolean isExpanded){ if (convertView==null){ return new ViewHolder(context,parent,layoutId,position,isExpanded); }else { //五 六 ViewHolder holder=(ViewHolder)convertView.getTag(); holder.mPosition=position; return holder; } } //通过viewId获取控件 //泛型T返回子类 public T getView(int viewId){ View view =mViews.get(viewId); if (view==null){ //四 view=mConvertView.findViewById(viewId); mViews.put(viewId,view); } return (T)view; } //八 public View getmConvertView() { return mConvertView; } /** * 设置TextView的值 * @param viewId * @param text * @return */ //四 七 九 public ViewHolder setText(int viewId,String text){ TextView tv= getView(viewId); tv.setText(text); return this; } /** * 一参传id,二参传图片地址,三参为true则调用点击变化效果,默认为false. * */ public ViewHolder setImageResource(int viewId,int resId,boolean expanded){ ImageView view= getView(viewId); view.setImageResource(resId); if (expanded){ if (misExpanded){ view.setVisibility(View.GONE); }else { view.setVisibility(View.VISIBLE); } } return this; } public ViewHolder setImageBitamp(int viewId,Bitmap bitmap){ ImageView view= getView(viewId); view.setImageBitmap(bitmap); return this; } public ViewHolder setImageURI(int viewId,String uri){ ImageView view= getView(viewId); // Imageloader.getInstance().loadImg(view,uri); return this; } }
实体类Expand_DTO:
package com.svp.haoyan.expandablelistview.dto; /** * Created by haoyan on 2016/5/7. */ public class Expand_DTO { private String name; private String contents; private int pic; private int photo; public static class Expand_child { private String childname; private String childcont; public String getChildname() { return childname; } public void setChildname(String childname) { this.childname = childname; } public String getChildcont() { return childcont; } public void setChildcont(String childcont) { this.childcont = childcont; } } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContents() { return contents; } public void setContents(String contents) { this.contents = contents; } public int getPic() { return pic; } public void setPic(int pic) { this.pic = pic; } public int getPhoto() { return photo; } public void setPhoto(int photo) { this.photo = photo; } }
接下来是最主要的Adapter:
package com.svp.haoyan.expandablelistview; import android.content.Context; import android.database.DataSetObserver; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListAdapter; import com.svp.haoyan.expandablelistview.tool.ViewHolder; import java.util.List; import java.util.Map; /** * Created by haoyan on 2016/4/28. */ public abstract class MyExpandableListAdapterimplements ExpandableListAdapter { private Context mcontext; private List mexpand_dtos; private Map > mmap = null; public MyExpandableListAdapter(Context context, List expand_dtos,Map > map) { this.mcontext = context; this.mexpand_dtos = expand_dtos; this.mmap=map; } /*注册一个观察者(observer),当此适配器数据修改时即调用此观察者。*/ @Override public void registerDataSetObserver(DataSetObserver observer) { } /*取消先前通过registerDataSetObserver(DataSetObserver)方式注册进该适配器中的观察者对象*/ @Override public void unregisterDataSetObserver(DataSetObserver observer) { } /*获取组的个数*/ @Override public int getGroupCount() { return mexpand_dtos.size(); } /*返回在指定Group的Child数目。*/ @Override public int getChildrenCount(int groupPosition) { return mmap.get(groupPosition).size(); } //获取当前父item的数据 @Override public T getGroup(int groupPosition) { return mexpand_dtos.get(groupPosition); } /*获取与在指定group给予child相关的数据。*/ @Override public F getChild(int groupPosition, int childPosition) { return (mmap.get(groupPosition).get(childPosition)); } /*获取指定组的ID*/ @Override public long getGroupId(int groupPosition) { return groupPosition; } //得到子item的ID @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } //组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。(没效果) @Override public boolean hasStableIds() { return true; } //设置父item组件 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ViewHolder viewHolder = ViewHolder.get(mcontext, convertView, parent, R.layout.expand_groupview, groupPosition,isExpanded); convert(viewHolder, getGroup(groupPosition)); return viewHolder.getmConvertView(); } //自己写 public abstract void convert(ViewHolder viewHolder, T t); public abstract void convertchild(ViewHolder viewHolder, F f); //设置子item的组件 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ViewHolder viewHolder = ViewHolder.get(mcontext, convertView, parent, R.layout.expand_childview, groupPosition,isLastChild); convertchild(viewHolder, getChild(groupPosition,childPosition)); return viewHolder.getmConvertView(); /*子元素是否处于组中的最后一个(对每个组的最后一个进行操作)*/ //boolean isLastChild /*下边是未封装代码,提供参考*/ // String Childname = (mmap.get(groupPosition).get(childPosition)).getChildname(); // LayoutInflater inflater = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); // convertView = inflater.inflate(R.layout.expand_childview, null); // TextView tv_child = (TextView) convertView.findViewById(R.id.tv_child); // tv_child.setText(Childcont); } /*是否选中指定位置上的子元素。*/ @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } /*true所有条目可以选择和点击*/ @Override public boolean areAllItemsEnabled() { return false; } /*如果当前适配器不包含任何数据则返回True。经常用来决定一个空视图是否应该被显示。 一个典型的实现将返回表达式getCount() == 0的结果,但是由于getCount()包含了头部和尾部,适配器可能需要不同的行为。*/ @Override public boolean isEmpty() { return false; } /*当组展开状态的时候此方法被调用。*/ @Override public void onGroupExpanded(int groupPosition) { } /*当组收缩状态的时候此方法被调用。*/ @Override public void onGroupCollapsed(int groupPosition) { } /*根据所给的子ID号和组ID号返回唯一的ID。此外,若hasStableIds()是true,那么必须要返回稳定的ID。*/ @Override public long getCombinedChildId(long groupId, long childId) { return 0; } /*同上*/ @Override public long getCombinedGroupId(long groupId) { return 0; } }
最后是调用类Activity:
package com.svp.haoyan.expandablelistview; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ExpandableListView; import com.svp.haoyan.expandablelistview.dto.Expand_DTO; import com.svp.haoyan.expandablelistview.tool.ViewHolder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class MainActivity extends AppCompatActivity { private ExpandableListView elistview; private Listexpanddata; private HashMap > map = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dataView(); expandView(); } private void expandView() { elistview = (ExpandableListView) findViewById(R.id.expandable_lv); elistview.setAdapter(new MyExpandableListAdapter (this, expanddata,map) { @Override public void convert(ViewHolder viewHolder, Expand_DTO expand_dto) { viewHolder.setText(R.id.tv_expand, expand_dto.getName()) .setText(R.id.tv_expand_two, expand_dto.getContents()) .setImageResource(R.id.img_expand, expand_dto.getPic(),false) .setImageResource(R.id.img_expand_two, expand_dto.getPhoto(),false); } @Override public void convertchild(ViewHolder viewHolder, Expand_DTO.Expand_child expand_child) { viewHolder.setText(R.id.tv_child,expand_child.getChildname()) .setText(R.id.tv_child2,expand_child.getChildcont()); } }); //去掉自带箭头 elistview.setGroupIndicator(null); } private void dataView() { expanddata = new ArrayList (); Expand_DTO bean_zero = new Expand_DTO(); bean_zero.setName("android新技能1"); bean_zero.setContents("1"); bean_zero.setPic(R.drawable.icon_check); bean_zero.setPhoto(R.drawable.travel_airfare_cn_position); expanddata.add(bean_zero); Expand_DTO bean_one = new Expand_DTO(); bean_one.setName("android新技能2"); bean_one.setContents("2"); bean_one.setPic(R.drawable.icon_check); bean_one.setPhoto(R.drawable.travel_airfare_cn_position); expanddata.add(bean_one); Expand_DTO bean_two = new Expand_DTO(); bean_two.setName("android新技能3"); bean_two.setContents("3"); bean_two.setPic(R.drawable.icon_check); bean_two.setPhoto(R.drawable.travel_airfare_cn_position); expanddata.add(bean_two); //这里可以使用SparseArray优化性能 map = new HashMap >(); List expand_children_zero=new ArrayList (); Expand_DTO.Expand_child child1=new Expand_DTO.Expand_child(); child1.setChildname("11111"); child1.setChildcont("22222"); expand_children_zero.add(child1); Expand_DTO.Expand_child child2=new Expand_DTO.Expand_child(); child2.setChildname("3333333"); child2.setChildcont("4444444"); expand_children_zero.add(child2); map.put(0,expand_children_zero); List expand_children_one=new ArrayList (); Expand_DTO.Expand_child child3=new Expand_DTO.Expand_child(); child3.setChildname("55555"); child3.setChildcont("66666"); expand_children_one.add(child3); Expand_DTO.Expand_child child4=new Expand_DTO.Expand_child(); child4.setChildname("77777"); child4.setChildcont("88888"); expand_children_one.add(child4); map.put(1,expand_children_one); List children_data_two=new ArrayList (); Expand_DTO.Expand_child child5=new Expand_DTO.Expand_child(); child5.setChildname("99999"); child5.setChildcont("00000"); children_data_two.add(child5); Expand_DTO.Expand_child child6=new Expand_DTO.Expand_child(); child6.setChildname("12345"); child6.setChildcont("54321"); children_data_two.add(child6); map.put(2,children_data_two); } }
完活布局很简单就不贴出来了,加上图片有图有真相:
源码下载:ExpandableListView二级分栏效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。