简体   繁体   中英

How to get the clicklistener for textview in child for expandable list?

I am currently using the custom layout for the child in Expandablelist which includes 2 different text views and an imageview. Also it will always have 1 child everytime. I am able to get the clicklistener in the adapter but not able to get this in the activity.

I have also tried to get the click listener event in the onChildClick() method but it was not working.

I am using this adapter in different activities so i want to have the click listener for one of the text view in the activity itself.

Any suggestions please.

I am able to get this done guys, i added android:onClick="textClickHandler"> in the layout file and included the listener as below in my activity:

public void textClickHandler(View view){

        if(view.getId() == R.id.textMore){
            Toast.makeText(mContext, "More Text Clicked.", Toast.LENGTH_LONG).show();
        }
    }

You can't set the listener on a View from the Activity (if I understood what you're trying to do). One way to do it would be to:

  • make your adapter have a boolean value that you set when you instantiate the adapter(This will tell you if the adapter belongs to an Activity where the TextView should have a listener(for true , for example). If the value is false then you'll make sure not to set a listener on the TextView ).
  • set the the listener on the desired TextView (only if the previous value was true , for other activities you'll simple ignore this part) in the getChildView method of your custom adapter.
  • in the onClick method for that view's OnCLickListener use the Context that you pass in the constructor of your custom adapter. Cast it to the desired activity and use it as you want. A more elegant way of doing this would be to let the desired Activity implement a custom interface(to notify you when the TextView was clicked). Then in the adapter make a field with the type of that interface that you'll set to point to your Activity when you instantiate the listener. When the TextView is clicked call the methods of that field above and notify the Activity that something happen and it should do some stuff.

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