在DialogFragment中使用ExpandableList

    我本来想在ListFragment中使用ExpandableList, 几次尝试终告失败。 因此,我修改了练习方案,在一个DialogFragment中使用ExpandableListView, 运行成功了,不过相貌比较难看,特别是那个GroupIndicator图片,因被拉伸显得极其丑陋, 这个问题以后再解决吧。

1. 在layout中配置ExpandableListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
   <ExpandableListView 
         android:id="@+id/expandableList1"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:groupIndicator="@drawable/collapsed"
         >
   </ExpandableListView> 

</LinearLayout>


2. 源码中,显示ExpandableListView的代码
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
    	 
       //找到layout资源中名为expandable_list.xml的文件
       View v = inflater.inflate(R.layout.expandable_list, container, false);
       //找到该xml文件中定义的ExpandableListView的ID。
       lv = (ExpandableListView) v.findViewById(R.id.expandableList1);
       Log.i(TAG, "lv is null:" + (lv==null?true:false));
       
       //提取父组织的数据
       mGroupsCursor = s.getListCursorByLevel(getActivity().getContentResolver(), 0);
       getActivity().startManagingCursor(mGroupsCursor);
		
       mGroupIdColumnIndex = mGroupsCursor.getColumnIndexOrThrow(AreaSQLiteHelper.COLUMN_ID);
   	
       // Set up our adapter
       String s = getResources().getString(R.string.list_data_null);
       	
        // 根据点击的父组织ID,显示其子组织的数据
     	mAdapter = new ProvinceExpandableListAdapter(
       		mGroupsCursor,
       		getActivity(),
       		android.R.layout.simple_expandable_list_item_1,
       		android.R.layout.simple_expandable_list_item_1,
            new String[] {AreaSQLiteHelper.COLUMN_NAMES}, // group title for group layouts
            new int[] { android.R.id.text1 },
            new String[] {AreaSQLiteHelper.COLUMN_NAMES}, // exercise title for child layouts
            new int[] { android.R.id.text1}
      	);
      	
      	lv.setAdapter(mAdapter);
       
        // 配置子组织点击的事件监听器
      	lv.setOnChildClickListener(this);
      	
       return v;
    }
	
	// extending SimpleCursorTreeAdapter
	class ProvinceExpandableListAdapter extends SimpleCursorTreeAdapter {
			
			public ProvinceExpandableListAdapter(Cursor cursor, Context context,
	                        int groupLayout, int childLayout, String[] groupFrom,
	                        int[] groupTo, String[] childrenFrom, int[] childrenTo) {
	                super(context, cursor, groupLayout, groupFrom, groupTo,
	                                childLayout, childrenFrom, childrenTo);
	                
	        }

	        // returns cursor with subitems for given group cursor
	        @Override
	        protected Cursor getChildrenCursor(Cursor groupCursor) {
	        	
	        	int _countryId = groupCursor.getInt(groupCursor.getColumnIndexOrThrow(AreaSQLiteHelper.COLUMN_ID));
	        	
	        	Cursor provinceCursor = s.getListCursorByParentId(getActivity().getContentResolver(), _countryId);
	            getActivity().startManagingCursor(provinceCursor);
	            return provinceCursor;
	        }
                        // 需要配置子组织可选择
			@Override
			public boolean isChildSelectable(int groupPosition,
					int childPosition) {
				return true;
			}

	}



3. 自定义点击子项时的操作
@Override //  该 class必须要执行ExpandableListView.OnChildClickListener的接口
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Log.i(TAG,"groupPosition=" + groupPosition + ", childPosition=" + childPosition + ", id=" + id);

		//此处实现的功能是:根据子组织的ID,动态刷新列表内容,并关闭此DIALOG

		CityListFragment list = (CityListFragment) getFragmentManager().findFragmentById(R.id.cities);
		if(list!=null) list.listByParentId(id);
		
		getDialog().dismiss();
		
		return true;
	}



