Friday, 17 February 2017

spinner in recyclerview android

spinner in recyclerview android


Hi friends!! hope you are guys doing awesome!! Here's complete guide about using spinner in recyclerview.

Please follow the steps below to get desired outcome

1. The very first step would be to create a new Android Studio Project. As it is quite obvious, i won't show up here but if anyone finds any difficulties, do let me know.

2. After creating New Project, open build.gradle from your app and then paste these lines of code. You may have different versions of gradle files as per your gradle libraries installed.

compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.android.support:recyclerview-v7:24.2.1'

3. Now open your activity_main.xml and paste these lines of codes.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.abhishek.spinnerinrecyclerview.MainActivity">
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

4. Now open your MainActivity.class and paste these codes.


package com.abhishek.spinnerinrecyclerview;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

    Spinner spinner;
    private Context mContext;
    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        
        recycler = (RecyclerView)findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(mContext));
        UserAdapter userAdapter = new UserAdapter(mContext, User.getUserList());
        recycler.setAdapter(userAdapter);

    }



}

5. Now create UserAdapter class for displaying recycler view data and paste these codes.


package com.abhishek.spinnerinrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import java.util.List;

/**
 * Created by Abhishek on 16/02/17.
 */

public class UserAdapter extends RecyclerView.Adapter<UserViewHolder> {

    private Context mContext;
    private List<User> userList;
    private ArrayAdapter<Customer> dataAdapter;
    public UserAdapter(Context context, List<User> userList){
        this.mContext = context;
        this.userList = userList;

        dataAdapter = new ArrayAdapter<Customer>(mContext,
                android.R.layout.simple_spinner_item, Customer.fillCustomer());
        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    }

    @Override
    public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        UserViewHolder userViewHolder = new UserViewHolder(view);
        return userViewHolder;
    }

    @Override
    public void onBindViewHolder(UserViewHolder holder, int position) {
        holder.tv_1.setText(""+userList.get(position).getUserId());
        holder.tv_2.setText(userList.get(position).getUserName());
        holder.spinner.setAdapter(dataAdapter);
    }

    @Override
    public int getItemCount() {
        return userList.size();
    }
}

6. Now create UserViewHolder class and paste these codes


package com.abhishek.spinnerinrecyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Created by Abhishek on 16/02/17.
 */
public class UserViewHolder extends RecyclerView.ViewHolder{

    public TextView tv_1;
    public TextView tv_2;
    public Spinner spinner;

    public UserViewHolder(View itemView) {
        super(itemView);
        tv_1 = (TextView)itemView.findViewById(R.id.tv_1);
        tv_2 = (TextView)itemView.findViewById(R.id.tv_2);

        spinner = (Spinner)itemView.findViewById(R.id.spinner);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // On selecting a spinner item
                String item = parent.getItemAtPosition(position).toString();
                // Showing selected spinner item
                //Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }
}

7. Now create User class for setting recycler data.


package com.abhishek.spinnerinrecyclerview;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Abhishek on 16/02/17.
 */

public class User {

    public int userId;
    public String userName;


    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public static List<User> getUserList(){
        List<User> userList = new ArrayList<>();
        for(int i=0; i<200000; i++){
            User user = new User();
            user.setUserId(0+i);
            user.setUserName("Name: Ashu"+i);
            userList.add(user);
        }
        return userList;
    }

}

8. Now create Customer class for displaying spinner data.


package com.abhishek.spinnerinrecyclerview;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Abhishek on 16/02/17.
 */

public class Customer {

    public String id;
    public String customerName;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public static List<Customer> fillCustomer(){
        List<Customer> customerList = new ArrayList<>();
        for(int i=0; i<1000; i++){
            Customer customer = new Customer();
            customer.setId(""+i);
            customer.setCustomerName("Name"+i);
            customerList.add(customer);
        }
        return customerList;

    }

    @Override
    public String toString() {
        return customerName;
    }
}

9. Now create recycler_item.xml in layout and paste these code.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:padding="20dp"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:text="text 1"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/tv_2"
        android:layout_width="wrap_content"
        android:text="text 2"
        android:layout_height="match_parent" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent">

    </Spinner>

</LinearLayout>

10. Now run the project and get your spinner working properly in the recycler view. 

#HAPPYCODING

9 comments:

  1. Not a good implementation of spinner inside of recycler view.

    ReplyDelete
  2. any idea how to get values from these spinners?

    ReplyDelete
  3. veryyyyyyyyyyyyyyyyyyyyyyyyyyyy gooooooooooooood

    Thx

    ReplyDelete
    Replies
    1. can u explain what u understood sir...

      Delete
  4. I needed a tutorial that showed the essentials of how to put a spinner in a recyclerview - this tutorial did that.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hi Please If any Have tutorial link please share it..

    ReplyDelete