简体   繁体   中英

recycled edittext inside recyclerview

With RecyclerView, the off-screen EditTexts will be recycled. For example, if we have a list of 200 items, and it shows 2 items at one time, we will only ever have 2 EditText. They will reuse the higher EditText for the lower elements.

For example, here is a list that contains EditText showing only 2 at a time, and as the user scrolls, it will recycle and reuse them.

  1. EditText A
  2. Edittext B
  3. EditText C (recycled)
  4. EditText D (recycled)
  5. ....

This means we cannot just loop over all the elements later and get the values, as they don't store their values.

So, what i am asking is that is there anyway to loop over all the items and get there values even the recycled ones and save them to firebase

EDIT: I have a recyclerview with an edit text inside it the user enter numbers inside edit text and when he click save button a for loop iterate over all the recyclerview edit text and save data to Firebase

this is my fragment class

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.purchases_list_activity, container, false);
    mItem = new ArrayList<>();
    mAdapter = new PurchasesAdapter(getContext(), mItem, this);
    re = view.findViewById(R.id.itemsPListView);
    re.setLayoutManager(new LinearLayoutManager(getActivity()));
    re.setAdapter(mAdapter);



    fab = view.findViewById(R.id.fab_purchases);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < mItem.size(); i++) {
                final ItemsAdapter mylist = mItem.get(i);

              View childView = re.getChildAt(i);



                final EditText Qty = childView.findViewById(R.id.itemNewPcs);
                final String qty = Qty.getText().toString();

Toast.makeText(getContext(),qty+"",Toast.LENGTH_LONG).show();



..
}

I think that what you might need is the RecyclerView 's OnChildAttachStateChangeListener functionality. Basically, you implement your own class that implements the OnChildAttachStateChangeListener interface. Specifically, the implement the onChildViewAttachedToWindow method to do save to Firebase or add to a list that can be added to Firebase later - basically whatever you need. You just attach your custom listener via the addOnChildAttachStateChangeListener() method.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM