第一部分代码:按钮双击检测500毫秒内点击两次认为是双击
private long firstPressTime = 0;
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
long now = System.currentTimeMillis();
if ((now - firstPressTime) > 500) {
firstPressTime = now;
} else {
chatFragment.click();
}
}
});
第二部分代码:双击事件代码处理
public void click() {
int position = mMessageAdapter.findReadPosition(lvMessage);
smoothMoveToPosition(position);
}
第三部分:查找未读消息位置
public int findReadPosition(SlideListView recyclerView){
int position = 0;
int size = this.getCount();
/** 列表中第一个可见的item position */
int firstPosition = recyclerView.getFirstVisiblePosition();
int lastPosition = recyclerView.getLastVisiblePosition();
/** 定位第一个未读消息的position */
int lastNumPosition = -1,firstNumPosition = 0;
for (int i = size - 1;i >= 0;i--){
if (this.getItem(i).getUnreadCount() > 0){
lastNumPosition = i;
break;
}
}
for (int i = 0;i < size;i++){
if (this.getItem(i).getUnreadCount() > 0){
firstNumPosition = i;
break;
}
}
/**************** end *****************/
for (int i = 0;i < size;i++){
int num = this.getItem(i).getUnreadCount();
/** 从可见的position开始往下查找 */
if (i > firstPosition && num > 0 && lastPosition != size-1) {
position = i;
break;
}else if (lastNumPosition == i){//如果是已经定位到最后一条未读position了,返回第一条未读position
position = firstNumPosition;
}
}
return position;
}
第四部分:滚动到指定位置
/**
* 滑动到指定位置
*/
private void smoothMoveToPosition(int position) {
int firstItem = lvMessage.getPositionForView(lvMessage.getChildAt(0))/*getChildLayoutPosition(recyclerView.getChildAt(0))*/;
int lastItem = lvMessage.getPositionForView(lvMessage.getChildAt(lvMessage.getChildCount() - 1))/*getChildLayoutPosition(recyclerView.getChildAt(recyclerView.getChildCount() - 1))*/;
if (position < firstItem) {//往上定位
// 第一种可能:跳转位置在第一个可见位置之前,使用smoothScrollToPosition
lvMessage.smoothScrollToPosition(position);
} else if (position <= lastItem) {//往下定位
// 第二种可能:跳转位置在第一个可见位置之后,最后一个可见项之前
int movePosition = position - firstItem;
if (movePosition >= 0 && movePosition < lvMessage.getChildCount()) {
int top = lvMessage.getChildAt(movePosition).getTop();
lvMessage.smoothScrollBy(top+2, 5);
}
}else{//未读item是最后一个的时候
lvMessage.smoothScrollToPosition(position);
}
}