2swan
RecyclerView 목록 만들기 본문
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="친구 목록"
android:textSize="20sp"
android:textColor="#BA6666"
android:textStyle="bold"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
item_recyclerview.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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="20dp">
<!--프로필 사진-->
<ImageView
android:id="@+id/profile"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/person"/>
<!--이름-->
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="이름"
android:textSize="16sp"
android:textColor="#000"/>
<!--상태 메시지-->
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="상태메시지"
android:textSize="12sp"
android:textColor="#444"
android:maxLines="1"/>
</LinearLayout>
<!--구분선-->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginHorizontal="10dp"
android:background="#aaa"/>
</LinearLayout>
main.activity
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MyRecyclerAdapter mRecyclerAdapter;
private ArrayList<FriendItem> mfriendItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
/* initiate adapter */
mRecyclerAdapter = new MyRecyclerAdapter();
/* initiate recyclerview */
mRecyclerView.setAdapter(mRecyclerAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL,false));
/* adapt data */
mfriendItems = new ArrayList<>();
for(int i=1;i<=10;i++){
if(i%2==0)
mfriendItems.add(new FriendItem(R.drawable.person,i+"번째 사람",i+"번째 상태메시지"));
else
mfriendItems.add(new FriendItem(R.drawable.person,i+"번째 사람",i+"번째 상태메시지"));
}
mRecyclerAdapter.setFriendList(mfriendItems);
}
}
MyRecyclerAdapter.activity
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder> {
private ArrayList<FriendItem> mFriendList;
@NonNull
@Override
public MyRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyRecyclerAdapter.ViewHolder holder, int position) {
holder.onBind(mFriendList.get(position));
}
public void setFriendList(ArrayList<FriendItem> list){
this.mFriendList = list;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return mFriendList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView profile;
TextView name;
TextView message;
public ViewHolder(@NonNull View itemView) {
super(itemView);
profile = (ImageView) itemView.findViewById(R.id.profile);
name = (TextView) itemView.findViewById(R.id.name);
message = (TextView) itemView.findViewById(R.id.message);
}
void onBind(FriendItem item){
profile.setImageResource(item.getResourceId());
name.setText(item.getName());
message.setText(item.getMessage());
}
}
}
FriendItem.java
public class FriendItem {
String name;
String message;
int resourceId;
public FriendItem(int resourceId, String name, String message) {
this.name = name;
this.message= message;
this.resourceId = resourceId;
}
public int getResourceId() {
return resourceId;
}
public String getMessage() {
return message;
}
public String getName() {
return name;
}
public void setMessage(String message) {
this.message = message;
}
public void setName(String name) {
this.name = name;
}
public void setResourceId(int resourceId) {
this.resourceId = resourceId;
}
}
결과 값
'Programming > Android Example' 카테고리의 다른 글
RecyclerView Image (0) | 2023.08.09 |
---|---|
SQLite 일기장 앱 (0) | 2023.08.09 |
Fragment 예제 (0) | 2023.08.08 |
SQLite 예제2 (0) | 2023.08.08 |
그리드 뷰 (0) | 2023.08.04 |