Sunday, 18 June 2017

Android: ExpandableListView item click in Activity

In this post I will show the implementation of getting the item click event of ExpandableListview in your calling Activity or Fragment.
ExpandableListView is used to group list data by categories.It is a view that shows items in a vertically scrolling two-level list. It differs from a ListView by allowing two levels which are groups that can be easily expanded and collapsed by touching to view and their respective children items.
If you are not familiar with using ExpandableListView, first refer the tutorial here.

Now to get the item click in your activity or fragment follow below steps:

1. First make an Interface for the connectivity of your item click in your activity. For this create a class IExpandableListItemClick.java and add the following code:

public interface IExpandableListItemClick {

    void onExpandedItemClick(View view, int position);

}

2. In your adapter class make a variable for your Interface as follows:

 private IExpandableListItemClick expandedItemClickListener;

3. Now in the constructor of your adapter class assign your interface as follows:

public ExpandableListAdapter(Context context, List<String> expandableListTitle, HashMap<String, String>> expandableListDetail,
                          IExpandableListItemClick listener) {
        this.context = context;
               ................                                             
        this.expandedItemClickListener = listener;
    }


4. Now in the getChildView method of your adapter class add the following code:

 @Override
    public View getChildView(int listPosition, int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.item_child_expandable_list, parent, false);
        
        // declare and initialize your view here

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                expandedItemClickListener.onExpandedItemClick(view, expandedListPosition);
            }
        });
        return convertView;
    }

5. Now in your activity or your fragment implement your interface as follows: I am using a fragment
and implement its method.

public class MainFragment extends Fragment implements IExpandableListItemClick {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
                                                     
          .......... ...............                 
                                                     
        return rootView;
    }                            
  ..... .........                 

@Override
    void onExpandedItemClick(View view, int position){   
                                                                                             
     }                                                                                      
 ...........                                                                                 
                                                                                             
}                

6. Now in the above method add a toast to see which item is clicked as follows:

@Override
    void onExpandedItemClick(View view, int position){                        
        Toast.makeText(getActivity, "Clicked item position: "+ position, Toast.LENGTH_SHORT).show();  
                                                         
     }


7. Now run your code and you can see the correct position of the item clicked in the toast.
You can change the parameters in interface method according to your requirements.

That's all.
Share it and comment if any issues.

6 comments:

  1. The Hai Di Lao menu is full of tasty options for hot pot lovers. You can pick from different meats like beef, lamb, and chicken, as well as seafood like shrimp and fish. There are also many vegetables such as mushrooms, spinach, and potatoes.

    ReplyDelete
  2. If you're looking to buy VPS Thailand, it's a smart choice for getting better online performance. A VPS, or Virtual Private Server, gives you your own space on a server, which means your website can run faster and handle more visitors.

    ReplyDelete
  3. brujos en orlando florida brujos or spiritual healers—play a significant role in the local community, especially for those seeking spiritual guidance, protection, or healing. Rooted in traditions from Latin America, the Caribbean, and African spirituality, brujos are believed to have a deep connection to the spiritual world and use their knowledge to help others.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Liam O'Leary- Raleigh's real estate market, known for his expertise in helping clients navigate the dynamic and competitive housing market in North Carolina’s capital. With a strong understanding of local neighborhoods, trends, and property values

    ReplyDelete