效果图:
activity的布局xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.testlistviewandbutton.MainActivity" > <ListView android:id="@+id/lv_main" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" android:gravity="center_horizontal" android:orientation="horizontal" android:weightSum="2" > <!-- android:descendantFocusability="blocksDescendants" 根布局的这个属性是重点 --> <Button android:id="@+id/btn_item" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> </LinearLayout>
package com.example.testlistviewandbutton; import java.util.List; import android.content.Context; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.Toast; public class MyAdapter extends BaseAdapter { private Context context; private List<String> str_list; public MyAdapter(Context context, List<String> str_list) { this.context = context; this.str_list = str_list; } @Override public int getCount() { return str_list == null ? 0 : str_list.size(); } @Override public String getItem(int position) { return str_list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { viewHoler = new ViewHoler(); convertView = View.inflate(context, R.layout.listview_item, null); viewHoler.btn_item = (Button) convertView .findViewById(R.id.btn_item); convertView.setTag(viewHoler); } else { viewHoler = (ViewHoler) convertView.getTag(); } viewHoler.btn_item.setText(getItem(position)); viewHoler.btn_item.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "点击了Button按钮", Toast.LENGTH_SHORT) .show(); } }); return convertView; } private ViewHoler viewHoler; public static class ViewHoler { Button btn_item; } }
package com.example.testlistviewandbutton; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; public class MainActivity extends Activity { private ListView lv_main; private List<String> str_list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { // TODO Auto-generated method stub lv_main = (ListView) findViewById(R.id.lv_main); str_list = new ArrayList<String>(); for(int i=0;i<20;i++) { str_list.add("这是第"+i+"个按钮.."); } MyAdapter adapter = new MyAdapter(MainActivity.this, str_list); lv_main.setAdapter(adapter); //listview的短按监听 lv_main.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(MainActivity.this, "点击了listview的onitem监听", Toast.LENGTH_SHORT) .show(); } }); //listview的长按监听 lv_main.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(MainActivity.this, "点击了listview的onitem长按监听", Toast.LENGTH_SHORT) .show(); return true;//这里默认返回false,但是会长按之后接着响应短按了,返回true问题解决 } }); } }
这里有三种解决方案
1.将ListView中的Item布局中的子控件focusable属性设置为false
2.在getView方法中设置button.setFocusable(false)
3.设置item的根布局的属性android:descendantFocusability="blocksDescendant"
我们可以发现,其实这三种方法都是为了让Button等控件不能获取焦点,从而使得item可以响应点击事件。
第三种方法使用起来相对方便,因为它是将item布局中的其他所有控件都设置为不能获取焦点。
android:descendantFocusability属性共有三个取值,分别为
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup 只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup 会覆盖子类控件而直接获得焦点