如何在滚动时使用回收器视图识别部分适配器的子项的位置

时间:2018-05-12 09:42:21

标签: android android-recyclerview recycler-adapter recyclerview-layout sectionedrecyclerviewadapter

我有一个回收站视图,其中包含由聊天消息组成的段适配器,我们为每条消息都有id。点击消息,我们可以获得消息的ID,但我想要的是获取当前在屏幕上可见的特定消息的ID。一次只有一条消息对用户可见,因此我们需要获取屏幕上显示的特定消息的ID。以下是我的应用截图。

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);
        }

    }

我已尝试使用以下代码在滚动时获取消息的ID

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);

        }
    });

我的输出低于输出

  

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

1 个答案:

答案 0 :(得分:0)

从LayoutManager尝试方法isViewPartiallyVisible

在适配器中,您可以从recyclerView获取de LayoutManager,您可以从onAttachedToRecyclerView事件获取该文件。