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

親愛なる私は、展開可能なリストビューでこのような問題に直面しています。私の問題私は親項目をクリックすると常にリストビュー全体をスクロールします。私の解決策になるかもしれません。

展開可能なリストビューを使用するときは、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" これを通常の状態に変更します

幸運

于 2013-03-08T05:55:59.157 に答える
0

問題は、インジケータとインジケータ 2 の背景を設定しようとしている getGroupView() メソッドにあります。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 に答える