Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add and remove items to RecyclerView in android ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 11.7k
    Comment on it

    Here I have created a RecyclerView app in android. In this app we have added item in RecyclerView and when we click on the item in RecyclerView the item will be removed. In this recycler view I have used notifyItem Inserted() and notifyItemRemoved() function, In below example I have described how to make add and remove items to recyclerview.

    Step (1)-Create xml.layout.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:autoLink="web"
            android:text="http://android-er.blogspot.com/"
            android:textStyle="bold" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <EditText
                android:id="@+id/name"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>
            <Button
                android:id="@+id/addbutton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="AddName"/>
        </LinearLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/myrecyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </LinearLayout>
    

    Step (2)-MainActivity code-

    public class MainActivity extends ActionBarActivity implements RecyclerViewAdapter.OnItemClickListener{
    
        private RecyclerView myRecyclerView;
        private LinearLayoutManager linearLayoutManager;
        private RecyclerViewAdapter myRecyclerViewAdapter;
    
        EditText name;
        Button btnAdd;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);
    
            linearLayoutManager =
                    new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
            linearLayoutManager =
                    new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
            myRecyclerViewAdapter = new RecyclerViewAdapter(this);
            myRecyclerViewAdapter.setOnItemClickListener(this);
            myRecyclerView.setAdapter(myRecyclerViewAdapter);
            myRecyclerView.setLayoutManager(linearLayoutManager);
    
            name = (EditText)findViewById(R.id.name);
            btnAdd = (Button)findViewById(R.id.addbutton);
            btnAdd.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    String newName = nameField.getText().toString();
    
                    if(!newName.equals("")){
                        if(myRecyclerViewAdapter.getItemCount()>1){
                            myRecyclerViewAdapter.add(1, newName);
                        }else{
                            myRecyclerViewAdapter.add(0, newName);
                        }
                    }
                }
            });
        }
    
        @Override
        public void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {
            Toast.makeText(this,
                    "Remove " + position + " : " + item.getItemName(),
                    Toast.LENGTH_SHORT).show();
            myRecyclerViewAdapter.remove(position);
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: