I have a recycler view which has section adapter which consists of chat messages and we have ids for every message. On click of message we are able to get the ids of messages but what i want is to get the id of particular message which is currently visible on the screen. At a time only one message will be visible to the user so we need to get the id of that particular message which is present on the screen. Below is my app screenshot.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int section, int relativePosition, int absolutePosition) {
String header1 = header.get(section);
allData = mArrChildHashMap.get(header1);
String Message = allData.get(relativePosition).getMessage();
String MessageId = allData.get(relativePosition).getMessageId();
..................
...................
String ticketId = allData.get(relativePosition).getTicketId();
String AcceptFlag = checkNull(allData.get(relativePosition).getTicketAccepted());
String ExpiryTime = checkNull(allData.get(relativePosition).getExpiryTime());
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
//Ticket
itemViewHolder.Timer.setVisibility(View.VISIBLE);
itemViewHolder.ticket.setVisibility(View.VISIBLE);
itemViewHolder.ticketTitle.setText(Message);
itemViewHolder.Message.setVisibility(View.GONE);
setTicketStatus(itemViewHolder , ticketType);
if(owner.equals("YES")){
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(150, 0, 0, 0);
itemViewHolder.mLayout.setLayoutParams(lp);
if(messageType.equals("1"))
{
itemViewHolder.mLayout.setBackgroundResource(R.drawable.bubble2);
}else {
itemViewHolder.mLayout.setBackgroundResource(0);
}
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// itemViewHolder.mLayout.setGravity(Gravity.RIGHT);
itemViewHolder.Tick.setVisibility(View.VISIBLE);
itemViewHolder.Accept.setVisibility(View.INVISIBLE);
// itemViewHolder.status.setVisibility(View.VISIBLE);
if(deliveryFlag.equals("YES")){
// itemViewHolder.status.setText("Deliv");
itemViewHolder.Tick.setBackgroundResource(R.drawable.check);
}
itemViewHolder.itemLongClick(Message,section,relativePosition,longClickListener);
}else{
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 150, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
itemViewHolder.mLayout.setLayoutParams(lp);
itemViewHolder.Accept.setVisibility(View.VISIBLE);
if(messageType.equals("1"))
{
itemViewHolder.mLayout.setBackgroundResource(R.drawable.bubble1);
}else {
itemViewHolder.mLayout.setBackgroundResource(0);
}
itemViewHolder.status.setVisibility(View.GONE);
itemViewHolder.Tick.setVisibility(View.GONE);
}
itemViewHolder.Message.setText(Message);
itemViewHolder.sampleTime.setText(time);
itemViewHolder.name.setText(name);
if(name.equals("")){
itemViewHolder.name.setVisibility(View.GONE);
}else {
itemViewHolder.name.setVisibility(View.VISIBLE);
}
if(AcceptFlag.equals("1")){
itemViewHolder.Accept.setVisibility(View.GONE);
// itemViewHolder.Accept.setText("Accepted");
itemViewHolder.Timer.setVisibility(View.VISIBLE);
}
if(!ExpiryTime.equals("0")){
customRunnable = new CustomRunnable(handler,itemViewHolder.Timer,Long.parseLong(ExpiryTime));
handler.removeCallbacks(customRunnable);
customRunnable.holder = itemViewHolder.Timer;
customRunnable.millisUntilFinished = Long.parseLong(ExpiryTime);
}
itemViewHolder.itemLongClick(Message,section,relativePosition,longClickListener);
itemViewHolder.OnAcceptClick(ticketId,section,absolutePosition,onClickListener);
itemViewHolder.OnTimerClick(ticketId,section,absolutePosition,onClickListener);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, boolean header) {
View v = null;
if (header)
{
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_section1, parent, false);
return new SectionViewHolder(v);
} else {
v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_section, parent, false);
return new ItemViewHolder(v);
}
}
I have tried the below code to get the id of the message while scrolling
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
overallXScroll = overallXScroll + dx;
Log.i("check","overall->" + overallXScroll);
}
});
I am getting below output
05-12 15:08:03.451 22694-22694/com...... E/check: overall->0 05-12 15:08:03.467 22694-22694/com...... E/check: overall->0
Try method isViewPartiallyVisible from LayoutManager.
In your adapter, you can get de LayoutManager from the recyclerView, which you can get from the onAttachedToRecyclerView event.