我在 Android Expandable 列表视图中的选择器面临一个主要问题。
这是我的代码可扩展列表视图。
public class UserMenuAdapter extends BaseExpandableListAdapter {
private ArrayList<String> groups;
private ArrayList<ArrayList<ChildItems>> childs;
private Context context;
public LayoutInflater inflater;
ImageView img_availabiliy;
private static final int[] EMPTY_STATE_SET = {};
private static final int[] GROUP_EXPANDED_STATE_SET =
{android.R.attr.state_expanded};
private static final int[][] GROUP_STATE_SETS = {
EMPTY_STATE_SET, // 0
GROUP_EXPANDED_STATE_SET // 1
};
public UserMenuAdapter(Context context, ArrayList<String> groups,
ArrayList<ArrayList<ChildItems>> childs) {
this.context = context;
this.groups = groups;
this.childs = childs;
inflater = LayoutInflater.from(context);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childs.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return (long) (groupPosition * 1024 + childPosition);
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = null;
if (convertView != null)
v = convertView;
else
v = inflater.inflate(R.layout.child_layout, parent, false);
ChildItems ci = (ChildItems) getChild(groupPosition, childPosition);
TextView tv = (TextView) v.findViewById(R.id.tvChild);
tv.setText(ci.getName());
TextView tv2 = (TextView) v.findViewById(R.id.tvChild2);
tv2.setText(ci.getDailyStatus());
img_availabiliy = (ImageView)v.findViewById(R.id.img_childlayout_AVAILABILITY);
ImageView friendPics = (ImageView)v.findViewById(R.id.ivFriendPics);
/**For Group child*/
if(groupPosition == 3 && childPosition!= 0){
if(ci.getPicture()!= null){
Bitmap bitmap = BitmapFactory.decodeByteArray(ci.getPicture(), 0, ci.getPicture().length);
bitmap = Bitmap.createScaledBitmap(bitmap, 48, 48, true);
friendPics.setImageBitmap(bitmap);
}else{
friendPics.setImageResource(R.drawable.ic_launcher);
}
img_availabiliy.setVisibility(View.INVISIBLE);
}else{
if(ci.getStatusState() == 1){
img_availabiliy.setImageResource(R.drawable.online);
}
else if(ci.getStatusState()==0){
img_availabiliy.setImageResource(R.drawable.offline);
}
else if (ci.getStatusState()==2) {
img_availabiliy.setImageResource(R.drawable.away);
}
else if(ci.getStatusState()==3){
img_availabiliy.setImageResource(R.drawable.busy);
}
else{
img_availabiliy.setImageDrawable(null);
}
if((groupPosition == 2 && childPosition == 0)){
friendPics.setImageResource(R.drawable.inviteto_ccm);
img_availabiliy.setVisibility(View.INVISIBLE);
}
else if(groupPosition == 3 && childPosition == 0){
friendPics.setImageResource(R.drawable.new_ccmgroup);
img_availabiliy.setVisibility(View.INVISIBLE);
}else{
if(ci.getPicture()!= null){
Bitmap bitmap = BitmapFactory.decodeByteArray(ci.getPicture(), 0, ci.getPicture().length);
bitmap = Bitmap.createScaledBitmap(bitmap, 48, 48, true);
friendPics.setImageBitmap(bitmap);
}else{
friendPics.setImageResource(R.drawable.ic_launcher);
}
img_availabiliy.setVisibility(View.VISIBLE);
}
}
return v;
}
@Override
public int getChildrenCount(int groupPosition) {
return childs.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return (long) (groupPosition * 1024);
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = null;
if (convertView != null)
v = convertView;
else
v = inflater.inflate(R.layout.group_layout, parent, false);
String gt = (String) getGroup(groupPosition);
TextView tv2 = (TextView) v.findViewById(R.id.tvGroup);
if (gt != null)
tv2.setText(gt);
/**Set Image on group layout, Max/min*/
View ind = v.findViewById( R.id.explist_indicator);
View groupInd = v.findViewById( R.id.llgroup);
if( ind != null ) {
ImageView indicator = (ImageView)ind;
if( getChildrenCount( groupPosition ) == 0 ) {
indicator.setVisibility( View.INVISIBLE );
} else {
indicator.setVisibility( View.VISIBLE );
int stateSetIndex = ( isExpanded ? 1 : 0) ;
Drawable drawable = indicator.getDrawable();
drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
}
}
if( groupInd != null ) {
RelativeLayout indicator2 = (RelativeLayout)groupInd;
if( getChildrenCount( groupPosition ) == 0 ) {
indicator2.setVisibility( View.INVISIBLE );
} else {
indicator2.setVisibility( View.VISIBLE );
int stateSetIndex = ( isExpanded ? 1 : 0) ;
Drawable drawable2 = indicator2.getBackground();
drawable2.setState(GROUP_STATE_SETS[stateSetIndex]);
}
}
return v;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void onGroupCollapsed(int groupPosition) {
}
public void onGroupExpanded(int groupPosition) {
}
public void updateUserMenu(ArrayList<ArrayList<ChildItems>> childBack){
this.childs = childBack;
}
}
当我展开组时,我会更改选择器栏的颜色,反之亦然。
我面临的问题是,当我从任何活动返回或严格向上和向下滚动我的可扩展列表视图时,即使选择/取消选择该组,选择器颜色也会自动更改
如果有人按下(设备的)屏幕顶部,那么选择器也会更改
我对它向我展示的行为感到完全困惑,并且无法找出正在发生的事情。如果有人明白请帮忙