我在Android可扩展列表视图中遇到了选择器的主要问题。
这是我的代码可扩展列表视图。
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;
}
}
当我展开组时,我会更改选择器栏颜色,反之亦然。
我面临的问题是,当我从任何活动返回或向上滚动时严格按照我的可扩展列表视图,即使选择/取消选择组,选择器颜色也会自动更改
如果有人按下屏幕顶部(设备),则选择器也会更改
我对它向我展示的行为感到困惑,并且无法弄清楚发生了什么。如果有人理解,请帮助
答案 0 :(得分:1)
您使用了一组错误的数组值,只需使用它 -
if( getChildrenCount( groupPosition ) == 0 ) {
indicator.setVisibility( View.INVISIBLE );
} else {
indicator.setVisibility( View.VISIBLE );
if(isExpanded){
indicator.setImageResource(R.drawable.expander_min);
indicator2.setBackgroundResource(R.drawable.list_selected);
mbtnMenu.setVisibility(View.VISIBLE);
}else{
indicator.setImageResource(R.drawable.expander_max);
indicator2.setBackgroundResource(R.drawable.list_unselected);
mbtnMenu.setVisibility(View.INVISIBLE);
}
}
感谢。
答案 1 :(得分:0)
问题出在你的getGroupView()方法中,你试图设置指标和指标2的背景。 Drawable的setState()方法没有显式设置背景 - 它只设置在响应状态改变事件时要在UI上使用的资源数组。您还必须在setState()之后调用invalidateSelf()以强制Drawable使用新资源重绘,这可能会也可能不会解决此问题。我建议在Views本身上使用setBackground()或setBackgroundResource()。像这样:
@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 );
indicator.setBackgroundResource(isExpanded ? android.R.attr.state_expanded : 0);
}
}
if( groupInd != null ) {
RelativeLayout indicator2 = (RelativeLayout)groupInd;
if( getChildrenCount( groupPosition ) == 0 ) {
indicator2.setVisibility( View.INVISIBLE );
} else {
indicator2.setVisibility( View.VISIBLE );
indicator2.setBackgroundResource(isExpanded ? android.R.attr.state_expanded : 0);
}
}
return v;
}
答案 2 :(得分:0)
亲爱的,我在可扩展列表视图中面对这个问题。 我的问题我点击任何父项始终滚动整个listview.may是 我的解决方案帮助。
使用expandable listview时,只需检查您的xml文件。
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transcriptMode="alwaysScroll"
android:layout_toRightOf="@+id/buttonBuyAmazon" >
</ExpandableListView>
<强>机器人:transcriptMode = “alwaysScroll”强> 用正常的其他方法改变这一点
祝你好运