zoukankan      html  css  js  c++  java
  • ANDROID笔记:ExpandableListView和Adapter的简单使用

     1 package com.example.adaptertest.doubleadapter;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.widget.ExpandableListView;
     6 
     7 import com.example.adaptertest.R;
     8 
     9 public class ExpandListViewActivity extends Activity {
    10     private String[] groupStrings = { "家人", "同事", "朋友" };
    11     private String[][] personStrings = { { "爸", "妈" }, { "同事1", "同事2" },
    12             { "朋友1", "朋友2" } };
    13 
    14     @Override
    15     protected void onCreate(Bundle savedInstanceState) {
    16         // TODO Auto-generated method stub
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_expandlistview);
    19         ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandlistview_list);
    20         expandableListView.setAdapter(new BaseExpandListViewAdapter(
    21                 ExpandListViewActivity.this, groupStrings, personStrings));
    22 
    23     }
    24 
    25 }
      1 package com.example.adaptertest.doubleadapter;
      2 
      3 import android.content.Context;
      4 import android.view.LayoutInflater;
      5 import android.view.View;
      6 import android.view.View.OnClickListener;
      7 import android.view.ViewGroup;
      8 import android.widget.BaseExpandableListAdapter;
      9 import android.widget.TextView;
     10 import android.widget.Toast;
     11 
     12 import com.example.adaptertest.R;
     13 
     14 public class BaseExpandListViewAdapter extends BaseExpandableListAdapter {
     15     private LayoutInflater inflater;
     16     private String[] groupStrings;
     17     private String[][] personStrings;
     18     private Context context;
     19 
     20     public BaseExpandListViewAdapter(Context context, String[] groupStrings,
     21             String[][] personStrings) {
     22         this.groupStrings = groupStrings;
     23         this.context = context;
     24         this.personStrings = personStrings;
     25         inflater = LayoutInflater.from(context);
     26     }
     27 
     28     @Override
     29     public int getGroupCount() {
     30         // TODO Auto-generated method stub
     31         return groupStrings.length;
     32     }
     33 
     34     @Override
     35     public int getChildrenCount(int groupPosition) {
     36         // TODO Auto-generated method stub
     37         return personStrings[groupPosition].length;
     38     }
     39 
     40     @Override
     41     public Object getGroup(int groupPosition) {
     42         // TODO Auto-generated method stub
     43         return groupStrings[groupPosition];
     44     }
     45 
     46     @Override
     47     public Object getChild(int groupPosition, int childPosition) {
     48         // TODO Auto-generated method stub
     49         return personStrings[groupPosition][childPosition];
     50     }
     51 
     52     @Override
     53     public long getGroupId(int groupPosition) {
     54         // TODO Auto-generated method stub
     55         return groupPosition;
     56     }
     57 
     58     @Override
     59     public long getChildId(int groupPosition, int childPosition) {
     60         // TODO Auto-generated method stub
     61         return 0;
     62     }
     63 
     64     @Override
     65     public boolean hasStableIds() {
     66         // TODO Auto-generated method stub
     67         return false;
     68     }
     69 
     70     @Override
     71     public View getGroupView(int groupPosition, boolean isExpanded,
     72             View convertView, ViewGroup parent) {
     73         // TODO Auto-generated method stub
     74         View view = inflater.inflate(R.layout.expandlistview_item, null);
     75         TextView textView = (TextView) view.findViewById(R.id.expand_text);
     76         textView.setText(groupStrings[groupPosition]);
     77         return view;
     78     }
     79 
     80     @Override
     81     public View getChildView(final int groupPosition, final int childPosition,
     82             boolean isLastChild, View convertView, ViewGroup parent) {
     83         View view = inflater.inflate(R.layout.expandlistview_childitem, null);
     84         view.setOnClickListener(new OnClickListener() {
     85 
     86             @Override
     87             public void onClick(View v) {
     88                 // TODO Auto-generated method stub
     89                 Toast.makeText(context,
     90                         personStrings[groupPosition][childPosition],
     91                         Toast.LENGTH_SHORT).show();
     92             }
     93         });
     94         TextView textView = (TextView) view
     95                 .findViewById(R.id.expand_child_text);
     96         textView.setText(personStrings[groupPosition][childPosition]);
     97         return view;
     98     }
     99 
    100     @Override
    101     public boolean isChildSelectable(int groupPosition, int childPosition) {
    102         // TODO Auto-generated method stub
    103         return false;
    104     }
    105 
    106 }
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <ExpandableListView
     8         android:id="@+id/expandlistview_list"
     9         android:layout_width="fill_parent"
    10         android:layout_height="fill_parent">
    11     </ExpandableListView>
    12 
    13 </LinearLayout>
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="horizontal" >
     6 
     7     <ImageView
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content"
    10         android:src="@drawable/ic_launcher" />
    11 
    12     <TextView
    13         android:id="@+id/expand_text"
    14         android:layout_width="wrap_content"
    15         android:layout_height="wrap_content"
    16         android:layout_weight="1"
    17         android:text="测试"
    18         android:textSize="30sp" />
    19 
    20 </LinearLayout>
     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="horizontal" >
     6 
     7     <ImageView
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content"
    10         android:src="@drawable/ic_launcher" 
    11         android:layout_marginLeft="40dp"/>
    12 
    13     <TextView
    14         android:id="@+id/expand_child_text"
    15         android:layout_width="wrap_content"
    16         android:layout_height="wrap_content"
    17         android:layout_weight="1"
    18         android:text="测试"
    19         android:textSize="30sp" />
    20 
    21 </LinearLayout>

  • 相关阅读:
    右滑返回上一页
    flutter 启动图
    [题解]NOIP2014
    [题解]LightOJ1289 LCM from 1 to n
    [题解]CodeForces442B Andrey and Problem
    [题解]HDU4035 Maze
    [题解]CodeForces#290(div1)
    SCP-bzoj-1078
    SCP-bzoj-1068
    SCP-bzoj-1054
  • 原文地址:https://www.cnblogs.com/afluy/p/3373763.html
Copyright © 2011-2022 走看看