解决ListView的item监听事件个子控件监听事件冲突问题
来源:互联网 发布:淘宝订单不能批量删除 编辑:程序博客网 时间:2024/05/17 04:43
当我们自定义ListView的Adaper的时候,给我们item布局添加监听事件,然后我再在去在Activity中给我们的ListView添加AdapterView.OnItemClickListener()的监听事件的时候,我们会发现,我们只能检测到我们子控件的监听事件,而不能都对我们的item进行监听了。这是因为我们在Activity中添加了button和checkbox这些控件,此时这些子控件会将焦点获取到,所以常常当点击item时变化的是子控件,item本身的点击没有响应。针对这种情况,我们可以强行设置item获取焦点,在我们item布局中我们可以设置blocksDescendants属性来解决获取我们item的焦点,属性有三种值,beforeDescendants:viewgroup会优先其子类控件而获取到焦点afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点通常我们需要获取item焦点时,我们会在Item布局的根布局中添加:`android:descendantFocusability="blocksDescendants"或者我们将可能会获取我们item焦点的Button或者ImageButton,CheckBox等等控件的focusable属性设置为false。
下面我们通过代码来体会一下
这是我们的ListView住布局
<?xml version="1.0" encoding="utf-8"?><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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.create.testlist.MainActivity"> <ListView android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
然后这是我们的Item布局
<?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:orientation="horizontal"> <TextView android:id="@+id/textViewId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="nihao" /> <ImageButton android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:contentDescription="@string/app_name" android:focusable="false" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="press" android:focusable="false" /></LinearLayout>
然后这是我们的适配器
package com.create.testlist;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Objects;/** * Created by 24540 on 2016/4/25. */public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater; private ArrayList<HashMap<String, Object>> date; private Context mContext; public MyAdapter(Context context) { getDate(); mContext = context; mInflater = LayoutInflater.from(context); } private void getDate() { date = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < 10; i++) { HashMap<String, Object> item = new HashMap<String, Object>(); item.put("name", "test" + i); item.put("con", "com" + i); date.add(item); } } @Override public int getCount() { return date.size(); } @Override public Object getItem(int position) { return date.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { holder p = null; if (convertView == null) { p = new holder(); convertView = mInflater.inflate(R.layout.layout_item, null); p.text = (TextView) convertView.findViewById(R.id.textViewId); p.bu1 = (Button) convertView.findViewById(R.id.button); p.image = (ImageButton) convertView.findViewById(R.id.imageView); p.bu1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "显示了" + position, Toast.LENGTH_SHORT).show(); } }); convertView.setTag(p); } else { convertView.getTag(); } return convertView; } class holder { TextView text; ImageButton image; Button bu1; }}
最后就是我们的MainAcitivty,在里面添加我们的item监听事件
package com.create.testlist;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends AppCompatActivity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView)findViewById(R.id.list); MyAdapter adapter = new MyAdapter(this); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this,"test"+position,Toast.LENGTH_SHORT).show(); } }); }}
当我们运行时我们可以发现我们可以获取我们的item的监听事件了。
运行结果如下
注意这里我们在Button和ImaeButton里面设置的focusable属性。
ps,还要注意一点就是,当item布局里面有子布局时,如果在Adapter中设置了子布局的监听事件,则我们还是无法获取item的焦点,因为Item中的布局先获取了我们的焦点。
1 0
- 解决ListView的item监听事件个子控件监听事件冲突问题
- ListView的Item布局子控件与Item的setOnItemClickListenter监听事件冲突失效的解决方法
- 解决Listview的item和里面控件点击事件冲突
- ListView的item单击事件与item布局中控件单击事件的冲突问题
- 监听listview中item有控件点击事件的变化,更新listview
- listview的监听事件
- listview或gridview中item的点击事件与控件点击事件冲突的解决
- Android中解决listview的item点击事件和子控件点击事件的冲突
- 解决ListView中Item的子控件与Item点击事件冲突
- 解决ListView中Item的子控件(比如Button)与Item点击事件冲突
- 解决ListView中Item的子控件与Item点击事件冲突
- 控件的监听事件。
- ListView Item点击监听事件失效
- ListView中控件点击事件与Item点击事件冲突的问题
- 关于ListView中控件点击事件与Item点击事件冲突的问题
- 关于ListView中控件点击事件与Item点击事件冲突的问题
- 关于ListView中控件点击事件与Item点击事件冲突的问题
- ExpandableListView中ListView无法使用Item的监听事件
- 根据select下拉框选择的值,向form表单添加所要显示的内容的方法
- android library projects cannot be launched
- c++ 暂停方法
- android 蓝牙打印和网络打印
- jquery和amd规范
- 解决ListView的item监听事件个子控件监听事件冲突问题
- Logcat不显示log解决方法
- poj3061Subsequence
- 计算机存储的大端小端问题
- c++第四次实验-项目三:乘法口诀
- JS取整的一些方法
- Spring 注解
- 机器学习基石第八讲:noise and error
- 区块链挑战传统支付体系