Wednesday, 14 February 2018

RecyclerView Example with click on every row and view data in another class.

1. ModelClass:-
public class ModelClass {
    int image;
    String name,number;

    public ModelClass(int image, String name, String number) {
        this.image = image;
        this.name = name;
        this.number = number;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }
}
2.AdapterClass.java
public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder> {
    MainActivity mainActivity;
    ArrayList<ModelClass> modelClassArrayList;

    public AdapterClass(MainActivity mainActivity, ArrayList<ModelClass> modelClassArrayList) {
        this.mainActivity = mainActivity;
        this.modelClassArrayList = modelClassArrayList;
    }

    @Override   
 public AdapterClass.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item,parent,false);
        MyViewHolder viewHolder=new MyViewHolder(view,modelClassArrayList,mainActivity);
        return viewHolder;
    }

    @Override    
public void onBindViewHolder(AdapterClass.MyViewHolder holder, int position) {
        holder.image.setImageResource(modelClassArrayList.get(position).getImage());
        holder.name.setText(modelClassArrayList.get(position).getName());
        holder.phone.setText(modelClassArrayList.get(position).getNumber());
    }

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

    //another class...    
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        ImageView image;
        TextView name,phone;
        ArrayList<ModelClass> modelClasses;
        MainActivity mainActivity;

        public MyViewHolder(View itemView,ArrayList<ModelClass> list,MainActivity mainActivity) {
            super(itemView);
            this.modelClasses=list;
            this.mainActivity=mainActivity;
            itemView.setOnClickListener(this);
            image=(ImageView)itemView.findViewById(R.id.image);
            name=(TextView)itemView.findViewById(R.id.name);
            phone=(TextView)itemView.findViewById(R.id.number);

        }

        @Override        
public void onClick(View v) {
            int position=getAdapterPosition();
            ModelClass modelClass=this.modelClasses.get(position);
            Intent intent=new Intent(mainActivity,DetailsClass.class);
            intent.putExtra("image",modelClass.getImage());
            intent.putExtra("name",modelClass.getName());
            intent.putExtra("phone",modelClass.getNumber());
            this.mainActivity.startActivity(intent);
        }
    }
}
3.row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="match_parent"    
android:orientation="horizontal"    
android:layout_margin="20dp"    
android:gravity="center"    
android:layout_height="wrap_content">
    <ImageView        
android:layout_width="70dp"        
android:layout_height="70dp"        
android:layout_weight="1"        
android:id="@+id/image"/>
    <TextView        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_weight="1"        
android:id="@+id/name"/>
    <TextView        android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_weight="1"        
android:id="@+id/number"/>

</LinearLayout>
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"    
tools:context="com.example.supriyabharti.recylerviewclickitem.MainActivity">

    <android.support.v7.widget.RecyclerView        
android:layout_width="match_parent"        
android:layout_height="match_parent"        
android:id="@+id/recycler">
</android.support.v7.widget.RecyclerView>

</LinearLayout>
4.MainActivity.java
public class MainActivity extends AppCompatActivity {
    private RecyclerView recycler;
    RecyclerView.LayoutManager manager;
    AdapterClass adapterClass;
    int[] imageList={R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher,R.mipmap.ic_launcher};
    String[] strName={"Supriya","Navin","Ankit","Saurabh","Pankaj","Pappu"};
    String[] strPhone={"+91333222","91333222","91333222","91333222","91333222","91333222"};
    ArrayList<ModelClass> list=new ArrayList<ModelClass>();
    ModelClass modelClass;

    @Override    
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recycler=(RecyclerView)findViewById(R.id.recycler);

        int i=0;
        for(String s:strName){
            modelClass =new ModelClass(imageList[i],strName[i],strPhone[i]);
            list.add(modelClass);
            i++;
        }

        manager=new LinearLayoutManager(this);
        adapterClass=new AdapterClass(MainActivity.this,list);
        recycler.setHasFixedSize(true);
        recycler.setLayoutManager(manager);
        recycler.setAdapter(adapterClass);

    }
}
5.DetailClass.java

public class DetailsClass extends AppCompatActivity {
    ImageView imageView;
    TextView name,number;
    @Override    
protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);
        imageView=(ImageView)findViewById(R.id.image);
        name=(TextView) findViewById(R.id.name);
        number=(TextView) findViewById(R.id.number);
               
imageView.setImageResource(getIntent().getIntExtra("image",1));
        name.setText(getIntent().getStringExtra("name"));
        number.setText(getIntent().getStringExtra("phone"));
    }
}
6.details.xml
<?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:gravity="center"    
android:layout_height="match_parent">
    <ImageView        
android:layout_width="70dp"        
android:layout_height="70dp"        
android:id="@+id/image"/>
    <TextView       
 android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:id="@+id/name"/>
    <TextView        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:id="@+id/number"/>

</LinearLayout>
7.screenShots:


No comments:

Post a Comment