新手总结,有不对的地方欢迎指正~~~~~~~~~~
之间对listview和button进行事件监听时,只能实现button的事件监听
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MainActivity.this,"点击了item=="+i,Toast.LENGTH_SHORT).show(); } });
public class ViewHolder implements View.OnClickListener{ private Button but; public ViewHolder(View ConvertView) { but= ((Button) ConvertView.findViewById(R.id.button)); but.setOnClickListener(this); } @Override public void onClick(View view) { String str=but.getText().toString().trim(); Toast.makeText(getContext(),"点击了button"+str,Toast.LENGTH_SHORT).show(); Log.i("button","点击了button"+str); } }在item里面添加button,item会失去焦点,事件分发会继续向底层分发被button消费掉!
若是想item和button分别实现事件监听需要对button添加属性设置
如下:
<Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:focusable="false" android:clickable="true"/>其中也可以为button的父布局添加可点击属性:
整个xml文件的根元素如LinearLayout中添加属性android:descendantFocusability="blocksDescendants"