ExpandableListView的小图标有个状态,一个是不点击的情况,一个是点击后展开的情况,用xml配置如下:
Java的代码如下:
注意:不知道为什么,使用自定义的GroupIndicator会发生图片被拉伸的现象,为了解决这个问题需要定义一个长度为groups.length的boolean数组:
然后重写OnGroupCollapseListener和OnGroupExpandListener用于修改当前group的状态:
最后在getGroupView()中应用,代码片段如下:
over!
Android版手风琴(ExpandableListView)
创新源于模仿之四:增强的ExpandableListView
android api里的例子:
Android UI 之实现多级列表TreeView
http://blog.csdn.net/carrey1989/article/details/10227165
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_expanded="true" android:drawable="@drawable/narrow_select" /> <item android:drawable="@drawable/narrow" /> </selector>
Java的代码如下:
ExpandableListView listView = getExpandableListView();
listView.setGroupIndicator(this.getResources().getDrawable(R.drawable.group_icon_selector));
注意:不知道为什么,使用自定义的GroupIndicator会发生图片被拉伸的现象,为了解决这个问题需要定义一个长度为groups.length的boolean数组:
private boolean[] isOpen=new boolean[groups.length];
然后重写OnGroupCollapseListener和OnGroupExpandListener用于修改当前group的状态:
expandableListView.setOnGroupCollapseListener(onGroupCollapseListener);
expandableListView.setOnGroupExpandListener(onGroupExpandListener);
expandableListView.setGroupIndicator(null);//不要自带的了!!!
OnGroupCollapseListener onGroupCollapseListener=new OnGroupCollapseListener(){
@Override
public void onGroupCollapse(int groupPosition) {
// TODO Auto-generated method stub
isOpen[groupPosition]=false;
}
};
OnGroupExpandListener onGroupExpandListener=new OnGroupExpandListener(){
@Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
isOpen[groupPosition]=true;
}
};
最后在getGroupView()中应用,代码片段如下:
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView textView=null;
if(convertView==null){
textView=new TextView(mContext);
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 50);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER);
textView.setPadding(10, 0, 0, 0);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16.0f);
textView.setTextColor(0xFFC6B39C);
textView.setBackgroundResource(R.drawable.bg_tv);//bg_tv_expand
convertView=textView;
}else{
textView=(TextView)convertView;
}
//为了处理图标被拉伸的问题!这里采用偷懒的办法让textView傍边产生一个图标,如果GroupView只是一个TextView的话,推荐这样做!
if(isOpen[groupPosition]){
Drawable leftDrawable= resources.getDrawable(R.drawable.arrow_down_on);
leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
textView.setCompoundDrawables(leftDrawable, null, null, null);
}else{
Drawable leftDrawable= resources.getDrawable(R.drawable.arrow_right_off);
leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
textView.setCompoundDrawables(leftDrawable, null, null, null);
}
textView.setText(((Market)getGroup(groupPosition)).market);
return textView;
}
over!
Android版手风琴(ExpandableListView)
创新源于模仿之四:增强的ExpandableListView
android api里的例子:
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import com.example.android.apis.R;
/**
* Demonstrates expandable lists using a custom {@link ExpandableListAdapter}
* from {@link BaseExpandableListAdapter}.
*/
public class ExpandableList1 extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Sample menu");
menu.add(0, 0, 0, R.string.expandable_list_sample_action);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
/**
* A simple adapter which maintains an ArrayList of photo resource Ids.
* Each photo is displayed as an image. This adapter supports clearing the
* list of photos and adding a new photo.
*
*/
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(ExpandableList1.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
Android UI 之实现多级列表TreeView
http://blog.csdn.net/carrey1989/article/details/10227165