简体   繁体   中英

Button in ListView not clickable

I want to click on a button inside an item of a ListView and it should have the same effect from clicking the whole item. But when I click button nothing happens, when I click image its all Okey. How to fix it? I tried setting on the button: android:focusable="true" android:clickable="true" I also experimented with android:descendantFocusability.

None of my tries made the buttons clickable.

my_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:descendantFocusability="blocksDescendants"
>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_toLeftOf="@+id/imageArrow"
    android:layout_toStartOf="@+id/imageArrow"
    />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageArrow"
    android:layout_alignTop="@+id/button"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@drawable/r_arr" />
</RelativeLayout>

MainActivity.java

 public class MainActivity extends ListActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//    setContentView(R.layout.activity_main);
   ListView listView = (ListView)findViewById(R.id.listView);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
            "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
            "Linux", "OS/2" };
    MyCustomAdapter adapter = new MyCustomAdapter(this, values);
    setListAdapter(adapter);
    }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    String item = (String) getListAdapter().getItem(position);
    Toast.makeText(this, item + " selected", Toast.LENGTH_SHORT).show();
}
}

MyCustomAdapter.java

public class MyCustomAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;

static class ViewHolder {
    public TextView myButton;
    public ImageView image;
}

public MyCustomAdapter(Activity context, String[] names) {
    super(context, R.layout.my_item, names);
    this.context = context;
    this.names = names;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    // reuse views
    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.my_item, null);
        // configure view holder
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.myButton = (Button)rowView.findViewById(R.id.button);
        viewHolder.image = (ImageView)         rowView.findViewById(R.id.imageArrow);
        viewHolder.myButton.setFocusable(false);

        rowView.setTag(viewHolder);

    }

    // fill data
    ViewHolder holder = (ViewHolder) rowView.getTag();
    String s = names[position];
    String abs=Integer.toString(position + 1);
    holder.myButton.setText(s+ " number " + abs);


    holder.image.setImageResource(R.drawable.r_arr);
    return rowView;
}
}

You just have to configure onClick method something like this:

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //your code goes here
            }
        });

请在按钮上写onclicklistener。

Put button click listener in your getView

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    // reuse views
    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.my_item, null);

        // configure view holder
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.myButton = (Button) rowView.findViewById(R.id.button);
        viewHolder.image = (ImageView) rowView.findViewById(R.id.imageArrow);          
        viewHolder.myButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // Button click functionalty here   
            });

        rowView.setTag(viewHolder);

    }

    // fill data
    ViewHolder holder = (ViewHolder) rowView.getTag();
    String s = names[position];
    String abs=Integer.toString(position + 1);
    holder.myButton.setText(s+ " number " + abs);

    holder.image.setImageResource(R.drawable.r_arr);
    return rowView;

    }
}

To make your both listview item and button clickable, do this.

make ListView focusable android:focusable="true"

Button not focusable in your custom listview item android:focusable="false"

Method 1:

First replace button code in xml as below <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:focusable="false" android:onClick="onClick" android:focusableInTouchMode="false" android:layout_toLeftOf="@+id/imageArrow" android:layout_toStartOf="@+id/imageArrow" />

Then go to your activity and write code as below

public void onClick(View v)
   {
      super.onClick(v);
      if (v.getId() == R.id.button)
      {
          //your code goes here
       }
   }

or You can add below peace of code in you oncreate

Method 2 :

Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    });

It is not recommended to have two click listeners for a same row item in listview.

  1. You can have in a Listview Custom Adapter. You can customize this according to your requirement.

or

  1. You can have in your Activity / Fragment listview onclicklistener. This cannot be customized and when you click the whole row is selected.

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