activity_main.xml

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

item_style.xml

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

<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"
android:padding = "5dp"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="5dp">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text = "agency name"
android:textSize = "30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="contacts"
android:textColor = "#f0f"
android:textSize = "24sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Agent.java

public class Agent {
String name;
String telNumber;

public Agent(String name, String telNumber){
this.name = name;
this.telNumber = telNumber;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getTelNumber(){
return telNumber;
}
public void setTelNumber(String telNumber){
this.telNumber = telNumber;
}
}

AgentAdapter.java

public class AgentAdapter extends RecyclerView.Adapter<AgentAdapter.ViewHolder>{
ArrayList<Agent> items = new ArrayList<Agent>();

public ViewHolder onCreateViewHolder(ViewGroup vg, int viewType){
LayoutInflater inflater = LayoutInflater.from(vg.getContext());
View itemView = inflater.inflate(R.layout.item_style, vg, false);

//view 객채를 담아서 새로생성된 ViewHolder 객체를 리턴한다
return new ViewHolder(itemView);
}
public void onBindViewHolder(ViewHolder vh, int position){
Agent item = items.get(position);
vh.setItem(item);
}//재사용할때 호출되는 method
public int getItemCount(){
return items.size();
}
public void addItem(Agent item){
items.add(item);
}
public void setItem(ArrayList<Agent> items){
this.items = items;
}
public Agent getItem(int position){
return items.get(position);
}
public void setItem(int position, Agent item){
items.set(position, item);
}
static class ViewHolder extends RecyclerView.ViewHolder{
TextView textView1;
TextView textView2;

public ViewHolder(View itemView){
super(itemView);

textView1 = itemView.findViewById(R.id.textView1);
textView2 = itemView.findViewById(R.id.textView2);
}
//item setting
public void setItem(Agent item){
textView1.setText(item.getName());
textView2.setText(item.getTelNumber());
}
}
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

RecyclerView recyclerView = findViewById(R.id.recyclerView);

LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);

AgentAdapter adapter = new AgentAdapter();

adapter.addItem(new Agent("busan", "123123123"));
adapter.addItem(new Agent("hoho", "123123123"));
adapter.addItem(new Agent("hoho", "123123123"));
adapter.addItem(new Agent("hoho", "123123123"));
adapter.addItem(new Agent("hoho", "123123123"));

recyclerView.setAdapter(adapter);
}
}

'android studio' 카테고리의 다른 글

startActivityForResult()(화면전환)  (0) 2019.08.13
activity 추가하기  (0) 2019.08.13
custom dialog  (0) 2019.08.13
alert dialog  (0) 2019.08.13
방향전환 event로 activity 유지하기  (0) 2019.08.13

+ Recent posts