web-dev-qa-db-fra.com

Changer l'indicateur extensible dans ExpandableListView

Essayer de créer un ExpandableListView. La vue initiale avec les groupes apparaît bien. Cependant, lorsque je clique sur l'élément de la liste, ma flèche ne change pas. Voir les images ci-dessous.

enter image description here

Comment puis-je changer la direction de la flèche?

La mise en page XML:

  <ExpandableListView
        Android:id="@+id/expandable_list"
        Android:layout_width="fill_parent"
        Android:layout_height="match_parent"            
        Android:divider="@null"
        Android:background="#ffffff"
        Android:groupIndicator="@drawable/settings_selector"
        Android:transcriptMode="alwaysScroll" />

settings_selector.xml:

 <?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:Android="http://schemas.Android.com/apk/res/Android" >

<selector xmlns:Android="http://schemas.Android.com/apk/res/Android" >
    <item
        Android:drawable="@drawable/arrow_down"
        Android:state_empty="true"/>
    <item
        Android:drawable="@drawable/arrow_right"
        Android:state_expanded="true"/>
</selector>

</animation-list>
44
NagarjunaReddy

liste de diffusion extensible

 <ExpandableListView
    Android:id="@+id/expandable_list"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:groupIndicator="@drawable/group_indicator"
    Android:transcriptMode="alwaysScroll" />

setindicator ici iam utilisant un code setindicator comme celui-ci, ça marche

 DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels; 

 mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
 mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));  

   public int GetPixelFromDips(float pixels) {
    // Get the screen's density scale 
    final float scale = getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale
    return (int) (pixels * scale + 0.5f);
}

res/drawable/group_indicator

  <?xml version="1.0" encoding="utf-8"?>

     <selector xmlns:Android="http://schemas.Android.com/apk/res/Android">            
           <item Android:drawable="@drawable/arrow_right" Android:state_empty="true">  </item>            
           <item Android:drawable="@drawable/arrow_down" Android:state_expanded="true"></item>                
           <item Android:drawable="@drawable/arrow_right"></item>
     </selector>
63
NagarjunaReddy

Essayez ça pour votre settings_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:Android="http://schemas.Android.com/apk/res/Android" >

    <item
        Android:drawable="@drawable/arrow_right"
        Android:state_expanded="true" />

    <item
        Android:drawable="@drawable/arrow_down" />

</selector>
25
Benoit Duffez

J'avais fait le chemin ci-dessous: décidez ce qui peut être dessiné à gauche/à droite pour votre groupView en fonction du drapeau isExpanded.
De cette façon, il nous est plus facile de personnaliser le remplissage/arrière-plan et d’autres éléments de l’indicateur pouvant être dessinés.

J'espère que ça aide.

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        TextView textView = (TextView) mLayoutInflater.inflate(R.layout.menu_group, null);
        textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, isExpanded ? 0 : Android.R.drawable.ic_menu_more, 0);
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }
15
lily

assujettir:

    int width = getResources().getDisplayMetrics().widthPixels;
    if (Android.os.Build.VERSION.SDK_INT < Android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        listView.setIndicatorBounds(width - getPixelValue(40), width - getPixelValue(10));
    } else {
        listView.setIndicatorBoundsRelative(width - getPixelValue(40), width - getPixelValue(10));
    }

et méthode d'assistance:

public static int getPixelValue(int dp) {

    final float scale = getResources().getDisplayMetrics().density;
    return (int) (dp * scale + 0.5f);
}
4
tier777
import Java.util.ArrayList;

import Android.app.Activity;
import Android.content.Context;
import Android.database.DataSetObserver;
import Android.os.Bundle;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.View.OnClickListener;
import Android.view.ViewGroup;
import Android.widget.BaseExpandableListAdapter;
import Android.widget.ExpandableListView;
import Android.widget.TextView;

public class MyActivity extends Activity {

