android中ExpandableListView点击哪一条那一条显示,而其他条隐藏的实现办法

效果图:


直接上代码

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();
	}
}


适配器:

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 interface 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]);
		
		return convertView;
	}
	
	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return roomName.length;
	}
	
	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return 1;
	}
	
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		
		convertView = (LinearLayout)LinearLayout.inflate(context, R.layout.child, null);
		
		TextView child_startTime = (TextView)convertView.findViewById(R.id.child_startTime);
		TextView child_peoNum = (TextView)convertView.findViewById(R.id.child_peoNum);
		Button child_button = (Button)convertView.findViewById(R.id.child_button);
		
		child_startTime.setText(startTime[groupPosition]);
		child_peoNum.setText(peoNum[groupPosition]);
		child_button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				handleClick.handleClick(1);
			}
		});
		
		return convertView;
	}
	
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}

}



你可能感兴趣的:(android基础)