首页 > 系统 > Android > 正文

Android ListView selected默认选中第一个position解决办法

2019-11-09 18:18:14
字体:
来源:转载
供稿:网友

最近在做一个实验键盘完成一个购物的流程,其中就涉及到使用键盘方向键控制ListView中的Item选中状态。本以为通过实现OnItemSelectedListener接口就可以实现功能,事实上并不能满足需求。 出现的问题是这样的: 每次进入页面,都会选中第一条,但是这时按下方向键“下”也会选中第一条,但是这时候却不会出发OnItemSelectedListener 接口中的onItemSelected方法,所以使用方向键“下”首次无法监听到事件。 到处百度、谷歌了很久未果,就自定义了一个ListView,解决了问题。 之所以出现默认选中position==0,肯定出在焦点问题上。虽然不知道google当时为什么默认选中第一个,但是确实给开发者造成了一定的困扰,有时候我们不想要那么智能,灵活性好我们能做的会更多更好。既然是焦点问题,我们就要重写PRotected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)方法,出现问题的地方只是在position==0和position==size-1,即首末两个item,我们只需要关注这两个即可,也就是getSelectedItemPosition() == 0 || getSelectedItemPosition() == getCount()-1再去执行我们处理的方法就行了。

思路有了,下面上代码:package com.wz.bubbleshop.widget;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Rect;import android.util.AttributeSet;import android.util.Log;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;/** * 兼容键盘方向键选取及Enter键点击事件 * @author zhangzhenguo * */public class CustomNoScrollListView extends ListView{ private EventListener mEventListener; private CustomItemSelectedListener mCustomItemSelectedListener; private CustomHoverListener mHoverListener; /** * 当前选中的item */ private int mSelectPosition = -1; public CustomNoScrollListView(Context context) { this(context,null); } public CustomNoScrollListView(Context context, AttributeSet attrs) { super(context, attrs); mCustomItemSelectedListener = new CustomItemSelectedListener(); mHoverListener = new CustomHoverListener(); } public CustomNoScrollListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mCustomItemSelectedListener = new CustomItemSelectedListener(); }// @Override// public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);// super.onMeasure(widthMeasureSpec, expandSpec);// } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (mEventListener != null) { if (mEventListener != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { mEventListener.onEnterEvent(mSelectPosition); } } return super.dispatchKeyEvent(event); } @Override protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); if (mEventListener == null) { return; } if (gainFocus) {// TODO 为了解决selected事件首次进入即position==0时不执行的问题 if (getSelectedItemPosition() == 0 || getSelectedItemPosition() == getCount()-1) { final int[] location = new int[2]; getSelectedView().getLocationOnScreen(location); mEventListener.onItemSelected(getSelectedItemPosition(),location[1]); } }else { mEventListener.onNextMenu(getSelectedItemPosition()); } } public void setOnEnterEventListener(EventListener eventListener){ this.mEventListener = eventListener; this.setOnItemSelectedListener(mCustomItemSelectedListener); this.setOnHoverListener(mHoverListener); } public EventListener getOnEnterEventListener(){ return this.mEventListener; } public interface EventListener{ /** * Enter键监听 * @param position */ void onEnterEvent(int position); /** * item selected监听 * @param position * @param rawY */ void onItemSelected(int position,float rawY); /** * 离开ListView时,记录当前选中的position * @param position */ void onNextMenu(int position); void onHoverExit(); } public class CustomItemSelectedListener implements OnItemSelectedListener{ @SuppressLint("NewApi") @Override public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) { mSelectPosition = position; if (mEventListener != null && isFocused()) { final int[] location = new int[2]; view.getLocationOnScreen(location); mEventListener.onItemSelected(position,location[1]); } } @Override public void onNothingSelected(AdapterView<?> parent) { mSelectPosition = -1; } } public class CustomHoverListener implements OnHoverListener{ @Override public boolean onHover(View v, MotionEvent event) { // TODO Auto-generated method stub if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT) { if (mEventListener != null) { mEventListener.onHoverExit(); } } return false; } }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表