2swan

RecyclerView 예제 본문

Programming/Android Example

RecyclerView 예제

2swan 2023. 8. 9. 13:37

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

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

</LinearLayout>

 

person_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이름"
        android:textSize="16sp"/>
    <TextView
        android:id="@+id/tvphone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="전화번호"
        android:textSize="12sp"/>
</LinearLayout>

 

main.activity

public class MainActivity2 extends AppCompatActivity {

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

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        PersonAdapter personAdapter = new PersonAdapter();

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);

        for(int i=0; i<20; i++){
            personAdapter.addItem(new Person("홍길동"+i, "010-1111-2222"));
        }
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(personAdapter);



    }
}

 

PersonAdapter.java

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

    public void setPersonList(ArrayList<Person> personList) {
        this.personList = personList;
    }

    public void addItem(Person person){
        personList.add(person);
    }

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

    @Override
    public void onBindViewHolder(@NonNull PersonAdapter.ViewHolder holder, int position) {
        // 뷰홀더에 바인딩 될때(화면 아이템 요소가 바뀔때, 내부적으로 아이템뷰 재활용함)
        Person person = personList.get(position);
        holder.setItem(person);

        // 이 방법도 가능
//        holder.tvName.setText((person.getName()));
//        holder.tvPhone.setText((person.getMobile()));
    }

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

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvName, tvPhone;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvname);
            tvPhone = itemView.findViewById(R.id.tvphone);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = getAdapterPosition();
                    Log.d("position",position+"");
                    Toast.makeText(view.getContext(),personList.get(position).getName()+" // "
                            +personList.get(position).getMobile()+"선택됨", Toast.LENGTH_SHORT).show();
                }
            });
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int position = getAdapterPosition();
                    Log.d("position",position+"");
                    Toast.makeText(view.getContext(),personList.get(position).getName()+" // "
                            +personList.get(position).getMobile()+"선택됨", Toast.LENGTH_SHORT).show();
                    return false;
                }
            });
        }
        public void setItem(Person person){
            tvName.setText(person.getName());
            tvPhone.setText(person.getMobile());
    }


    }
}

 

Person.java

public class Person {
    private String name;
    public  String mobile;
    public Person(){

    }

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

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", mobile='" + mobile + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}

결과 값

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

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