android中ExpandableListView点击哪一条那一条显示,而其他条隐藏的实现办法
效果图:直接上代码
[java]
public class MyActivity extends Activity implements HandleClick{
private ExpandableListView listView;
private int lastClick = -1;//上一次点击的group的position
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ExpandableListView)findViewById(R.id.listView);
//去掉前面的箭头图标
listView.setGroupIndicator(null);
MyExpandAdapter adapter = new MyExpandAdapter(this);
adapter.setHnadleClick(this);
listView.setAdapter(adapter);
listView.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
if(lastClick == -1)
{
listView.expandGroup(groupPosition);
}
if(lastClick != -1 && lastClick != groupPosition)
{
listView.collapseGroup(lastClick);
listView.expandGroup(groupPosition);
}
else if(lastClick == groupPosition)
{
if(listView.isGroupExpanded(groupPosition))
listView.collapseGroup(groupPosition);
else if(!listView.isGroupExpanded(groupPosition))
listView.expandGroup(groupPosition);
}
lastClick = groupPosition;
return true;
}
});
}
public void handleClick(int type) {
Toast.makeText(MyActivity.this, "...点了一下...", Toast.LENGTH_SHORT).show();
}
}
适配器:
[java]
public class MyExpandAdapter extends BaseExpandableListAdapter{
private Context context;
private String[] roomName = new String[]{"房间1","房间2","房间3","房间4","房间5"};
private String[] peoNum = new String[]{"1323人","6654人","86554人","6565人","7967人"};
private String[] startTime = new String[]{"马上开始","1分钟","2分钟","3分钟","4分钟"};
private HandleClick handleClick;
public inte易做图ce HandleClick
{
public void handleClick(int type);
}
public void setHnadleClick(HandleClick hc)
{
this.handleClick = hc;
}
public MyExpandAdapter(Context context)
{
this.context = context;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
convertView = (RelativeLayout)RelativeLayout.inflate(context, R.layout.group, null);
TextView tv_roomName = (TextView)convertView.findViewById(R.id.tv_roomName);
TextView tv_time = (TextView)convertView.findViewById(R.id.tv_time);
TextView tv_peoNum = (TextView)convertView.findViewById(R.id.tv_peoNum);
tv_roomName.setText(roomName[groupPosition]);
tv_time.setText(peoNum[groupPosition]);
tv_peoNum.setText(startTime[groupPosition]);
retu
补充:移动开发 , Android ,