티스토리 뷰
build.gradle (Module: app)
dependencies {
// 리사이클러뷰 추가
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:id="@+id/txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="혀가 길지 않은 개발자"
android:textSize="30dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
RecyclerViewAdapter.java
package com.jwsoft.javaproject;
import android.content.Context;
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 RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private ArrayList<String> mData;
public RecyclerViewAdapter(ArrayList<String> arrData) {
mData = arrData;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
txtView = itemView.findViewById(R.id.txtView);
}
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.recyclerview_item, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
String strData = mData.get(position);
holder.txtView.setText(position + "번째 혀가 길지 않은 개발자");
}
@Override
public int getItemCount() {
return mData.size();
}
}
MainActivity.java
package com.jwsoft.javaproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arrListData = new ArrayList<>();
for (int i=0; i<100; i++) {
arrListData.add(Integer.toString(i));
}
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(
new LinearLayoutManager(getApplicationContext(),
RecyclerView.VERTICAL,
false));
RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(arrListData);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
}
}
'Android > Java' 카테고리의 다른 글
[Java] Handler.post (0) | 2020.06.27 |
---|---|
[Java] Handler (0) | 2020.06.27 |
[Java] Button 활용 (0) | 2020.06.11 |
[Java] TextView 접근 (0) | 2020.06.11 |
[Java] 변수 선언 및 할당 (0) | 2020.06.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- XML
- 안드로이드
- James Kim
- JSONArray
- Livedata
- Intent
- ViewModel
- handler
- Android
- 자바
- 혀가 길지 않은 개발자
- Kotlin
- CoordinatorLayout
- ArrayList
- TabLayout
- fragment
- Architecture Pattern
- 코틀린
- ViewPager2
- MVVM
- Vue.js #Vue.js + javascript
- DataBinding
- View
- activity
- 안드로이드 #코틀린 #Android #Kotlin
- Design Pattern
- recyclerview
- java
- coroutine
- JSONObject
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함