-1

So,basically in this as soon as I will click on the image view i should go on the next page that is firstpage .IN this for firstpage I have used recyclerview and for the introduction pge i have used gridadapter.but when I click on the imag view its not getting intented to the next page.I tried all possible sols. ut still the same.plzz help.

this is my introduction.xml code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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="wrap_content"
    android:background="@drawable/ic_launcher_background"
    tools:context=".MainActivity2">


    <androidx.cardview.widget.CardView
        android:layout_width="199dp"
        android:layout_height="199dp"
        app:cardElevation="10dp"
        app:cardUseCompatPadding="true"
        app:contentPadding="10dp"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.247"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="35dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="193dp"
            android:layout_height="194dp">

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="143dp"
                android:layout_height="109dp"
                tools:layout_editor_absoluteX="7dp"
                tools:layout_editor_absoluteY="16dp"
                tools:srcCompat="@tools:sample/avatars"
                android:onClick="image"/>

            <TextView
                android:id="@+id/textView15"
                android:layout_width="158dp"
                android:layout_height="33dp"
                android:layout_marginTop="4dp"
                android:text="text"
                android:textColor="@color/DarkBlue"
                android:textSize="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.2"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/imageView4" />


        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>

this is my MainActivity2.java code

public abstract class MainActivity2 extends AppCompatActivity implements ClickedListener {
    RecyclerView recycleview1;
    List<String> titles;
    List<Integer>images;
    GridAdapter adapter;
    ImageView image;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intropage);


        recycleview1 = findViewById(R.id.recycleview1);

        titles = new ArrayList<>();
        images = new ArrayList<>();

        titles.add("LATEST MOVIES");
        titles.add("UPCOMING MOVIES");
        titles.add("TRENDING MOVIES");
        titles.add("NOW PLAYING");
        titles.add("RELEASED MOVIES");
        titles.add("HINDI MOVIES");
        titles.add("MARATHI MOVIES");
        titles.add("ENGLISH MOVIES");
        titles.add("TELUGU MOVIES");
        titles.add("TAMIL MOVIES");


        images.add(R.drawable.latestmovie);
        images.add(R.drawable.upcomingmovies);
        images.add(R.drawable.trendingmovie);
        images.add(R.drawable.nowplaying);
        images.add(R.drawable.releasedmovie);
        images.add(R.drawable.hindimovies);
        images.add(R.drawable.marathimovies);
        images.add(R.drawable.englishmovies);
        images.add(R.drawable.telugumovies);
        images.add(R.drawable.tamilmovies);


        adapter =  new GridAdapter(this,titles,images,this);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
        recycleview1.setLayoutManager(gridLayoutManager);
        recycleview1.setAdapter(adapter);


    }


    @Override
    public void onPictureClicked() {
        Intent intent = new Intent(MainActivity2.this, MainActivity1.class);
        startActivity(intent);
    }
}

this is my GridAdapter.java code

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {

    List<String>titles;
    List<Integer>images;
    LayoutInflater inflater;

    public GridAdapter(Context context,List<String>titles,List<Integer>images){
        this.titles = titles;
        this.images = images;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.intoduction,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.titles.setText(titles.get(position));
        holder.images.setImageResource(images.get(position));

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView titles;
        ImageView images;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            titles = itemView.findViewById(R.id.textView15);
            images = itemView.findViewById(R.id.imageView4);
        }
    }
}

this is my firstpage.xml code

<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
    android:background="@color/LightGrey"
    android:layout_marginTop="5dp"
    tools:context=".MainActivity1">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_margin="10dp"
        tools:srcCompat="@tools:sample/avatars" />

    <LinearLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Title"
            android:textSize="20sp"
            android:textStyle="bold">

        </TextView>

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_marginTop="-10dp"
            android:text="Description"
            android:textSize="20sp">

        </TextView>

        <TextView
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="Ratings"
            android:textSize="20sp">

        </TextView>
    </LinearLayout>
