0

Hello i`m trying to get recyclerview viewholder at particular position with this code:

  val viewHolder = educationRv.findViewHolderForAdapterPosition(i) as SectionsRecyclerAdapter.ViewHolder

the problem is that recycler view recycle not visible items and i can't get item and position 0 if its not visible. I searched before ask i tried this solutions, but can't prevent recycling of the views (i know that this is not good solution but i`m stuck and can't figure it out better) :

  educationRv.recycledViewPool.setMaxRecycledViews(1,0)

  override fun getItemViewType(position: Int): Int {
        return 1
  }

  override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (holder != null) {
            holder.setIsRecyclable(false)
   }

Generalized -> i have recyclerview with 20 edittexts which can be added dynamically, when user click save i need to get all edittexts values and save it to array, the problem comes when button save is clicked only two or three views are visible when i try to get not visible return null

Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37
  • Check my this previous answer for this functionality https://stackoverflow.com/a/47975852/7666442 && https://stackoverflow.com/a/51454770/7666442 – AskNilesh Aug 28 '19 at 06:47

1 Answers1

5

As i got your query this solution would definitely help you

In you viewHolder do like this

etText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            yourEditTextList.set(getAdapterPosition(), s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    btnAddNewEditText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            yourEditTextList.add(new String(""));
            notifyDataSetChanged();
        }
    });

Here yourEditTextList is the list of String in of your editText in your recylerView

In your activity where you have recylerView

btnDone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Fetch you list that your have passed on recycler view you will get here
        }
    });