2019. 1. 28. 09:54ㆍCoding/Android Studio
요런거 만들꺼임
RecyclerView 만들때 필요한것
-java
MainActivity.java (RecyclerView가 들어가는 부분)
TestItem.java
TestViewHolder.java
TestAdapter.java
-xml
activity_main.xml (RecyclerView가 들어가는 부분)
item_test.xml
-build.gradle(Module:app)
implementation 'com.android.support:recyclerview-v7:28.0.0'
( v7:28.0.0 버전 부분은 알아서 본인 버전에 맞게...)
(최신버전은 https://developer.android.com/guide/topics/ui/layout/recyclerview 여기로...)
-더미데이터(json형식)
[{"name":"마리오", "age":42, "gender":"남", "job":"배관공"},{"name":"피카츄", "age":5, "gender":"여", "job":"발전기"},{"name":"루이지", "age":38, "gender":"남", "job":"배관공"},{"name":"찌리리공", "age":1, "gender":"남", "job":"배터리"},{"name":"네이버", "age":20, "gender":"남", "job":"초록색"},{"name":"다음", "age":22, "gender":"남", "job":"파랑색"},{"name":"카카오", "age":5, "gender":"여", "job":"노란색"},{"name":"구글", "age":30, "gender":"남", "job":"빨노초파"},{"name":"야후", "age":42, "gender":"여", "job":"빨간색"},{"name":"라이코스", "age":15, "gender":"여", "job":"멍멍이"},{"name":"네이트", "age":38, "gender":"여", "job":"주작"}]
MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.google.gson.Gson;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView rv_main;
private String tempData =
"[" +
"{\"name\":\"마리오\", \"age\":42, \"gender\":\"남\", \"job\":\"배관공\"}," +
"{\"name\":\"피카츄\", \"age\":5, \"gender\":\"여\", \"job\":\"발전기\"}," +
"{\"name\":\"루이지\", \"age\":38, \"gender\":\"남\", \"job\":\"배관공\"}," +
"{\"name\":\"찌리리공\", \"age\":1, \"gender\":\"남\", \"job\":\"배터리\"}," +
"{\"name\":\"네이버\", \"age\":20, \"gender\":\"남\", \"job\":\"초록색\"}," +
"{\"name\":\"다음\", \"age\":22, \"gender\":\"남\", \"job\":\"파랑색\"}," +
"{\"name\":\"카카오\", \"age\":5, \"gender\":\"여\", \"job\":\"노란색\"}," +
"{\"name\":\"구글\", \"age\":30, \"gender\":\"남\", \"job\":\"빨노초파\"}," +
"{\"name\":\"야후\", \"age\":42, \"gender\":\"여\", \"job\":\"빨간색\"}," +
"{\"name\":\"라이코스\", \"age\":15, \"gender\":\"여\", \"job\":\"멍멍이\"}," +
"{\"name\":\"네이트\", \"age\":28, \"gender\":\"여\", \"job\":\"주작\"}" +
"]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
setData();
}
private void init() {
rv_main = findViewById(R.id.rv_main);
}
private void setData() {
Gson gson = new Gson();
ArrayList<TestItem> itemList = new ArrayList<>();
TestItem[] items = gson.fromJson(tempData, TestItem[].class);
for(TestItem item : items) {
itemList.add(item);
}
TestAdapter adapter = new TestAdapter(getApplicationContext(), itemList);
rv_main.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
rv_main.setAdapter(adapter);
}
}
activity_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">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
TestItem.java
public class TestItem {
String name;
int age;
String gender;
String job;
public TestItem(String name, int age, String gender, String job) {
this.name = name;
this.age = age;
this.gender = gender;
this.job = job;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
item_test.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="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:gravity="center"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="name"/>
<TextView
android:id="@+id/tv_age"
android:gravity="center"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="age"/>
<TextView
android:id="@+id/tv_gender"
android:gravity="center"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="gender"/>
<TextView
android:id="@+id/tv_job"
android:gravity="center"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="job"/>
</LinearLayout>
TestViewHolder.java
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
public class TestViewHolder extends RecyclerView.ViewHolder {
public TextView tv_name, tv_age, tv_gender, tv_job;
public TestViewHolder(@NonNull View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_name);
tv_age = itemView.findViewById(R.id.tv_age);
tv_gender = itemView.findViewById(R.id.tv_gender);
tv_job = itemView.findViewById(R.id.tv_job);
}
}
TestAdapter.java
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
public class TestAdapter extends RecyclerView.Adapter<TestViewHolder> {
private Context mContext;
private ArrayList<TestItem> itemList;
public TestAdapter(Context mContext, ArrayList<TestItem> itemList) {
this.mContext = mContext;
this.itemList = itemList;
}
@NonNull
@Override
public TestViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View baseView = View.inflate(mContext, R.layout.item_test,null);
TestViewHolder testViewHolder = new TestViewHolder(baseView);
return testViewHolder;
}
@Override
public void onBindViewHolder(@NonNull TestViewHolder testViewHolder, int position) {
TestItem item = itemList.get(position);
testViewHolder.tv_name.setText(item.getName());
testViewHolder.tv_age.setText(item.getAge()+"");
testViewHolder.tv_gender.setText(item.getGender());
testViewHolder.tv_job.setText(item.getJob());
}
@Override
public int getItemCount() {
return itemList.size();
}
}
build.gradle(Module:app)
dependencies {
.
.
.
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.google.code.gson:gson:2.8.5'
}
전체코드가 없으니 헷깔리는거 같아서 다시 작성...
'Coding > Android Studio' 카테고리의 다른 글
[Error]Dialog에서 Unable to add window -- token null is not for an application 뜰때 (0) | 2019.03.19 |
---|---|
android studio 무선으로 연결하기(wifi 디버깅) (0) | 2019.02.08 |
Adapter에서 Activity finish()하기 (1) | 2019.01.24 |
Android Activity Stack 확인하기 (0) | 2019.01.21 |
ProgressBar의 Color 바꾸기 (0) | 2019.01.19 |