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.

9 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
  7. 벳게임즈는 다양한 게임을 즐기고 베팅할 수 있는 온라인 사이트입니다. 카드, 주사위, 룰렛과 같은 게임들이 있으며, 실제 사람들이 게임을 진행합니다. 라이브로 게임을 시청하고 결과를 예측하는 재미가 있습니다. 겟게임즈 는 실제 카지노의 느낌을 온라인에서 경험할 수 있게 해주며, 운을 시험하고 간단한 게임으로 즐거움을 찾고 싶은 모든 사람들에게 적합한 곳입니다.
    https://kor-onca.com/category/casino-gaming/betgames/

    ReplyDelete
  8. Executive Search ist ein spezieller Personalvermittlungsdienst, der Unternehmen dabei hilft, die besten Führungskräfte für Spitzenjobs zu finden. Der Schwerpunkt liegt auf der Suche nach qualifizierten und erfahrenen Mitarbeitern für hochrangige Positionen wie Manager und Direktoren. Anders als bei der regulären Personalbeschaffung untersucht Executive Search genau, was ein Unternehmen braucht, und findet Kandidaten, die seinen Zielen und Werten entsprechen.

    ReplyDelete
  9. มีหลายวิธีในการ ดูภาพยนตร์ฟรี 2024 คุณสามารถค้นหาเว็บไซต์และบริการสตรีมมิ่งที่นำเสนอภาพยนตร์หลากหลายประเภทโดยไม่เรียกเก็บเงินใดๆ แพลตฟอร์มยอดนิยมบางแห่งยังมีส่วนที่อุทิศให้กับภาพยนตร์ฟรี ซึ่งคุณสามารถเพลิดเพลินกับประเภทต่างๆ เช่น แอ็คชั่น ตลก ดราม่า และอื่นๆ อย่าลืมใช้เว็บไซต์ที่ถูกกฎหมายและปลอดภัยเพื่อให้แน่ใจว่าได้รับประสบการณ์การรับชมที่ยอดเยี่ยม

    ReplyDelete