</LinearLayout>
sank
  • 133
  • 7
  • 2
    Does this answer your question? [RecyclerView onClick](https://stackoverflow.com/questions/24471109/recyclerview-onclick) – a_local_nobody Dec 05 '20 at 13:48

1 Answers1

1

This interface will be implemented in class where your adapter is called.

I created an interface and implemented in MainActivity2, that interface has on override() method clickedListener. This clickedListener will be called whenever an image clicked and then the intent inside clickedListener will be called and you will be moved to MainActivity1.java.

This is your working code.

   public interface ClickedListener {
    
        void onPictureClicked();
     
    }

GridAdapter.java

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {

    List<String>titles;
    List<Integer>images;
    LayoutInflater inflater;
    ClickedListener clickedListener;


    public GridAdapter(Context context,List<String>titles,List<Integer>images, ClickedListener  clickedListener ){
        this.titles = titles;
        this.images = images;
        this.inflater = LayoutInflater.from(context);
        this.clickedListener = clickedListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.intoduction,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.titles.setText(titles.get(position));
        holder.images.setImageResource(images.get(position));

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView titles;
        ImageView images;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            titles = itemView.findViewById(R.id.textView15);
            images = itemView.findViewById(R.id.imageView4);
            images.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickedListener.onPictureClicked();
                }
            });
        }
    }
}

MainActivity2

public class MainActivity2 extends AppCompatActivity implements ClickedListener{
    RecyclerView recycleview1;
    List<String> titles;
    List<Integer>images;
    GridAdapter adapter;
    ImageView image;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intropage);


        recycleview1 = findViewById(R.id.recycleview1);

        titles = new ArrayList<>();
        images = new ArrayList<>();

        titles.add("LATEST MOVIES");
        titles.add("UPCOMING MOVIES");
        titles.add("TRENDING MOVIES");
        titles.add("NOW PLAYING");
        titles.add("RELEASED MOVIES");
        titles.add("HINDI MOVIES");
        titles.add("MARATHI MOVIES");
        titles.add("ENGLISH MOVIES");
        titles.add("TELUGU MOVIES");
        titles.add("TAMIL MOVIES");


        images.add(latestmovie);
        images.add(R.drawable.upcomingmovies);
        images.add(R.drawable.trendingmovie);
        images.add(R.drawable.nowplaying);
        images.add(R.drawable.releasedmovie);
        images.add(R.drawable.hindimovies);
        images.add(R.drawable.marathimovies);
        images.add(R.drawable.englishmovies);
        images.add(R.drawable.telugumovies);
        images.add(R.drawable.tamilmovies);


        adapter =  new GridAdapter(this,titles,images,this);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
        recycleview1.setLayoutManager(gridLayoutManager);
        recycleview1.setAdapter(adapter);


    }


    
    }
    @Override
    public void onPictureClicked() {
        Intent intent = new Intent(getApplicationContext(), MainActivity1.class);
        startActivity(intent);
    }
}

MainActivity1.java

public class MainActivity1 extends AppCompatActivity {
    RecyclerView recyclerView;
    RecyclerView.Adapter MovieAdapter;
    RecyclerView.LayoutManager layoutManager;
    String[] movieNameList = {"BHOOT","PANGA","PSYCHO","JUMANJI","DIL BECHARA","BAAGHI 3","THE BRIDGE CURSE","LOST GIRLS","BAD BOYS FOR LIFE",
            "THAPPAD","SADAK 2","TANHAJI","BHEESHMA" };
    String[] movieDescriptionList = {"Horror/Mystery","Sport/Drama","Thriller/Psychological   Thriller","Comedy/Action","Romance/Drama","Action/Thriller",
            "Horror/Thriller","Mystery/Drama","Action/Comedy","Drama","Drama/Thriller","Action/Historical     drama","Romance/Rom-com"};
    String[] movieRatings = {"3.5/5","4.5/5","3/5","4.5/5","4/5","4/5","3.5/5","2.5/5","3/5","5/5","3.5/5","4/5","3/5"};
    int[] movieImages = {R.drawable.bhoot,R.drawable.panga,R.drawable.psycho,R.drawable.jumanji,R.drawable.dil_bechera,R.drawable.baaghi3,R.drawable.bridgecurse,
            R.drawable.lostgirls,R.drawable.bad_boys,R.drawable.thappad,R.drawable.sadak2,R.drawable.tanhaji,R.drawable.bheeshma};


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainpage);
        recyclerView  = findViewById(R.id.recycleview);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        MovieAdapter= new MovieAdapter(this,movieNameList,movieDescriptionList,movieRatings,movieImages);
        recyclerView.setAdapter(MovieAdapter);
    }




}
Shoaib Kakal
  • 1,090
  • 2
  • 16
  • 32