Android之ExpandableListView

来源:互联网 发布:C语言 函数库 下载 编辑:程序博客网 时间:2024/06/06 08:44

 ExpandableListView笔记

  ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里又可包含多个列表项。ExpandableListVivew的用法与普通ListView的用法非常相似,只是ExpandableListVivew显示的列表项应该由ExpandableAdapter提供。

ExpandableListVivew的代码实现:

在XML文件:

先建一个ExpandableListView XML文件

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="@drawable/main_bg"

    >

<ExpandableListView

    android:id="@+id/main_expandableListView" 

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    />

</LinearLayout>

再建一个ExpandableAdapter元素的XML文件

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <TextView

    android:id="@+id/main_activity_account_layout_textview"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:gravity="center"

    android:textColor="#000000"

    android:textSize="8pt"/>

</LinearLayout>

 

java文件中:

privateExpandableListViewexpandListView;

private ExpandableListAdapteradapter;

    private ExpandableListContextMenuInfoinfo;

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

/**实例化ExpandableListView对象*/      expandListView=(ExpandableListView)findViewById(R.id.main_expandableListView);

        /**一个baseExpandableListAdapter适配器*/

        adapter=new BaseExpandableListAdapter() {

           @Override

           publicboolean isChildSelectable(int groupPosition, int childPosition) {

              returntrue;

           }

          

           @Override

           publicboolean hasStableIds() {

              returntrue;

           }

          

           @Override/**获得groupView*/

           public View getGroupView(finalint groupPosition, boolean isExpanded,View convertView,ViewGroup parent) {

LayoutInflaterinflater=LayoutInflater.from(context);

View view=

inflater.inflate(R.layout.main_activity_category_layout,null);

              return view;

           }

          

           @Override

           publiclong getGroupId(int groupPosition) {

              return groupPosition;

           }

          

           @Override

           publicint getGroupCount() {

              returnlist_category.size();

           }

          

           @Override

           public Object getGroup(int groupPosition) {

              Category cg=list_category.get(groupPosition);

              return cg.getName();

           }

          

           @Override

           publicint getChildrenCount(int groupPosition) {

              Category cg=list_category.get(groupPosition);

              list_account=cdao.deepList_LoadAccountName(context, cg.getId());

              returnlist_account.size();

           }

          

           @Override/**获得chileView*/

           public View getChildView(int groupPosition,int childPosition,

                  boolean isLastChild, View convertView, ViewGroup parent) {

              LayoutInflater inflater=LayoutInflater.from(context);

              View view=inflater.inflate(R.layout.main_activiyt_account_layout,null);

              TextView textview=(TextView)view.findViewById(R.id.main_activity_account_layout_textview);

              return view;

           }

          

           @Override

           publiclong getChildId(int groupPosition, int childPosition) {

              return childPosition;

           }

          

           @Override

           public Object getChild(int groupPosition,int childPosition) {

              Category cg=list_category.get(groupPosition);

              list_account=cdao.deepList_LoadAccountName(context,cg.getId());

              Account ac=list_account.get(childPosition);

              return ac.getName();

           }

 

           @Override

           publicvoid notifyDataSetChanged() {

              super.notifyDataSetChanged();

           }

          

       };

       expandListView.setAdapter(adapter);

       //注册一个上下文菜单

registerForContextMenu(expandListView);

//对childView进行点击监听

expandListView.setOnChildClickListener(new

ExpandableListView.OnChildClickListener(){

           @Override

           publicboolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition,long id) {

              returntrue;

           }

       });

}

 

@Override

    publicvoid onCreateContextMenu(ContextMenu menu, View v,

           ContextMenuInfo menuInfo) {

       /**获得长按对象的所有信息*/

       info=(ExpandableListContextMenuInfo)menuInfo;

//     得到父位置和子位置

System.out.println(ExpandableListView.getPackedPositionGroup(info.packedPosition)+"->father");

System.out.println(ExpandableListView.getPackedPositionChild(info.packedPosition)+"->child");

       /**获得长按的类型,父型或子型*/

    inttype=ExpandableListView.getPackedPositionType(info.packedPosition);

    refreshCategoryData();

    if(type==ExpandableListView.PACKED_POSITION_TYPE_GROUP){   menu.add(1,CATEGORY_UPDATE,1,R.string.category_update_title);

        menu.add(1,CATEGORY_DELETE,2,R.string.category_delete_title);

    }elseif(type==ExpandableListView.PACKED_POSITION_TYPE_CHILD){

    menu.add(1,ACCOUNT_UPDATE,1,R.string.account_update_title);

        menu.add(1,ACCOUNT_DELETE,2,R.string.account_delete_title);

    }

       super.onCreateContextMenu(menu, v, menuInfo);

    }

 

 

Android中替换ExpandableListView控件前面的箭头图标
    首先,自定义一个expandablelistviewselector.xml文件放到layout,具体内容如下:

<?xmlversion="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_expanded="true"

//down存放已有下拉时的图片

android:drawable="@drawable/expandablelistviewindicatordown"/>

//存放还没有下拉时的图片
<item android:drawable="@drawable/expandablelistviewindicator"/>
</selector>

其次,在程序的主体部分编写类似如下的代码:

expandList.setGroupIndicator(this.getResources().getDrawable(R.layout.expandablelistviewselector));

 

 

解决ExpandableList滑动时出现黑屏的情况:

expandList.setCacheColorHint(Color.TRANSPARENT);

原创粉丝点击