Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

expandablelistview - customized Expandable listview right arrow to down arrow not working in android

Hi in the below code I was customized my expandable listview. my view like image,header title,right arrow.

If click the expanded my list item at the time want to change the right arrow to down arrow.

But how to achieve this can any one help me

listheader.java:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:id="@+id/listheadermain"
        android:orientation="horizontal">



        <ImageView
            android:id="@+id/iconimage"
            android:layout_width="30dp"
            android:layout_height="50dp"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            app:tint="@color/theme"
            android:paddingTop="5dp" />

        <TextView
            android:id="@+id/submenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/theme"
            android:fontFamily="@font/robotoregular"
            android:paddingLeft="5dp"
            android:layout_weight="1"
            android:paddingTop="15dp"
            android:textSize="12sp"/>

        <ImageView
            android:id="@+id/right_arrow"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:padding="5dp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:src="@drawable/arrow_right"
            app:tint="@color/theme" />

    </LinearLayout>

</LinearLayout>

ExpandableListAdapter.java:

public class ExpandableListAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<ExpandedMenuModel> mListDataHeader; // header titles

    // child data in format of header title, child title
    private HashMap<ExpandedMenuModel, List<String>> mListDataChild;
    ExpandableListView expandList;

    public ExpandableListAdapter(Context context, List<ExpandedMenuModel> listDataHeader, HashMap<ExpandedMenuModel, List<String>> listChildData, ExpandableListView mView) {
        this.mContext = context;
        this.mListDataHeader = listDataHeader;
        this.mListDataChild = listChildData;
        this.expandList = mView;
    }

    @Override
    public int getGroupCount() {
        int i = mListDataHeader.size();
        Log.d("GROUPCOUNT", String.valueOf(i));
        return this.mListDataHeader.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int childCount = 0;
        if (groupPosition != 8) {
            childCount = this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size();
        }
        return childCount;
    }

    @Override
    public Object getGroup(int groupPosition) {

        return this.mListDataHeader.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        Log.d("CHILD", mListDataChild.get(this.mListDataHeader.get(groupPosition))
                .get(childPosition).toString());
        return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
                .get(childPosition);
    }

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ExpandedMenuModel headerTitle = (ExpandedMenuModel) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.listheader, null);
        }
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.submenu);
        ImageView headerIcon = (ImageView) convertView.findViewById(R.id.iconimage);
        final ImageView rightarrowIcon=(ImageView) convertView.findViewById(R.id.right_arrow);
        final LinearLayout listheadermain=(LinearLayout)convertView.findViewById(R.id.listheadermain);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle.getIconName());
        headerIcon.setImageResource(headerTitle.getIconImg());
        rightarrowIcon.setImageResource(R.drawable.arrow_right);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_submenu, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.submenu);

        txtListChild.setText(childText);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

MainActivity.java:

private void prepareListData() {
        listDataHeader = new ArrayList<ExpandedMenuModel>( );
        listDataChild = new HashMap<ExpandedMenuModel, List<String>>( );

        ExpandedMenuModel item0 = new ExpandedMenuModel ( );
        item0.setIconName ("Home");
        item0.setIconImg (R.drawable.ic_round_dashboard_24);
        listDataHeader.add (item0);
        List<String> heading0 = new ArrayList<String>( );

        ExpandedMenuModel item1 = new ExpandedMenuModel ( );
        item1.setIconName ("My Account");
        item1.setIconImg (R.drawable.ic_account_circle_black_24dp);
        listDataHeader.add (item1);

        List<String> heading1 = new ArrayList<String>( );
        heading1.add("My Profile");
        heading1.add("My Family");
        heading1.add("My Consultation History");
        heading1.add("My Health Records");

        ExpandedMenuModel item2 = new ExpandedMenuModel ( );
        item2.setIconName ("COVID19");
        item2.setIconImg (R.drawable.ic_baseline_coronavirus_24);
        // Adding data header
        listDataHeader.add (item2);
        List<String> heading2 = new ArrayList<String>( );

        ExpandedMenuModel item3 = new ExpandedMenuModel ( );
        item3.setIconName ("Appointments");
        item3.setIconImg (R.drawable.ic_baseline_library_books_24);
        // Adding data header
        listDataHeader.add (item3);
        List<String> heading3 = new ArrayList<String>( );
        heading3.add ("Book Appointment");
        heading3.add ("My Appointments");
        heading3.add ("Terms & conditions");
        heading3.add ("Privacy Policy");

        ExpandedMenuModel item4 = new ExpandedMenuModel ( );
        item4.setIconName ("Speciality");
        item4.setIconImg (R.drawable.ic_account_circle_black_24dp);
        listDataHeader.add (item4);
        List<String> heading4 = new ArrayList<String>( );


        ExpandedMenuModel item5 = new ExpandedMenuModel ( );
        item5.setIconName ("Hospitals/Clinics");
        item5.setIconImg (R.drawable.ic_round_local_hospital_24);
        listDataHeader.add (item5);
        List<String> heading5 = new ArrayList<String>( );


        ExpandedMenuModel item6 = new ExpandedMenuModel ( );
        item6.setIconName ("Logout");
        item6.setIconImg (R.drawable.ic_launcher);
        listDataHeader.add (item6);
        List<String> heading6 = new ArrayList<String>( );
        listDataChild.put (listDataHeader.get (0), heading0);
        listDataChild.put (listDataHeader.get (1), heading1);
        listDataChild.put (listDataHeader.get (2), heading2);// Header, Child data
        listDataChild.put (listDataHeader.get (3), heading3);
        listDataChild.put (listDataHeader.get (4), heading4);
        listDataChild.put (listDataHeader.get (5), heading5);
        listDataChild.put (listDataHeader.get (6), heading6);
      
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got the solution

 if(isExpanded){
            rightarrowIcon.setImageResource(R.drawable.arrow_down);
        }else {
            rightarrowIcon.setImageResource(R.drawable.arrow_right);
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...