简体   繁体   中英

How to implement different clicklisteners for group items in Expandablelistview in Android?

So my expandablelistview looks like(Sorry i can't post images i have less than 10 reputation).In group item layout file i have one textview & one imageview like this:

Textview         Imageview(info icon)
Textview         Imageview(info icon)

What i want is on clicking the info icon on right it should display toast for few seconds giving info about this group & if i click on textview it should expand normally displaying child items under it.

My 2 layout files look like this : Mainlayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

      <ExpandableListView
          android:id="@+id/lvexp"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:groupIndicator="@null"
          android:background="@drawable/back">
     </ExpandableListView>

</RelativeLayout>

GroupLayout.xml

<?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"
                android:orientation="horizontal" >

<TextView
    android:id="@+id/lblheader"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="Large Text"
    android:layout_weight="1.0"
    android:gravity="center"
    android:paddingTop="12dp"
    android:paddingBottom="12dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#24B9FF" />

<ImageView
    android:id="@+id/help_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/info_icon"
    android:layout_weight=".1"
     android:paddingTop="12dp"
     android:clickable="true"
    android:paddingBottom="12dp" />

How can i handle click on imageview to show toast.I have implemented onGroupClick & i am able to expand groups on click.But how can i implement click listener for Imageview?

Please help.I don't have any other alternative but to use Expandablelistview in app.

How you are attaching this groupLayout xml to your adapter..actually you have to implement method getGroupView() inside your ExpandableListAdapter . There you can inflate this layout and access all the views from this layout and write click listeners for this layout..

Ok..this is the sample getGroupView() implementing clicklisteners for your reference:

@Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            View v = convertView;
            if(v == null){
                v= mInflator.inflate(R.layout.group_layout, null);
            }

            TextView text = (TextView )v.findViewById(R.id.your text id);
            ImageView image = (ImageView)v.findViewById(R.id.your image id);


          text .setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ///your code here
       }
        }
    });



       image .setOnClickListener(new OnClickListener() {
            @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ///your code here
       }
        }
    });

            return v;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM