Adding Items in RecycleView Dynamically using a button

前端 未结 3 1485
别那么骄傲
别那么骄傲 2020-12-16 08:42

I have a button(save contact) to save contacts ,the button when pressed get name and email from edit text and should dynamically add 1 list item in recycle view

The

相关标签:
3条回答
  • 2020-12-16 09:15

    layout

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvContactNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="false"
        android:overScrollMode="never"
        tools:itemCount="2"
        tools:listitem="@layout/row_add_contact_number" />
    

    java

    private ArrayList<ContactNumberModel> arrayList;
    private ContactNumberAdapter adapter;
    

    onCreate

    arrayList = new ArrayList<>();
    arrayList.add(new ContactNumberModel());
    adapter = new ContactNumberAdapter(context);
    
    binding.rvContactNumber.setLayoutManager(new LinearLayoutManager(context));
    binding.rvContactNumber.setHasFixedSize(true);
    binding.rvContactNumber.setAdapter(adapter);
    

    ContactNumberModel.java

    public class ContactNumberModel {
        private String contactNo;
    
        public String getContactNo() {
            return contactNo;
        }
    
        public void setContactNo(String contactNo) {
            this.contactNo = contactNo;
        }
    }
    

    ContactNumberAdapter.java

    public class ContactNumberAdapter extends RecyclerView.Adapter<ContactNumberAdapter.ViewHolder> {
    
        private Context context;
        private ArrayList<ContactNumberModel> arrayList;
    
        public ContactNumberAdapter(Context context) {
            this.context = context;
            this.arrayList = new ArrayList<>();
            this.arrayList.add(new ContactNumberModel());
        }
    
        @NonNull
        @Override
        public ContactNumberAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            RowAddContactNumberBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.row_add_contact_number, parent, false);
            return new ViewHolder(binding);
        }
    
        @Override
        public void onBindViewHolder(@NonNull final ContactNumberAdapter.ViewHolder holder, int position) {
            ContactNumberModel model = arrayList.get(position);
            //holder.bind(model);
            holder.binding.etContactNumber.setText("");
    
            holder.binding.etContactNumber.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                }
    
                @Override
                public void afterTextChanged(Editable editable) {
                    arrayList.get(holder.getAdapterPosition()).setContactNo(editable.toString());
                }
            });
    
            if (arrayList.size() - 1 == holder.getAdapterPosition()) {
                holder.binding.tvAddMore.setVisibility(View.VISIBLE);
                holder.binding.ivRemove.setVisibility(View.GONE);
            } else {
                holder.binding.tvAddMore.setVisibility(View.GONE);
                holder.binding.ivRemove.setVisibility(View.VISIBLE);
            }
    
            holder.binding.ivRemove.setOnClickListener(null);
            holder.binding.tvAddMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    addNewRow();
                }
            });
        }
    
        public void addNewRow() {
            this.arrayList.add(new ContactNumberModel());
            notifyDataSetChanged();
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            RowAddContactNumberBinding binding;
    
            public ViewHolder(RowAddContactNumberBinding binding) {
                super(binding.getRoot());
                this.binding = binding;
            }
    
            public void bind(ContactNumberModel model) {
                binding.setContact(model);
                binding.executePendingBindings();
            }
        }
    }
    

    row_add_contact_number.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    
        <data>
    
            <variable
                name="contact"
                type="com.appname.model.ContactNumberModel" />
        </data>
    
        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_10sdp"
            android:orientation="vertical">
    
            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/_15sdp"
                android:layout_marginRight="@dimen/_15sdp"
                android:gravity="center"
                android:orientation="horizontal">
    
                <com.google.android.material.textfield.TextInputLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:theme="@style/TextInputLayoutHint">
    
                    <androidx.appcompat.widget.AppCompatEditText
                        android:id="@+id/etContactNumber"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:clickable="false"
                        android:focusableInTouchMode="true"
                        android:hint="Enter Contact Number"
                        android:inputType="number"
                        android:maxLines="1" />
    
                </com.google.android.material.textfield.TextInputLayout>
    
                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/tvAddMore"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/_3sdp"
                    android:layout_marginLeft="@dimen/_3sdp"
                    android:fontFamily="@font/font_barlow"
                    android:gravity="center"
                    android:text="@string/add_more"
                    android:textColor="@color/colorOrange"
                    android:textSize="@dimen/_12ssp"
                    android:textStyle="bold" />
    
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/ivRemove"
                    android:layout_width="@dimen/_15sdp"
                    android:layout_height="@dimen/_15sdp"
                    android:layout_marginLeft="@dimen/_5sdp"
                    android:adjustViewBounds="true"
                    android:src="@drawable/ic_logo"
                    android:visibility="gone" />
    
            </androidx.appcompat.widget.LinearLayoutCompat>
    
            <View style="@style/Divider" />
    
        </androidx.appcompat.widget.LinearLayoutCompat>
    
    </layout>
    
    0 讨论(0)
  • 2020-12-16 09:17

    You don't need to go through the entire life cycle of your Activity or Fragment just because you've made changes to the list. Instead try adding the item in the list you currently have associated with the Adapter. And then call adapter.notifyDataSetChange(). This should automatically add the new Item to the RecyclerView.

    0 讨论(0)
  • 2020-12-16 09:24

    Add this method to your adapter and call on button click.

    public void newAddeddata(String company_name){
            NewleadsPOJO newValue=new NewleadsPOJO();
            newValue.setLeads_company(company_name);
            datalist.add(newValue);
            notifyDataSetChanged();
        }
    

    Add following method to NewLeadFrag

    public NewleadsAdapter getAdapter(){
       return adapter;
    }
    

    now in Createmeetingfrag

    leads.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NewLeadFrag fragment = getFragmentManager().findFragmentByTag("NewLeadFrag_TAG"); //set tag of fragment when you add with fragment manager. and if you are using support library use getSupportFragmentManager()
                   if(fragment!= null){
                        fragment.getAdapter().newAddeddata(company_name.getText().toString());
                    }
    
                }
            });
    
    0 讨论(0)
提交回复
热议问题