资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android中ExpandableListView使用示例详解

本文实例为大家分享了ExpandableListView使用示例,供大家参考,具体内容如下

长沙网站建设公司成都创新互联,长沙网站设计制作,有大型网站制作公司丰富经验。已为长沙上千多家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的长沙做网站的公司定做!

MainActivity:

public class Expandable_test extends Activity {
  private ExpandableListView listView;
  private Map> dataset = new HashMap<>();
  private String[] parentList = new String[]{"第一个菜单", "第二个菜单"};

  private ExpandableListViewAdpter adpter;
  private Context mContext = this;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expandable_test);
    listView=(ExpandableListView)findViewById(R.id.expandableListViewtext);
    adpter=new ExpandableListViewAdpter(this,parentList);
    listView.setAdapter(adpter);
  }

}

MainActivity.xml:







ExpandableListViewAdpter:

public class ExpandableListViewAdpter extends BaseExpandableListAdapter {
  private Context mContext;
  private Map> dataset = new HashMap<>();
  private String[] parentList = new String[]{"第一个菜单", "第二个菜单"};
  private String[][] chdrenList=new String[][]
      {{"菜单1","菜单1","菜单1","菜单1","菜单1","菜单1"},{"菜单2","菜单2","菜单2","菜单2","菜单2"}};

  public ExpandableListViewAdpter(Context context, String[] parentList){
    this.mContext=context;
    this.dataset=dataset;
    this.parentList=parentList;
  }

  @Override
  public int getGroupCount() {
    return parentList.length;
  }

  @Override
  public int getChildrenCount(int groupPosition) {
    return chdrenList[groupPosition].length;
  }

  @Override
  public Object getGroup(int groupPosition) {
    return parentList[groupPosition];
  }

  @Override
  public Object getChild(int groupPosition, int childPosition) {
    return chdrenList[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 true;
  }

  @Override
  public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    GroupViewHolder holder=null;
    if(convertView==null){
      convertView = LayoutInflater.from(mContext).inflate(R.layout.itemlayoutexpandable, null);
      holder=new GroupViewHolder();
      holder.text=(TextView)convertView.findViewById(R.id.item_text);
      convertView.setTag(holder);
    }
    else {
      holder=(GroupViewHolder)convertView.getTag();
    }
    holder.text.setText(parentList[groupPosition]);
    return convertView;
  }

  @Override
  public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ChildViewHolder holder=null;
      if(convertView==null){
        convertView = LayoutInflater.from(mContext).inflate(R.layout.itemlayoutexpandable, null);
        holder=new ChildViewHolder();
        holder.text=(TextView)convertView.findViewById(R.id.item_text);
        convertView.setTag(holder);
    }
    else {
        holder=(ChildViewHolder)convertView.getTag();
      }
      holder.text.setText(chdrenList[groupPosition][childPosition]);
    return convertView;
  }

  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
  }
  class GroupViewHolder {
    TextView text;
  }

  class ChildViewHolder {
    TextView text;
  }
}

Item.xml:




Android中ExpandableListView使用示例详解

Android中ExpandableListView使用示例详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网页标题:Android中ExpandableListView使用示例详解
文章转载:http://cdkjz.cn/article/pssceh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220