0

How can i get items inside my listview to detect on single clickevent my listview OnitemClickListener is working when the item is clicked but my listview contains an image and a button each with different actions but i have to click twice before the action of the image is fire and also click twice befor the action of the button is fired. Is there a way to detect if the image is click on the first click and then fire the action. Here is my listview onItemClick code

  listView.setOnItemClickListener((parent, view, position, id) -> {
            ImageView imgOpen = view.findViewById(R.id.pImage);
            imgOpen.setOnClickListener(v -> {
                //Action For Image Here
            });

            Button btnOpen = view.findViewById(R.id.btnOpen);
            btnOpen.setOnClickListener(v -> {
                //Action For Button Here
            });
        });

1 Answers1

0

Reference to : setOnItemClickListener on custom ListView

In the sample code above:

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             Object listItem = list.getItemAtPosition(position);

       } 
    });

Note: You should move findViewById out of onItemClick()

TrucLC
  • 17
  • 3
  • So how do i call the image and the button click events since i will need to reference it as such ```view.findViewById(R.id.pImage);``` – Serenity Emmanuel Sep 18 '21 at 05:17
  • Follow the example: https://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial You can add your button's onClick event in the Adapter's getView() method or override onClick method similar the sample code – TrucLC Sep 18 '21 at 09:29