Programming/Android

안드로이드 리사이클러뷰(RecyclerView) 예제

fishersheep 2021. 8. 17. 14:05
반응형

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:orientation="vertical">
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/recyclerView"
        android:paddingBottom="40dp"/>

<LinearLayout
    android:id="@+id/linear2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="bottom"
    android:layout_alignParentBottom="true">

    <ImageButton
        android:id="@+id/im1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:src="@drawable/person"
        android:scaleType="fitXY"
        android:adjustViewBounds="true"
        android:layout_weight="1"/>

        <ImageButton
            android:id="@+id/im2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/chat"
            android:layout_weight="1"/>

    </LinearLayout>

    </RelativeLayout>

</LinearLayout>

person.java

package com.cookandroid.recycler_ex03;

public class Person {
    String name;
    String mobile;
    String date;


    public Person(String name, String mobile,String date) {
        this.name = name;
        this.mobile = mobile;
        this.date= date;
    }

    public String getName(){
        return name;
    }

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

    public String getMobile(){
        return mobile;
    }
    public void setMobile(){
        this.mobile=mobile;
    }
    public String getDate(){
        return date;
    }
    public void setDate(){
        this.date=date;
    }

}

personadapter.java

package com.cookandroid.recycler_ex03;

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

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

import java.util.ArrayList;

public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder> {
    ArrayList<Person> items =new ArrayList<>();

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {      /* view를 생성할때 실행되어  viewholder를 리턴*/
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = inflater.inflate(R.layout.person_item,viewGroup,false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {    /* viewholder내용 변경*/
        Person item = items.get(position);
        viewHolder.setItem(item);
    }

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

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        TextView textView2;
        TextView textView3;

        public ViewHolder(View itemVIew){
            super(itemVIew);

            textView= itemVIew.findViewById(R.id.textView);
            textView2= itemVIew.findViewById(R.id.textView2);
            textView3 = itemVIew.findViewById(R.id.textView3);

        }

        public void setItem(Person item){
            textView.setText(item.getName());
            textView2.setText(item.getMobile());
            textView3.setText(item.getDate());
        }
    }
    public void addItem(Person item){
        items.add(item);
    }
    public void setItems(ArrayList<Person>items){
        this.items = items;
    }
    public Person getItem(int position){
        return items.get(position);
    }
    public void setItem(int position,Person item){
        items.set(position,item);
    }
}

person_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:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="80dp"
            android:layout_height="80dp"
            app:srcCompat="@drawable/person"
            android:padding="5dp"/>


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

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="30sp"
                    android:text="이름"/>

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text="날짜"
                    android:textSize="20sp" />

            </RelativeLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textView2"
                android:text="최근대화"
                android:textSize="25sp"/>

            </LinearLayout>


</LinearLayout>
</LinearLayout>

main.java

package com.cookandroid.recycler_ex03;

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

import android.os.Bundle;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

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

        setTitle("채팅");

        RecyclerView recyclerView= findViewById(R.id.recyclerView);

        LinearLayoutManager layoutManager =
                new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);
        PersonAdapter adapter = new PersonAdapter();

        adapter.addItem(new Person("김민수","aaa","2020.09.14"));
        adapter.addItem(new Person("김하늘","bbb","2020.09.13"));
        adapter.addItem(new Person("홍길동","ccc","2020.09.12"));
        adapter.addItem(new Person("홍길동","ddd","2020.09.11"));
        adapter.addItem(new Person("홍길동","eee","2020.09.10"));
        adapter.addItem(new Person("홍길동","fff","2020.09.9"));
        adapter.addItem(new Person("홍길동","ggg","2020.09.8"));
        adapter.addItem(new Person("홍길동","hhh","2020.09.7"));
        adapter.addItem(new Person("홍길동","iii","2020.09.6"));
        adapter.addItem(new Person("홍길동","jjj","2020.09.5"));

        recyclerView.setAdapter(adapter);
    }
}

반응형