반응형
main2.java
package doubleslash05.mini.team11;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity2 extends AppCompatActivity {
private ImageButton im1,im2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setTitle("채팅");
im1 = (ImageButton) findViewById(R.id.im1);
im2 = (ImageButton) findViewById(R.id.im2);
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);
im1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent_01 = new Intent(getApplication(), sub.class);
startActivity(intent_01);
}
});
im2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent_02 = new Intent(getApplication(), MainActivity2.class);
startActivity(intent_02);
}
});
}
}
sub.java
package doubleslash05.mini.team11;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class sub extends AppCompatActivity {
private ImageButton im3,im4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
setTitle("친구");
im3 = (ImageButton) findViewById(R.id.im3);
im4=(ImageButton) findViewById(R.id.im4);
RecyclerView recyclerView2 = findViewById(R.id.recyclerView2);
LinearLayoutManager layoutManager2 =
new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerView2.setLayoutManager(layoutManager2);
FriendAdapter adapter2 = new FriendAdapter();
adapter2.addItem(new Friend("김영수","좋음",R.drawable.person,"발라드"));
adapter2.addItem(new Friend("박민석","슬픔",R.drawable.person1,"힙합"));
adapter2.addItem(new Friend("홍길동","나쁨",R.drawable.person2,"잔잔한노래"));
adapter2.addItem(new Friend("김민석","슬픔",R.drawable.person1,"힙합"));
adapter2.addItem(new Friend("백민석","슬픔",R.drawable.person,"발라드"));
adapter2.addItem(new Friend("김민석","슬픔",R.drawable.person1,"힙합"));
adapter2.addItem(new Friend("홍길동","좋음",R.drawable.person2,"발라드"));
adapter2.addItem(new Friend("홍길동","나쁨",R.drawable.person,"발라드"));
adapter2.addItem(new Friend("홍길동","좋음",R.drawable.person1,"7080"));
recyclerView2.setAdapter(adapter2);
im3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent_01 = new Intent(getApplication(), sub.class);
startActivity(intent_01);
}
});
im4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent_02 = new Intent(getApplication(), MainActivity2.class);
startActivity(intent_02);
}
});
}
}
FriendAdapter
package doubleslash05.mini.team11;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class FriendAdapter extends RecyclerView.Adapter<FriendAdapter.ViewHolder> {
ArrayList<Friend> items= new ArrayList<>();
@NonNull
@Override
public FriendAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater2 = LayoutInflater.from(parent.getContext());
View itemview2= inflater2.inflate(R.layout.friend_item,parent,false);
return new ViewHolder(itemview2); /*여기있는 ViewHolder */
}
@Override
public void onBindViewHolder(@NonNull FriendAdapter.ViewHolder holder, int position) {
Friend item = items.get(position);
holder.setItem(item);
}
@Override
public int getItemCount() {
return items.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
TextView tv1,tv2,tv3;
ImageView iv1;
public ViewHolder(View itemView){
super(itemView);
tv1 = itemView.findViewById(R.id.text1);
tv2=itemView.findViewById(R.id.text2);
tv3 = itemView.findViewById(R.id.text3);
iv1 = itemView.findViewById(R.id.image1);
}
public void setItem(Friend item){
tv1.setText(item.getF_name());
tv2.setText(item.getF_music());
tv3.setText(item.getF_state());
iv1.setImageResource(item.getF_iv());
}
}
public void addItem(Friend item){
items.add(item);
}
public void setItems(ArrayList<Friend>items){
this.items = items;
}
public Friend getItem(int position){
return items.get(position);
}
public void setItem(int position, Friend item){
items.set(position,item);
}
}
friend_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/image1"
android:layout_width="60dp"
android:layout_height="60dp"
app:srcCompat="@drawable/person"
android:padding="5dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이름"
android:textSize="20sp"/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="음악"
android:textSize="20dp"
android:gravity="right"/>
</RelativeLayout>
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="상태메시지"
android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
friend.java
package doubleslash05.mini.team11;
import android.widget.ImageView;
public class Friend {
private String f_name;
private String f_state;
private int f_iv;
private String f_music;
public Friend (String f_name,String f_state,int f_iv,String f_music)
{
this.f_name=f_name;
this.f_state=f_state;
this.f_iv = f_iv;
this.f_music = f_music;
}
public String getF_name(){
return f_name;
}
public void setF_name(){
this.f_name=f_name;
}
public String getF_state(){
return f_state;
}
public void setF_state(){
this.f_state=f_state;
}
public int getF_iv(){
return f_iv;
}
public void setF_iv(int f_iv1){
this.f_iv=f_iv1;
}
public String getF_music(){
return f_music;
}
public void setF_music(){
this.f_music=f_music;
}
}
personAdapter
package doubleslash05.mini.team11;
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{
private TextView textView;
private TextView textView2;
private 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.getrechat());
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>
person.java
package doubleslash05.mini.team11;
public class Person {
String name;
String rechat;
String date;
public Person(String name, String rechat,String date) {
this.name = name;
this.rechat = rechat;
this.date= date;
}
public String getName(){
return name;
}
public void setName(){
this.name=name;
}
public String getrechat(){
return rechat;
}
public void setrechat(){
this.rechat=rechat;
}
public String getDate(){
return date;
}
public void setDate(){
this.date=date;
}
}
반응형
'Programming > Android' 카테고리의 다른 글
안드로이드 로또번호 생성기 소스 (0) | 2021.08.17 |
---|---|
안드로이드 textview null값 체크하기, 안드로이드 버튼 디자인 사이트 (0) | 2021.08.17 |
안드로이드 시간/날짜 예약 예제 (0) | 2021.08.17 |
안드로이드 리사이클러뷰(RecyclerView) 예제 (0) | 2021.08.17 |
안드로이드 리스트뷰(listview) 예제 (0) | 2021.08.17 |