当前位置:编程学习 > 网站相关 >>

ExpandableList------可扩展的list

即是:单击某个item后,又可显示一个子list。它的数据通过绑定到ExpandableListAdapter或者ExpandableListAdapter的子类上。
示例一:通过SimpelExpandableListAdapter绑定数据

public class MainActivity extends ExpandableListActivity {

List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childrens = new ArrayList<List<Map<String, String>>>();
SimpleExpandableListAdapter adapter ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new SimpleExpandableListAdapter(
          this,
          groupData(),
          android.R.layout.易做图_expandable_list_item_1,
          new String[]{"group"},
          new int[]{android.R.id.text1},
          childData(),
          android.R.layout.易做图_expandable_list_item_2,
          new String[]{"child"},
          new int[]{android.R.id.text2}
          );
        setListAdapter(adapter);
    }
    /**
     * 该方法为一级条目提供数据
     * @return  一级条目数据
     */
    private List<Map<String, String>> groupData(){
      Map<String, String> group1 = new HashMap<String, String>();
      group1.put("group", "group1");
      Map<String, String> group2 = new HashMap<String, String>();
      group2.put("group", "group2");
      groups.add(group1);
      groups.add(group2);
return groups;
    }
   
    /**
     * 该方法为二级条目提供数据
     * @return  二级条目数据
     */
    private List<List<Map<String, String>>> childData(){
      List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
      Map<String, String> child1Data = new HashMap<String, String>();
      child1Data.put("child", "child1");
      child1.add(child1Data);
      List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
      Map<String, String> child2Data = new HashMap<String, String>();
      child2Data.put("child", "child2");
      child2.add(child2Data);
      childrens.add(child1);
      childrens.add(child2);
return childrens;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

示例二:通过SimpleCussorTreeAdapter绑定数据:

public class MainActivity extends ExpandableListActivity {

/**
 * 获取联系人ID的列位置,一般都是第一个,也就是0
 */
private int mGroupIdColumnIndex;
/**
 *  要查询联系人的电话号码的字段(也就是子视图的信息)
 */
private String[] mPhoneNumberProject = new String[]{
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER};
private ExpandableListAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        // 查询联系人并获取其Cursor对象
        CursorLoader loader = new CursorLoader(this,
         ContactsContract.Contacts.CONTENT_URI,
         new String[]{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME},
null,
null,
null);
        Cursor groupCursor =loader.loadInBackground();
        // 保存取联系人ID的列位置
        mGroupIdColumnIndex = groupCursor
         .getColumnIndexOrThrow(ContactsContract.Contacts._ID);
       
        adapter = new MyExpandableListAdapter(this,
         groupCursor,
         android.R.layout.易做图_expandable_list_item_1,
         new String[]{ContactsContract.Contacts.DISPLAY_NAME},
         new int[]{android.R.id.text1},
         android.R.layout.易做图_expandable_list_item_2,
         new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
         new int[]{android.R.id.text2}
        );
        setListAdapter(adapter);
    }
   
    private class MyExpandableListAdapter extends SimpleCursorTreeAdapter{

public MyExpandableListAdapter(Context context, Cursor cursor,
int groupLayout, String[] groupFrom, int[] groupTo,
int childLayout, String[] childFrom, int[] childTo) {
super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom,
childTo);
}

@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
long contactId = groupCursor.getLong(mGroupIdColumnIndex);
Uri.Builder builder = ContactsContract.CommonDataKinds
.Phone.CONTENT_URI.buildUpon();
Uri phoneNumbersUri = builder.build();
CursorLoader curLoader = new CursorLoader(MainActivity.this,
phoneNumbersUri,
mPhoneNumberProject,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)},
null
);
Cursor cursor = curLoader.loadInBackground();
return cursor;
}
    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    &nb

补充:综合编程 , 其他综合 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,