다음 그림의 예제 처럼 만들어 보는 시간 입니다.
하울의 코딩 채널에서 본 예제를 만들어보았습니다
출처 : https://www.youtube.com/watch?v=o3InUmheU3c&index=8&list=PLmdU__e_zPf_Pv5S3OT8lpTJa7pxwYNUL
gradle 추가
compile 'com.android.support:recyclerview-v7:25.3.0' -- RecyclerView Compile
compile 'de.hdodenhof:circleimageview:2.1.0' --circleImageView Compile
Project 구성
activity_main
<?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="com.example.shj89.recyclerviewlist.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
<?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:layout_margin="3dp">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/image_1"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
<TextView
android:text="홍길동"
android:layout_toRightOf="@+id/profile_image"
android:textSize="20sp"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:gravity="center"
android:background="#faf2b6"
android:layout_width="130dp"
android:layout_height="25dp">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="오늘 기분 개굿!!"/>
</LinearLayout>
</RelativeLayout>
package com.example.shj89.recyclerviewlist;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by shj89 on 2017-09-15.
*/
public class MainActivity extends AppCompatActivity {
@Overrie
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
RecyclerView view = (RecyclerView) findViewById(R.id.main_recyclerview);
recyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
MyRecyclerViewAdapter myRecyclerViewAdapter = new MyRecyclerViewAdapter();
view.setLayoutManager(layoutManager);
view.setAdapter(myRecyclerViewAdapter);
}
}
package com.example.shj89.recyclerviewlist;
/**
* Created by shj89 on 2017-09-15.
*/
public class MemberDTO {
public int image;
public String name;
public String message;
public MemberDTO(int image, String name, String message) {
this.image = image;
this.name = name;
this.message = message;
}
}
MyRecyclerViewAdapter
package com.example.shj89.recyclerviewlist;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by shj89 on 2017-09-15.
*/
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private ArrayList<MemberDTO> memberDTOs = new ArrayList<>();
public MyRecyclerViewAdapter(){
memberDTOs.add(new MemberDTO(R.drawable.image_1,"유명식","오늘 예비군 훈련"));
memberDTOs.add(new MemberDTO(R.drawable.image_2,"나명식","코딩 잼씀!"));
memberDTOs.add(new MemberDTO(R.drawable.image_3,"김명식","굿굿!!"));
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//XML 가져오는 부분
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,parent,false);
return new RowCell(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//데이터를 넣어주는 부분
((RowCell)holder).circleImageView.setImageResource(memberDTOs.get(position).image);
((RowCell)holder).name.setText(memberDTOs.get(position).name);
((RowCell)holder).message.setText(memberDTOs.get(position).message);
}
@Override
public int getItemCount() {
//카운터
return memberDTOs.size();
}
//소스코드 절약해주는 부분0
private static class RowCell extends RecyclerView.ViewHolder {
CircleImageView circleImageView;
TextView name;
TextView message;
public RowCell(View view) {
super(view);
circleImageView = (CircleImageView)view.findViewById(R.id.profile_image);
name = (TextView)view.findViewById(R.id.name);
message = (TextView)view.findViewById(R.id.message);
}
}
}
'Android' 카테고리의 다른 글
안드로이드 Json 파싱 (0) | 2017.09.13 |
---|---|
Sevice 구현 예제 (0) | 2017.09.09 |
Parcelable (0) | 2017.08.01 |
PDF 파일을 띄우는 Source (0) | 2017.07.26 |
Inflater (0) | 2017.07.25 |