3

我在 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;

    }

}

当我展开组时,我会更改选择器栏的颜色,反之亦然。

我面临的问题是,当我从任何活动返回或严格向上和向下滚动我的可扩展列表视图时,即使选择/取消选择该组,选择器颜色也会自动更改

如果有人按下(设备的)屏幕顶部,那么选择器也会更改

我对它向我展示的行为感到完全困惑,并且无法找出正在发生的事情。如果有人明白请帮忙

4

3 回答 3

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

谢谢。

于 2013-03-11T05:43:27.780 回答
0

问题出在您的 getGroupView() 方法中,您试图在其中设置指标和指标 2 的背景。Drawable 的 setState() 方法没有显式地设置背景——它只设置在响应状态改变事件时在 UI 上使用的资源数组。您还必须在 setState() 之后调用 invalidateSelf() 以强制 Drawable 使用新资源重绘,这可能会也可能不会解决此问题。我建议只在视图本身上使用 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;
    }
于 2013-03-07T20:06:45.163 回答
0

亲爱的,我在可扩展列表视图中遇到了类似的问题。我的问题是我单击任何父项总是滚动整个列表视图。可能是我的解决方案帮助。

当您使用可扩展列表视图时,只需检查您的 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>

android:transcriptMode="alwaysScroll" 用普通改变这个 else 禁用 ok

好运

于 2013-03-08T05:55:59.157 回答