-ReclyerView

 

버튼 누르면 추가됨
꾹 누르면 삭제되고 그냥 누르면 밑에 숫자 뜸

 

 

 

<MainActivity.java>

package com.example.recycleex;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements MyRecyclerAdapter.MyRecyclerViewClickListener {
    private String[] catName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        int[] cats= new int[]{
                R.drawable.cat01, R.drawable.cat02, R.drawable.cat03, R.drawable.cat04, R.drawable.cat05,
                R.drawable.cat06, R.drawable.cat07, R.drawable.cat08, R.drawable.cat09, R.drawable.cat10,
                R.drawable.cat11, R.drawable.cat12, R.drawable.cat13, R.drawable.cat14, R.drawable.cat15,
                R.drawable.cat16, R.drawable.cat17, R.drawable.cat18, R.drawable.cat19, R.drawable.cat20,
                R.drawable.cat21, R.drawable.cat22, R.drawable.cat23, R.drawable.cat24, R.drawable.cat25,

        };

        catName = new String[]{
                "R.drawable.cat01", "R.drawable.cat02", "R.drawable.cat03", "R.drawable.cat04", "R.drawable.cat05",
                "R.drawable.cat06", "R.drawable.cat07", "R.drawable.cat08", "R.drawable.cat09", "R.drawable.cat10",
                "R.drawable.cat11", "R.drawable.cat12", "R.drawable.cat13", "R.drawable.cat14", "R.drawable.cat15",
                "R.drawable.cat16", "R.drawable.cat17", "R.drawable.cat18", "R.drawable.cat19", "R.drawable.cat20",
                "R.drawable.cat21", "R.drawable.cat22", "R.drawable.cat23", "R.drawable.cat24", "R.drawable.cat25",
        };

        ArrayList<ItemData> dataList = new ArrayList<>();

        MyRecyclerAdapter adapter = new MyRecyclerAdapter(dataList);
        recyclerView.setAdapter(adapter);

        Button btnAdd = findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            int i=0;
            @Override
            public void onClick(View v) {
                dataList.add(new ItemData(cats[i],"고양이 "+i,String.format("고양이리뷰 %03d",i)));
                adapter.notifyDataSetChanged();
                i++;
            }
        });
        adapter.setOnClickListener(this);
    }

    @Override
    public void onItemClicked(int position) {
        Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onImageViewClicked(int position) {
        Toast.makeText(getApplicationContext(), catName[position], Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onImageLongClicked(int position) {
        Toast.makeText(getApplicationContext(),position+ " is removed",Toast.LENGTH_SHORT).show();
    }

}

<MyRecyclerAdapter.java>

package com.example.recycleex;

import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import java.util.ArrayList;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
    ArrayList<ItemData> itemData;

    public MyRecyclerAdapter(ArrayList<ItemData> itemData) {
        this.itemData = itemData;
    }

    //Interface 생성
    public interface MyRecyclerViewClickListener{
        void onItemClicked(int position); //객체 클릭
        void onImageViewClicked(int position);
        void onImageLongClicked(int position);
    }

    private MyRecyclerViewClickListener mListener;
    public void setOnClickListener(MyRecyclerViewClickListener listener){
        this.mListener = listener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) { //외부에서 가져온 데이터 셋팅
            ItemData item = itemData.get(position);
            holder.title.setText(item.getTitle());
            holder.contents.setText(item.getContents());
            holder.imageView.setImageResource(item.getImage());

            if(mListener != null){
                final int pos = position;
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.onItemClicked(pos);
                    }
                });
                holder.imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.onImageViewClicked(pos);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        mListener.onImageLongClicked(pos);
                        Remove(pos);
                        return true;
                    }
                });
            }
    }

    @Override
    public int getItemCount() {
        return itemData.size(); //외부에서 가져온 데이터의 개수
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        TextView title, contents;
        ImageView imageView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView= itemView.findViewById(R.id.imageView);
            imageView.setBackground(new ShapeDrawable(new OvalShape()));
            imageView.setClipToOutline(true);
            title = itemView.findViewById(R.id.textTitle);
            contents = itemView.findViewById(R.id.textContents);
        }
    }

    public void Remove(int position){
        try {
            itemData.remove(position);
            notifyDataSetChanged();
        }catch (IndexOutOfBoundsException e){
            e.printStackTrace();
        }
    }

}

<ItemData.java>

package com.example.recycleex;

public class ItemData {
    private int image;
    private String title;
    private String contents;

    public ItemData(int image, String title, String contents) {
        this.image = image;
        this.title = title;
        this.contents = contents;
    }

    public int getImage() {
        return image;
    }

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

    public String getTitle() {
        return title;
    }

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

    public String getContents() {
        return contents;
    }

    public void setContents(String contents) {
        this.contents = contents;
    }

}

<list_item.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="wrap_content"
    android:background="@android:color/white"
    android:gravity="center_vertical"
    android:padding="8dp"
    android:layout_marginBottom="8dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:includeFontPadding="false"
            android:textSize="18dp"
            android:textColor="@android:color/black"
            android:textStyle="bold"
            android:text="TITLE" />

        <TextView
            android:id="@+id/textContents"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:includeFontPadding="false"
            android:textSize="13dp"
            android:textColor="@android:color/darker_gray"
            android:text="CONTENTS" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:scaleType="centerCrop"
        tools:srcCompat="@tools:sample/avatars" />
</LinearLayout>

<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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#f0f0f0">

    </androidx.recyclerview.widget.RecyclerView>

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

+ Recent posts