2swan

RecyclerView Image 본문

Programming/Android Example

RecyclerView Image

2swan 2023. 8. 9. 13:32

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">

    <androidx.recyclerview.widget.RecyclerView
     android:id="@+id/recyclerView"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

</LinearLayout>

 

movie.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_movie"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/ivPoster"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"/>

</LinearLayout>

 

MovieAdapter.activity

package com.example.app08;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.ViewHolder> {
    ArrayList<MovieItem>movieList;

    //setter
    public void setMovieList(ArrayList<MovieItem> movieList) {
        this.movieList = movieList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.activity_main_movie, parent, false);
        return new MovieAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        MovieItem movieItem = movieList.get(position);
        holder.imageView.setImageResource(movieItem.getPosterId());
    }

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

    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.ivPoster);

        }
    }
}

 

main.activity

public class MainActivity extends AppCompatActivity {
private  MovieAdapter movieAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Integer[] posterID={
                R.drawable.mov01, R.drawable.mov02,
                R.drawable.mov03, R.drawable.mov04,
                R.drawable.mov05, R.drawable.mov06,
                R.drawable.mov07, R.drawable.mov08,
                R.drawable.mov09, R.drawable.mov10
        };


    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    ArrayList<MovieItem>movieList = new ArrayList<>();


    //리스트 값 할당
    for(int i =0; i < posterID.length; i++){
        MovieItem movie = new MovieItem();
        movie.setPosterId(posterID[i]);
        movie.setTitle("타이틀"+i);
        movieList.add(movie);
    }
    //어댑터 생성 및 리스트 전달
    movieAdapter = new MovieAdapter();
    //setter
    movieAdapter.setMovieList(movieList);

    //뷰 어댑터 연결
    recyclerView.setAdapter(movieAdapter);
    recyclerView.setLayoutManager(new GridLayoutManager(this, 3));

    }


}

 

MovieItem.java

public class MovieItem {

    private int posterId;
    private String title;


    public int getPosterId() {
        return posterId;
    }

    public void setPosterId(int posterId) {
        this.posterId = posterId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

결과 값

'Programming > Android Example' 카테고리의 다른 글

RecyclerView 추가, 전체보기  (0) 2023.08.09
RecyclerView 예제  (0) 2023.08.09
SQLite 일기장 앱  (0) 2023.08.09
RecyclerView 목록 만들기  (0) 2023.08.09
Fragment 예제  (0) 2023.08.08