欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android中ExpandableListView使用示例详解

时间:2021-07-25 08:38:52|栏目:Android代码|点击:

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

MainActivity:

public class Expandable_test extends Activity {
  private ExpandableListView listView;
  private Map<String, List<String>> 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:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.fae.mobile.testActivity.Expandable_test">
<ExpandableListView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/expandableListViewtext">

</ExpandableListView>
</LinearLayout>

ExpandableListViewAdpter:

public class ExpandableListViewAdpter extends BaseExpandableListAdapter {
  private Context mContext;
  private Map<String, List<String>> 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:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
<TextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="bottom"
  android:id="@+id/item_text"/>
</LinearLayout>

上一篇:android自定义view仿今日头条加载文字变色效果

栏    目:Android代码

下一篇:Android编程实现popupwindow弹出后屏幕背景变成半透明效果

本文标题:Android中ExpandableListView使用示例详解

本文地址:http://www.codeinn.net/misctech/161220.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有