一、条目点击事件:setOnItemClickListener
几个参数----根据图片参照对应:
parent:背景变成淡绿色
view:背景变成随机颜色
position:弹出来
下面未点击的变色了,可见条目已正常复用
listview点击.gif
设置点击监听
mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() { /** * * @param parent ListView * @param view 条目 * @param position 位置 * @param id id 测试Id和 position一样 */ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.e(TAG, "onItemClick() called with:\n " + "parent = [" + parent + "], \n" + "view = [" + view + "],\n " + "position = [" + position + "],\n" + " id = [" + id + "]"); parent.setBackgroundColor(0x5500ff00); view.setBackgroundColor(ColUtils.randomColor()); ToastUtil.showAtOnce(ChatActivity.this, "当前位置:" + position); } });
一、条目长按事件:setOnItemLongClickListener
几个参数和点击一样
值得注意的一点是返回值:true
: 手指抬起时不触发条目的点击事件false
: 手指抬起时触发条目的点击事件
设置长按监听
mLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { ToastUtil.showAtOnce(ChatActivity.this, "长按--当前位置:" + position); return true; } });
三、滑动事件:setOnScrollListener
有两个回调方法:
onScrollStateChanged(AbsListView view, int scrollState)
onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
1.onScrollStateChanged方法:view对象滑动时蓝色,滑动停止淡绿色
listview滑动.gif
mLv.setOnScrollListener(new AbsListView.OnScrollListener() { /** * * @param view ListView * @param scrollState 上滑 1 滑动结束 0 */ @Override public void onScrollStateChanged(AbsListView view, int scrollState) { view.setBackgroundColor(scrollState == 0 ? 0x5500ff00 : 0x550000ff); } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } });
2.onScroll:android.widget.AbsListView.OnScrollListener#onScroll
/** * * @param view view ListView * @param firstVisibleItem 最顶端条目的id * @param visibleItemCount 可见的条目数 * @param totalItemCount 总条目数 */@Overridepublic void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { mIsLastItem = visibleItemCount + firstVisibleItem == totalItemCount; mIsTopItem = firstVisibleItem == 0; }
3.基于此实现监听滑到顶端和底端
滑动到上下端监听.gif
/** * 是否滑到最底端 */ private boolean mIsLastItem; /** * 是否滑到最顶端 */ private boolean mIsTopItem;
mLv.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { view.setBackgroundColor(scrollState == 0 ? 0x5500ff00 : 0x550000ff); if (scrollState == 0) { if (mIsLastItem) { ToastUtil.show(ChatActivity.this, "滑到最底端"); } if (mIsTopItem) { ToastUtil.show(ChatActivity.this, "滑到最顶端"); } } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { mIsLastItem = visibleItemCount + firstVisibleItem == totalItemCount; mIsTopItem = firstVisibleItem == 0; } });
四、条目抢占交点问题
只在一类布局上加上CheckBox,看图对比
交点抢占.gif
1.设置适配器的setData方法里可以获取条目内的布局:case 0:
CheckBox checkBox = (CheckBox) holder.getView(R.id.id_cb_left); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ToastUtil.showAtOnce(ChatActivity.this, "状态改变为:"+(isChecked?"已选择":"未选中")); } });
2.解决:让checkbox默认不抢焦点
checkBox.setFocusable(false);
抢占交点解决.gif
作者:张风捷特烈
链接:https://www.jianshu.com/p/1c836eee2507