ListView and buttons inside ListView

I want to show Button inside a ListView . The goal is to click on the ListView line or button.

Click on the line that displays additional information. Click on the button that shows more buttons below.

Same as the GMAIL app.

The checkbox is installed on the right and after clicking on the button, a button bar appears from the bottom.

My problem is to insert a button in a ListView , the button is not available. If I add to the LinearLayout using the llButton.setClickable() button, it will work. But only a button. ListView itself no longer responds to clicks!

I tried this example .

Same problem as above ...

+55
android listview button
Jun 15 2018-10-15
source share
5 answers

If you are using a custom adapter, the Click button inside the ListView will not work, so you should try using the following code to check the OnItemClickListener .

 listId.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> list, View v, int pos, long id) { // Your code for item clicks } }); 
+8
Feb 12 2018-11-12T00:
source share

Just to make it clear - and no one seems to have said something so simple - while one is not allowed to work with focus buttons in conjunction with a list, there is a much simpler solution for this.

The accepted answer is - given - you should always do this when setting up the click listener for list items, so it’s stupid that the OP didn’t know that.

If you use the XML layout as a list item, just set the following attribute for the button, and this will cause the list item to be clickable:

android:focusable="false"

+116
May 10 '12 at 7:19
source share

Add the line below to your list XML element.

 android:descendantFocusability="blocksDescendants" 

Then your list item will look like this:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:descendantFocusability="blocksDescendants" android:layout_height="wrap_content" > // Your layout objects here </RelativeLayout> 
+15
Mar 22 '14 at 20:47
source share

To fire an event when a button or list item is clicked, you can do the following:

You only process onItemClick:

 mListView.setOnItemClickListener(new ListView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> a, View v, int i, long l) { // handle click here } ); 

In the adapter, you change the button so that it cannot be clicked / focused (or do it in an XML file):

 public class MyAdapter extends BaseAdapter { ... public View getView(int position, View convertView, ViewGroup parent) { ..... Button btn = view.findViewById(R.id.button); btn.setFocusable(false); btn.setClickable(false); } } 
+7
Sep 26
source share

In my case, I had to add this attribute to listView:

 <ListView ... android:clickable="true" ... </ListView> 

And in the adapter, just add a button click listener.

 wrapper.getButtonHi().setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub DebugUtils.logDebug("Clickeado :: "+ mContact.getUserId()); } }); 

It is important to set the final variables:

 public View getRowView(final int position, View convertView, ViewGroup parent) { final BrowseContactItemWrapper wrapper; final UserModel mContact = lstContact.get(position); ..... } 
0
Sep 30 '14 at 18:28
source share



All Articles