    private ExpandableListView mExpandableList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);


        mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
        mExpandableList.setGroupIndicator(null);
        ArrayList<Parent> arrayParents = new ArrayList<Parent>();
        ArrayList<String> arrayChildren = new ArrayList<String>();

        //here we set the parents and the children
        for (int i = 0; i < 10; i++){
            //for each "i" create a new Parent object to set the title and the children
            Parent parent = new Parent();
            parent.setTitle("Parent " + i);

            arrayChildren = new ArrayList<String>();
            for (int j = 0; j < 10; j++) {
                arrayChildren.add("Child " + j);
            }
            parent.setArrayChildren(arrayChildren);

            //in this array we add the Parent object. We will use the arrayParents at the setAdapter
            arrayParents.add(parent);
        }

        //sets the adapter that provides data to the list.
        mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this,arrayParents));

    }

    public class Parent {
        private String mTitle;
        private ArrayList<String> mArrayChildren;

        public String getTitle() {
            return mTitle;
        }

        public void setTitle(String mTitle) {
            this.mTitle = mTitle;
        }

        public ArrayList<String> getArrayChildren() {
            return mArrayChildren;
        }

        public void setArrayChildren(ArrayList<String> mArrayChildren) {
            this.mArrayChildren = mArrayChildren;
        }
    }




    public class MyCustomAdapter extends BaseExpandableListAdapter implements OnClickListener{


        private LayoutInflater inflater;
        private ArrayList<Parent> mParent;

        public MyCustomAdapter(Context context, ArrayList<Parent> parent){
            mParent = parent;
            inflater = LayoutInflater.from(context);
        }


        @Override
        //counts the number of group/parent items so the list knows how many times calls getGroupView() method
        public int getGroupCount() {
            return mParent.size();
        }

        @Override
        //counts the number of children items so the list knows how many times calls getChildView() method
        public int getChildrenCount(int i) {
            return mParent.get(i).getArrayChildren().size();
        }

        @Override
        //gets the title of each parent/group
        public Object getGroup(int i) {
            return mParent.get(i).getTitle();
        }

        @Override
        //gets the name of each item
        public Object getChild(int i, int i1) {
            return mParent.get(i).getArrayChildren().get(i1);
        }

        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i1) {
            return i1;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        //in this method you must set the text to see the parent/group on the list
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

            if (view == null) {
                view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
            }
            view.findViewById(R.id.button).setTag(i);
            view.findViewById(R.id.button).setOnClickListener(this);
            TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
            //"i" is the position of the parent/group in the list
            textView.setText(getGroup(i).toString());

            //return the entire view
            return view;
        }

        @Override
        //in this method you must set the text to see the children on the list
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
            }

            TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
            //"i" is the position of the parent/group in the list and
            //"i1" is the position of the child
            textView.setText(mParent.get(i).getArrayChildren().get(i1));

            //return the entire view
            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return true;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            /* used to make the notifyDataSetChanged() method work */
            super.registerDataSetObserver(observer);
        }


        /* (non-Javadoc)
         * @see Android.view.View.OnClickListener#onClick(Android.view.View)
         * @since Mar 20, 2013
         * @author rajeshcp 
         */
        @Override
        public void onClick(View v) {
            if(mExpandableList.isGroupExpanded((Integer)v.getTag()))
        {
            mExpandableList.collapseGroup((Integer)v.getTag());
        }else
        {
            mExpandableList.expandGroup((Integer)v.getTag());
        }
        }
    }


}

Changez votre MyActivity comme ceci et faites-moi savoir ce que vous voulez d'autre?

4
Triode

Créez simplement une vue/Imageview où vous voulez dans le code XML d'un exemple d'élément de groupe:

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:id="@+id/parent_group"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="horizontal"
Android:padding="10dp">

<ImageView
    Android:id="@+id/expandable_icon"
    Android:layout_width="25dp"
    Android:layout_height="25dp"
    Android:layout_marginTop="6dp"
    Android:src="@drawable/group_icon_not_expanded" />

<TextView
    Android:id="@+id/group_name"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:layout_gravity="center"
    Android:layout_marginLeft="10dp"
    Android:fontFamily="@font/roboto_thin"
    Android:textColor="@Android:color/black"
    Android:textSize="17sp" />

Ensuite, pour votre ExpandableListView, utilisez GroupClickListener pour modifier l’image pour ImageView par programme, par exemple:

listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            parent.smoothScrollToPosition(groupPosition);

            if (parent.isGroupExpanded(groupPosition)) {
                ImageView imageView = v.findViewById(R.id.expandable_icon);
                imageView.setImageDrawable(getResources().getDrawable(R.drawable.group_icon_not_expanded));
            } else {
                ImageView imageView = v.findViewById(R.id.expandable_icon);
                imageView.setImageDrawable(getResources().getDrawable(R.drawable.group_icon_expanded));
            }
            return false    ;
        }
    });
1
shadygoneinsane