rectclerview 라이브러리 추가하기
Gradle Scripts>build.gradel
implementation 'androidx.recyclerview:recyclerview:1.0.0'를 dependencies안에 추가하기
activity_main.xml생성/
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
activity_address.xml생성
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#3F51B5"
android:layout_margin="10sp"
android:id="@+id/item">
<TextView
android:id="@+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="김경민"
android:textColor="#ffffff"
android:textSize="20sp"
android:padding="10sp"/>
<TextView
android:id="@+id/txtadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="인천 학익동 icia교육원"
android:textColor="#0E312F"
android:textSize="15sp"
android:layout_below="@id/txtname"
android:padding="10sp"/>
<TextView
android:id="@+id/txttel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="010-1234-1234"
android:textColor="#0E312F"
android:textSize="20sp"
android:layout_alignParentRight="true"
android:padding="10sp"/>
</RelativeLayout>
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"
android:background="#2196F3"
android:layout_margin="10sp"
android:orientation="vertical"
android:id="@+id/item">
<TextView
android:id="@+id/txtcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="여기에 내용출력"
android:textColor="#FFFEFE"
android:textSize="20sp"
android:padding="10sp"/>
<TextView
android:id="@+id/txtwdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="여기는 날짜출력"
android:textColor="#66E8F8"
android:textSize="15sp"
android:padding="10sp"
/>
</LinearLayout>
MemoVO.java 생성
package com.example.ex12;
//메모에대한 정보를 넣어줄 클래스
public class MemoVO {
private String content;
private String wdate;
//생성자
public MemoVO(String content, String wdate) {
this.content = content;
this.wdate = wdate;
}
//Getters AND Setters
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getWdate() {
return wdate;
}
public void setWdate(String wdate) {
this.wdate = wdate;
}
}
AddressVO.java 생성
package com.example.ex12;
public class AddressVO {
private String name;
private String add;
private String tel;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAdd() {
return add;
}
public void setAdd(String add) {
this.add = add;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public AddressVO(String name, String add, String tel) {
this.name = name;
this.add = add;
this.tel = tel;
}
}
MemoAdapter.java 생성
package com.example.ex12;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class MemoAdapter extends RecyclerView.Adapter<MemoAdapter.ViewHolder> {
ArrayList<MemoVO> array;
Context context;
//어떤데이터,어디서 넘어왔는지
public MemoAdapter(ArrayList<MemoVO> array,Context context) {
this.array = array;
this.context=context;
}
//생성
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//item.xml을 만들어서 view에 넣겠음
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.txtconetnt.setText(array.get(position).getContent());
holder.txtwdate.setText(array.get(position).getWdate());
//리스너 달기
holder.item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,array.get(position).getContent(),Toast.LENGTH_SHORT).show();
//Log.i("log",array.get(position).getContent());
}
});
if(position % 2 == 1){
holder.item.setBackgroundColor(Color.MAGENTA);
holder.txtconetnt.setTextColor(Color.YELLOW);
holder.txtwdate.setTextColor(Color.YELLOW);
}
}
@Override
public int getItemCount() {
return array.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtconetnt,txtwdate;
LinearLayout item;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtconetnt=itemView.findViewById(R.id.txtcontent);
txtwdate=itemView.findViewById(R.id.txtwdate);
item=itemView.findViewById(R.id.item);
}
}
}
AddAdapter.java생성
package com.example.ex12;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class AddAdapter extends RecyclerView.Adapter<AddAdapter.ViewHolder> {
ArrayList<AddressVO> array;
Context context;
public AddAdapter(ArrayList<AddressVO> array, Context context) {
this.array = array;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_address,parent,false);
return new ViewHolder(view);
}
//데이터를 실제로 넣어줍니다.
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.txtname.setText(array.get(position).getName());
holder.txttel.setText(array.get(position).getTel());
holder.txtadd.setText(array.get(position).getAdd());
holder.item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,array.get(position).getName(),Toast.LENGTH_SHORT).show();
}
});
if(position %2 ==1){
holder.item.setBackgroundColor(Color.parseColor("#28E7FF"));
holder.txtname.setTextColor(Color.parseColor("#000000"));
}
}
@Override
public int getItemCount() {
return array.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtname,txtadd,txttel;
RelativeLayout item;
public ViewHolder(@NonNull View itemView) {
super(itemView);
txtname=itemView.findViewById(R.id.txtname);
txtadd=itemView.findViewById(R.id.txtadd);
txttel=itemView.findViewById(R.id.txttel);
item=itemView.findViewById(R.id.item);
}
}
}
MainAtivity.java생성
package com.example.ex12;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<MemoVO> array;
RecyclerView list;
MemoAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//데이터만들기
array=new ArrayList<MemoVO>();
MemoVO vo=new MemoVO("주소록","2019/11/14 08:00:00");
array.add(vo);
vo=new MemoVO("은행계좌","2019/11/12 08:58:20");
array.add(vo);
vo=new MemoVO("cgv아이디","2019/11/08 20:10:20");
array.add(vo);
vo=new MemoVO("yes24아이디","2019/08/08 19:59:55");
array.add(vo);
//list만들기
list=findViewById(R.id.list);
LinearLayoutManager manager=new LinearLayoutManager(this);
//매니저
list.setLayoutManager(manager);
//어덥터만들기
adapter=new MemoAdapter(array,this);
list.setAdapter(adapter);
}
}
'Android' 카테고리의 다른 글
주소록 만들기 (0) | 2019.11.14 |
---|---|
RecyclerView를 이용한 상품목록만들기 (0) | 2019.11.13 |
Firebase를 이용한 이메일 로그인 (0) | 2019.11.12 |
검색할 수 있는 메모장 만들기 (1) | 2019.11.12 |
캘린더를 활용한 다이어리 앱 만들기 (0) | 2019.11.11 |