二级下拉列表

适配器:

packagecom.example.lenovo.expandlistview;

importandroid.content.Context;

importandroid.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.BaseExpandableListAdapter;

importandroid.widget.TextView;

importandroid.widget.Toast;

importjava.util.ArrayList;

/**

* Created by Lenovo on 2017/11/17.

*/

public classAdapterextendsBaseExpandableListAdapter {

privateArrayListbiglist;

privateArrayList>totaltitlelist;

privateContextcontext;

publicAdapter(ArrayList biglist, ArrayList> totaltitlelist, MainActivity mainActivity) {

this.biglist= biglist;

this.totaltitlelist= totaltitlelist;

this.context= mainActivity;

}

@Override

public intgetGroupCount() {

returnbiglist.size();//一级标签的数目

}

@Override

public intgetChildrenCount(intgroupPosition) {//一级标签下二级标签的数目

returntotaltitlelist.get(groupPosition).size();

}

@Override

publicObject getGroup(intgroupPosition) {//一级标签的内容

return null;

}

@Override

publicObject getChild(intgroupPosition,intchildPosition) {//一级标签下二级标签的内容

return null;

}

@Override

public longgetGroupId(intgroupPosition) {//一级标签的ID

return0;

}

@Override

public longgetChildId(intgroupPosition,intchildPosition) {//二级标签的ID

return0;

}

@Override

public booleanhasStableIds() {//指定位置相应的组视图

return false;

}

@Override

publicView getGroupView(intgroupPosition,booleanisExpanded, View convertView, ViewGroup parent) {

/*

* 条目的复用会提高系统的加载性能,每一次创建新的convertview,具有一定的系统开销

*

*

* 第一次打开Listview的时候 Adapter中的convertview无疑是空的,所以就会创建convertview,那是多少个呢,一屏有多少条,就创建多少条

* 下面还有一个tag用于标记convertview ,复用机制不保证顺序,但可以利用完,可以把这放在一个首尾相拉的链表里(一个闭环的圆圈链表)

*

*

* */

GroupViewHolder holder =null;

if(null== convertView) {

holder =newGroupViewHolder();

convertView = LayoutInflater.from(context).inflate(R.layout.groupitem,null);

holder.tv_group= convertView.findViewById(R.id.tv_group);

convertView.setTag(holder);

}else{

holder = (GroupViewHolder) convertView.getTag();

}

holder.tv_group.setText(biglist.get(groupPosition));

returnconvertView;

}

@Override

publicView getChildView(final intgroupPosition,final intchildPosition,booleanisLastChild, View convertView, ViewGroup parent) {

ChildViewHolder holder=null;

if(null==convertView){

holder=newChildViewHolder();

convertView=LayoutInflater.from(context).inflate(R.layout.childitem,null);

holder.tv_child= convertView.findViewById(R.id.tv_child);

convertView.setTag(holder);

}else{

holder= (ChildViewHolder) convertView.getTag();

}

holder.tv_child.setText(totaltitlelist.get(groupPosition).get(childPosition));

holder.tv_child.setOnClickListener(newView.OnClickListener() {

@Override

public voidonClick(View v) {

Toast.makeText(context,totaltitlelist.get(groupPosition).get(childPosition)+"被点击了", Toast.LENGTH_SHORT).show();

}

});

returnconvertView;

}

@Override

public booleanisChildSelectable(intgroupPosition,intchildPosition) {

return false;

}

}

classGroupViewHolder {

TextViewtv_group;

}

classChildViewHolder{

TextViewtv_child;

}



=========================================================================================================

//主activity

packagecom.example.lenovo.expandlistview;

importandroid.graphics.drawable.Drawable;

importandroid.os.Bundle;

importandroid.support.v7.app.AppCompatActivity;

importandroid.view.View;

importandroid.widget.ExpandableListView;

importjava.lang.reflect.Array;

importjava.util.ArrayList;

public classMainActivityextendsAppCompatActivity {

privateExpandableListViewelv_main;

privateArrayListbiglist;

privateArrayList>totaltitlelist;

/*

* Android开发中我们经常会用listview来加载一些列表数据,但有时候呢,并不能满足我们的需求,所以我们用expandlistview来实现我们的功能更为方便点

* expandlistview(可扩展的)主要是由组和子元素来组成的,所以呢我们就把组元素和子元素的数据都搞出来

*

*

* */

@Override

protected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

initData();

}

private voidinitData() {

biglist=newArrayList<>();

biglist.add("家人");

biglist.add("同学");

ArrayList family =newArrayList<>();

family.add("父亲");

family.add("母亲");

ArrayList student =newArrayList<>();

student.add("张三");

student.add("李四");

totaltitlelist=newArrayList<>();

totaltitlelist.add(family);

totaltitlelist.add(student);

//不需要加这个箭头的话,可以用这个属性来取消

//elv_main.setGroupIndicator(null);

/* 这里涉及到了事件分发,这里事件将不能被传递给子条目

elv_main.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

@Override

public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

return true;//TODO 事件被消费了}

});*/

Adapter adapter =newAdapter(biglist,totaltitlelist,this);

elv_main.setAdapter(adapter);

}

private voidinitView() {

elv_main= (ExpandableListView) findViewById(R.id.elv_main);

}

}

你可能感兴趣的:(二级下拉列表)