完整的代码:
package cn.com.demo.android.activity;

import cn.com.demo.R;
import cn.com.demo.android.activity.ProvinceList.ProvinceExpandableListAdapter;
import cn.com.demo.android.contentprovider.area.AreaContentProvider;
import cn.com.demo.android.db.AreaSQLiteHelper;
import cn.com.demo.android.service.AreaDBImpl;
import cn.com.demo.utility.MyActivity;
import android.app.DialogFragment;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.Spinner;

public class ProvinceListDialogFragment  extends DialogFragment
	implements MyActivity, ExpandableListView.OnChildClickListener{
	
	private static final String TAG = ProvinceListDialogFragment.class.getSimpleName();
	
	ExpandableListView lv;
	private Cursor mGroupsCursor; // cursor for list of groups (list top nodes)
	private int mGroupIdColumnIndex;
	private ExpandableListAdapter mAdapter;
	
	private AreaDBImpl s = new AreaDBImpl();
	
	static ProvinceListDialogFragment newInstance() {
		return new ProvinceListDialogFragment();
    }
	 
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         // Pick a style based on the num.
        int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme_Holo_Light;
        setStyle(style, theme);
         
    }
	 
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState) {
    	 
       View v = inflater.inflate(R.layout.expandable_list, container, false);
       lv = (ExpandableListView) v.findViewById(R.id.expandableList1);
       Log.i(TAG, "lv is null:" + (lv==null?true:false));
       
       mGroupsCursor = s.getListCursorByLevel(getActivity().getContentResolver(), 0);
       getActivity().startManagingCursor(mGroupsCursor);
		
       mGroupIdColumnIndex = mGroupsCursor.getColumnIndexOrThrow(AreaSQLiteHelper.COLUMN_ID);
   	
       // Set up our adapter
       String s = getResources().getString(R.string.list_data_null);
       	
     	mAdapter = new ProvinceExpandableListAdapter(
       		mGroupsCursor,
       		getActivity(),
       		android.R.layout.simple_expandable_list_item_1,
       		android.R.layout.simple_expandable_list_item_1,
            new String[] {AreaSQLiteHelper.COLUMN_NAMES}, // group title for group layouts
            new int[] { android.R.id.text1 },
            new String[] {AreaSQLiteHelper.COLUMN_NAMES}, // exercise title for child layouts
            new int[] { android.R.id.text1}
      	);
      	
      	lv.setAdapter(mAdapter);
      	lv.setOnChildClickListener(this);
      	
       return v;
    }
	
	// extending SimpleCursorTreeAdapter
	class ProvinceExpandableListAdapter extends SimpleCursorTreeAdapter {
			
			public ProvinceExpandableListAdapter(Cursor cursor, Context context,
	                        int groupLayout, int childLayout, String[] groupFrom,
	                        int[] groupTo, String[] childrenFrom, int[] childrenTo) {
	                super(context, cursor, groupLayout, groupFrom, groupTo,
	                                childLayout, childrenFrom, childrenTo);
	                
	        }

	        // returns cursor with subitems for given group cursor
	        @Override
	        protected Cursor getChildrenCursor(Cursor groupCursor) {
	        	
	        	int _countryId = groupCursor.getInt(groupCursor.getColumnIndexOrThrow(AreaSQLiteHelper.COLUMN_ID));
	        	
	        	Cursor provinceCursor = s.getListCursorByParentId(getActivity().getContentResolver(), _countryId);
	            getActivity().startManagingCursor(provinceCursor);
	            return provinceCursor;
	        }

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

	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		Log.i(TAG,"groupPosition=" + groupPosition + ", childPosition=" + childPosition + ", id=" + id);
		
		CityListFragment list = (CityListFragment) getFragmentManager().findFragmentById(R.id.cities);
		if(list!=null) list.listByParentId(id);
		
		getDialog().dismiss();
		
		return true;
	}


}

你可能感兴趣的:(android)