Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to remove CardView item in the RecyclerView list

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.53k
    Comment on it

    In the below example I have created a RecyclerView, In RecyclerView I have added CardView item. When you click on FloatingActionButton a dialog box will open on screen where user will fill name, address and gender and then finally click on ok button, on clicking ok button the user details will get added on RecyclerView list , and when you need to remove any item(CarView), In recyclerView list you have to will click on that paticuler cardItem Image,  it will remove items easily.
     
    Here in below example, First I have added RecyclerView , CardView and design support  library in  build.gradle file. Then  I have created RecyclerView and FloatingActionButton within Framelayout in main_activity.xml layout, In next step I have created a new dialog.xml layout here I have added TextView, EditView, Button and Spinner. After then in fourth step I have created a new item.xml layout here I have added CardView, ImageView and TextView. Now see programming area here I have created emp class  and MyAdapter class, in MyAdapter class I have created ViewHolder class and In MainActivity I have used DialogBox fuction. You can see below program it will clearly describes "how to remove CardView item in the RecyclerView list?".

     

    Step(1)-I have added RecyclerView, CardView and design library in build.gradle file -

    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile 'com.android.support:cardview-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'
    
    

     

    Step(2)activity_main.xml -

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        tools:context=".MainActivity">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyle_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="10dp"
            android:src="@mipmap/one"
            app:fabSize="normal" />
    
    </FrameLayout>

     

    Step(3)Created a new dialog.xml layout-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
              android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:gravity="center"
                android:text="UserName"/>
            <EditText
                android:id="@+id/name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:inputType="textCapSentences"/>
    
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
           <TextView
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:layout_weight="0.5"
               android:gravity="center"
               android:text="Address"/>
            <EditText
                android:id="@+id/address"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:inputType="text"/>
    
        </LinearLayout>
    
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="0.5"
                android:gravity="center"
                android:text="Gender"/>
            <Spinner
                android:id="@+id/gender"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:inputType="text"/>
    
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
           <Button
               android:id="@+id/btn_ok"
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:layout_gravity="center"
               android:layout_weight="0.5"
               android:gravity="center"
               android:text="Ok"/>
            <Button
                android:id="@+id/btn_cancel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="cancel"/>
    
        </LinearLayout>
    </LinearLayout>

     

    Step(4)-Created a new item.xml layout-

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:cardview="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/mainLayout"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:id="@+id/image"
                android:padding="5dp"
                android:src="@mipmap/ic_launcher"/>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text=""
                    android:textColor="@android:color/holo_blue_dark"/>
                <TextView
                    android:id="@+id/address"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/name"
                    android:text=""
                    android:textColor="@android:color/holo_green_dark"
                    android:textStyle="bold" />
    
            </RelativeLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

     

    Step(5)Created Friend class-

      public class Friend {
        private String name;
        private String address;
        private boolean gender;
    
        public Friend(String name,boolean gender,String address){
            this.name=name;
            this.gender=gender;
            this.address=address;
    
        }
        public String getName(){
            return  name;
        }
        public Boolean isGender(){
            return gender;
        }
        public String getAddress(){
            return address;
    
        }
    
    }

     

    Step(6)-Created MyAdapter class-

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    
        private List<Friend> friends;
        private Activity activity;
        int mLastPosition = 0;
        private RemoveClickListner mListner;
    
        public MyAdapter(Activity activity, List<Friend> friends,RemoveClickListner listner) {
            this.friends = friends;
            this.activity = activity;
            mListner=listner;
    
        }
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    
            LayoutInflater inflater =activity.getLayoutInflater();
           final View view =inflater.inflate(R.layout.item_recycler, viewGroup, false);
    
            ViewHolder holder = new ViewHolder(view, new RemoveClickListner() {
                @Override
                public void OnRemoveClick(int index) {
                    mListner.OnRemoveClick(index);
                }
            });
    
            return holder;
        }
    
        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder viewHolder,final int position) {
            final ViewHolder holder =(ViewHolder)viewHolder;
            holder.setItemDetails(friends.get(position),position);
            mLastPosition =position;
            viewHolder.name.setText(friends.get(position).getName());
            viewHolder.address.setText(friends.get(position).getAddress());
            if (friends.get(position).isGender()) {
                viewHolder.imageView.setImageResource(R.drawable.male);
            }else {
                viewHolder.imageView.setImageResource(R.drawable.female);
    
            }
            viewHolder.container.setOnClickListener(onClickListener(position));
        }
        private void setDataToView(TextView name,TextView address,ImageView genderIcon,int position){
            name.setText(friends.get(position).getName());
            address.setText(friends.get(position).getAddress());
            if (friends.get(position).isGender()) {
                genderIcon.setImageResource(R.drawable.male);
            }else{
                genderIcon.setImageResource(R.drawable.female);
    
            }
    
        }
    
        @Override
        public int getItemCount() {
            return (null !=friends?friends.size():0);
        }
        private View.OnClickListener onClickListener(final int position){
            return new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Dialog dialog = new Dialog(activity);
                    dialog.setContentView(R.layout.item_recycler);
                    dialog.setTitle("Position" + position);
                    dialog.setCancelable(true);
    
                    TextView name =(TextView)dialog.findViewById(R.id.name);
                    TextView address=(TextView)dialog.findViewById(R.id.address);
                    ImageView icon = (ImageView) dialog.findViewById(R.id.image);
                    setDataToView(name,address,icon,position);
                    dialog.show();
                }
            };
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            private ImageView imageView;
            private TextView name;
            private TextView address;
            private View container;
            private LinearLayout mainLayout;
            private RemoveClickListner mListner;
    
            public ViewHolder(final View view, RemoveClickListner removeClickListner) {
                super(view);
                Context context;
                this.mListner =removeClickListner;
                imageView = (ImageView) view.findViewById(R.id.image);
                name = (TextView) view.findViewById(R.id.name);
                address = (TextView) view.findViewById(R.id.address);
                container = view.findViewById(R.id.card_view);
                mainLayout = (LinearLayout) view.findViewById(R.id.mainLayout);
                mainLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(itemView.getContext(), "Position:" + Integer.toString(getPosition()), Toast.LENGTH_SHORT).show();
                    }
                });
            }
    
            public void setItemDetails(Friend friend, final int position) {
                name.setText(friend.getName());
                address.setText(friend.getAddress());
                imageView.setOnClickListener(new AdapterView.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mListner.OnRemoveClick(position);
                    }
                });
            }
        }
    
    }

     

    Step(7)MainActivity-

    public class MainActivity extends AppCompatActivity {
    
        private RecyclerView recyclerView;
        private MyAdapter adapter;
        private ArrayList<Friend>friendArrayList;
        private FloatingActionButton feb;
        private  boolean gender;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            friendArrayList = new ArrayList<>();
            recyclerView =(RecyclerView)findViewById(R.id.recyle_view);
            feb=(FloatingActionButton)findViewById(R.id.fab);
            recyclerView.setHasFixedSize(true);
    
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            recyclerView.setLayoutManager(layoutManager);
    
            setRecyclerViewData();
    
            adapter = new MyAdapter(this, friendArrayList, new RemoveClickListner() {
                @Override
                public void OnRemoveClick(int index) {
                    friendArrayList.remove(index);
                    adapter.notifyDataSetChanged();
                }
            });
            recyclerView.setAdapter(adapter);
            feb.setOnClickListener(onAddingListener());
        }
        private void setRecyclerViewData(){
            friendArrayList.add(new Friend("Sumit", false, "Name"));
            friendArrayList.add(new Friend("Address", true, "Kanpur"));
            friendArrayList.add(new Friend("Inder", false, "Name"));
            friendArrayList.add(new Friend("Address", true, "Kanpur"));
        }
        private View.OnClickListener onAddingListener(){
            return new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    final Dialog dialog = new Dialog(MainActivity.this);
                    dialog.setContentView(R.layout.dialog);
                    dialog.setTitle("Add a new friend");
                    dialog.setCancelable(false);
    
                    EditText name =(EditText)dialog.findViewById(R.id.name);
                    EditText address=(EditText)dialog.findViewById(R.id.address);
                    Spinner spnGender =(Spinner)dialog.findViewById(R.id.gender);
                    View btnAdd =dialog.findViewById(R.id.btn_ok);
                    View btnCancel = dialog.findViewById(R.id.btn_cancel);
                    ArrayList<String>gendersList = new ArrayList<>();
                    gendersList.add("Male");
                    gendersList.add("Female");
                    ArrayAdapter<String>spnAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_dropdown_item,gendersList);
                    spnGender.setAdapter(spnAdapter);
                    spnGender.setOnItemSelectedListener(onItemSelectedListener());
                    btnAdd.setOnClickListener(onConfirmListener(name, address, dialog));
                    btnCancel.setOnClickListener(onCancelListener(dialog));
                    dialog.show();
    
                }
    
            };
        }
        private AdapterView.OnItemSelectedListener onItemSelectedListener(){
            return new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (position == 0){
                        gender = true;
                    }
    
                    else
    
                    {
                        gender = false;
                    }
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
    
                }
            };
    
        }
        private View.OnClickListener onConfirmListener(final EditText name,final EditText address,final Dialog dialog){
            return new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Friend friend = new Friend(name.getText().toString().trim(),gender,address.getText().toString().trim());
                    friendArrayList.add(friend);
                    adapter.notifyDataSetChanged();
                    dialog.dismiss();
    
                }
            };
        }
    
        private View.OnClickListener onCancelListener(final Dialog dialog){
            return new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            };
        }
    
    
    }

     

 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: