Android RecyclerView Onclick New Activity Custom Adapter

dependency

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:recyclerview-v7:28.0.0'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

  1. 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=".MainActivity">

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

</LinearLayout>

2. mylist.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="vertical"
    android:id="@+id/linearLayout"
    android:layout_height="match_parent">

<TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

details_activity.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=".Detailview">
<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

Mainactivity.java

package sap.business.one.recycleonclick;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private ArrayList <Movie> list = new ArrayList<Movie>(  );
private  Myadapter myadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        recyclerView = findViewById( R.id.recyclerview );
        GridLayoutManager gridLayoutManager = new GridLayoutManager( this,2 );
        recyclerView.setLayoutManager( gridLayoutManager );
        myadapter = new Myadapter( this,list );
        recyclerView.setAdapter( myadapter );
        list.add( new Movie( "ashik",R.drawable.admin ) );
        list.add( new Movie( "Virat",R.drawable.person ) );
        list.add( new Movie( "Rohit",R.drawable.finance ) );
        list.add( new Movie( "Shakib",R.drawable.sales ) );
    }
}

detailsview.java

package sap.business.one.recycleonclick;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Detailview extends AppCompatActivity {
private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_detailview );
        textView = findViewById( R.id.text );
String name = getIntent().getStringExtra( "name" ).trim();
textView.setText( name );

    }
}

Movie.java

package sap.business.one.recycleonclick;

public class Movie {
    String name;
    int logo;

    public Movie(String name,int logo) {
        this.name = name;
        this.logo = logo;
    }

    public String getName() {
        return name;
    }

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

    public int getLogo() {
        return logo;
    }

    public void setLogo(int logo) {
        this.logo = logo;
    }
}

Myadapder.java

package sap.business.one.recycleonclick;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class Myadapter extends RecyclerView.Adapter<Myadapter.Holder> {

    Context context;
    ArrayList<Movie> list;

    public Myadapter(Context context,ArrayList<Movie> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup viewGroup,int i) {
        View view = LayoutInflater.from( context ).inflate( R.layout.mylist,viewGroup,false );
        Holder holder = new Holder( view );
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder,int i) {
final Movie movie = list.get( i );
holder.name.setText( movie.name );
holder.logo.setImageResource( movie.logo );
holder.linearLayout.setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent( context,Detailview.class );
        intent.putExtra( "name",movie.getName() );
        context.startActivity( intent );
    }
} );
    }

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

    public class Holder extends RecyclerView.ViewHolder {
        public TextView name;
        public ImageView logo;
        public LinearLayout linearLayout;
        public Holder(@NonNull View itemView) {
            super( itemView );
            name = itemView.findViewById(  R.id.name);
            logo = itemView.findViewById(  R.id.logo);
            linearLayout = itemView.findViewById(  R.id.linearLayout);

        }
    }
}

Published by

Unknown's avatar

Nusrat Faria

I Am A Web Developer And A Android Developer. This Is My Personal Blog So Noted My Work For Helping People .

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.