简体   繁体   English

ListView中的按钮不可单击

[英]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. 我想单击ListView项内的按钮,并且单击整个项应具有相同的效果。 But when I click button nothing happens, when I click image its all Okey. 但是,当我单击按钮时,什么都没有发生,当我单击图像时,它全部是Okey。 How to fix it? 如何解决? I tried setting on the button: android:focusable="true" android:clickable="true" I also experimented with android:descendantFocusability. 我尝试在按钮上进行设置:android:focusable =“ true” android:clickable =“ true”我也尝试了android:descendantFocusability。

None of my tries made the buttons clickable. 我的尝试都没有使按钮可点击。

my_item.xml 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 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 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: 您只需要配置如下的onClick方法:

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 将按钮单击侦听器放入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" 使ListView可聚焦android:focusable =“ true”

Button not focusable in your custom listview item android:focusable="false" 按钮在您的自定义列表视图项目android:focusable =“ false”中无法聚焦

Method 1: 方法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" /> 首先按如下所示替换xml中的按钮代码<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 或者,您可以在oncreate的代码中添加以下代码

Method 2 : 方法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. 您可以在Listview自定义适配器中使用。 You can customize this according to your requirement. 您可以根据需要自定义。

or 要么

  1. You can have in your Activity / Fragment listview onclicklistener. 您可以在“活动/片段”列表视图中包含onclicklistener。 This cannot be customized and when you click the whole row is selected. 这不能自定义,并且当您单击整个行时,将被选中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM