Android-仿微信双击消息定位未读会话列表

第一部分代码:按钮双击检测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);
		}
	}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值