ExpandableListView的基本用法
来源:互联网 发布:fastjson解析复杂json 编辑:程序博客网 时间:2024/04/28 10:30
package com.example.k.myapplication;import android.content.Context;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.LayoutInflater;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;import com.google.android.gms.appindexing.Action;import com.google.android.gms.appindexing.AppIndex;import com.google.android.gms.common.api.GoogleApiClient;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends AppCompatActivity { ExpandableListView mainlistview = null; List<String> parent = null; Map<String, List<String>> map = null; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private GoogleApiClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mainlistview = (ExpandableListView) this .findViewById(R.id.expandableListView); //为子项设置点击事件 mainlistview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { if(groupPosition==0){ Toast.makeText(MainActivity.this,"第一行第"+childPosition+"个子项被点击",Toast.LENGTH_SHORT).show(); } if(groupPosition==1){ Toast.makeText(MainActivity.this,"第二行第"+childPosition+"个子项被点击",Toast.LENGTH_SHORT).show(); } if(groupPosition==2){ Toast.makeText(MainActivity.this,"第三行第"+childPosition+"个子项被点击",Toast.LENGTH_SHORT).show(); } return true; } }); initData(); mainlistview.setAdapter(new MyAdapter()); //去掉系统默认的箭头 mainlistview.setGroupIndicator(null); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); } // 初始化数据 public void initData() { parent = new ArrayList<String>(); parent.add("parent1"); parent.add("parent2"); parent.add("parent3"); map = new HashMap<String, List<String>>(); List<String> list1 = new ArrayList<String>(); list1.add("child1-1"); list1.add("child1-2"); list1.add("child1-3"); map.put("parent1", list1); List<String> list2 = new ArrayList<String>(); list2.add("child2-1"); list2.add("child2-2"); list2.add("child2-3"); map.put("parent2", list2); List<String> list3 = new ArrayList<String>(); list3.add("child3-1"); list3.add("child3-2"); list3.add("child3-3"); map.put("parent3", list3); } @Override public void onStart() { super.onStart(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. client.connect(); Action viewAction = Action.newAction( Action.TYPE_VIEW, // TODO: choose an action type. "Main Page", // TODO: Define a title for the content shown. // TODO: If you have web page content that matches this app activity's content, // make sure this auto-generated web page URL is correct. // Otherwise, set the URL to null. Uri.parse("http://host/path"), // TODO: Make sure this auto-generated app URL is correct. Uri.parse("android-app://com.example.k.myapplication/http/host/path") ); AppIndex.AppIndexApi.start(client, viewAction); } @Override public void onStop() { super.onStop(); // ATTENTION: This was auto-generated to implement the App Indexing API. // See https://g.co/AppIndexing/AndroidStudio for more information. Action viewAction = Action.newAction( Action.TYPE_VIEW, // TODO: choose an action type. "Main Page", // TODO: Define a title for the content shown. // TODO: If you have web page content that matches this app activity's content, // make sure this auto-generated web page URL is correct. // Otherwise, set the URL to null. Uri.parse("http://host/path"), // TODO: Make sure this auto-generated app URL is correct. Uri.parse("android-app://com.example.k.myapplication/http/host/path") ); AppIndex.AppIndexApi.end(client, viewAction); client.disconnect(); } class MyAdapter extends BaseExpandableListAdapter { //得到每一项的子View @Override public Object getChild(int groupPosition, int childPosition) { String key = parent.get(groupPosition); return (map.get(key).get(childPosition)); } //得到子item的ID @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } //设置子item的组件 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { String key = MainActivity.this.parent.get(groupPosition); String info = map.get(key).get(childPosition); if (convertView == null) { LayoutInflater inflater = (LayoutInflater) MainActivity.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.layout_children, null); } TextView tv = (TextView) convertView .findViewById(R.id.second_textview); tv.setText(info); return tv; } //获取当前父item下的子item的个数 @Override public int getChildrenCount(int groupPosition) { String key = parent.get(groupPosition); int size = map.get(key).size(); return size; } //获取当前父item的数据 @Override public Object getGroup(int groupPosition) { return parent.get(groupPosition); } @Override public int getGroupCount() { return parent.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } //设置父item组件 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = (LayoutInflater) MainActivity.this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.layout_parent, null); } TextView tv = (TextView) convertView .findViewById(R.id.parent_textview); tv.setText(MainActivity.this.parent.get(groupPosition)); ImageView i = (ImageView) convertView.findViewById(R.id.imageView); //当视图处于展开时 if (isExpanded) { i.setImageResource(R.drawable.b); } else { //当视图处于关闭时 i.setImageResource(R.drawable.a); } return convertView; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } }}布局文件:加入ExpandableListview即可
<ExpandableListView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentTop="true" android:id="@+id/expandableListView"/>
0 0
- ExpandableListView的基本用法
- ExpandableListView的基本用法
- ExpandableListView的基本使用
- ExpandableListView 的用法
- ExpandableListView的详细用法
- ExpandableListView 的用法
- ExpandableListView的用法
- ExpandableListView 的简单用法
- ExpandableListView的用法
- 原ExpandableListView的用法
- ExpandableListView的用法
- ExpandableListView的用法
- 5.2 ExpandableListView的用法
- ExpandableListView的用法
- ExpandableListView的用法
- ExpandableListView的用法
- ExpandableListView的用法
- ExpandableListView的一些基本语法以及用法,程序员须知的基本标准
- 浅谈C语言中的强符号、弱符号、强引用和弱引用
- HTTP 数据包头解析
- 用友电话面试后的几个问题总结
- 一组数字的循环输出
- java线程执行器
- ExpandableListView的基本用法
- Python append , pop, del 和 remove 基础知识篇
- Shell 脚本中如何使用make命令
- 一个程序员的进化史-第六章
- 13-1盒模型代码简写关于颜色的css样式也是可以缩写的,当你设置的颜色是16进制的色彩值时,如果每两位的值相同,可以缩写一半。 例子1: p{color:#000000;} 可以缩写为: p{
- Shell 脚本知识回顾 (六) —— Shell 函数
- 开源一个网络库ARHttpRequest,iOS适配NSURLSession/ASIHTTPRequest等,Android适配HttpURLConnection等
- linux shell for 循环语句
- java基础-css