뷰를 재활용하면서 무한으로 리스트 뷰를 생성할 수 있는 커스텀적인 리스트 뷰
null != arraylistMusic이라는 조건식이 참(true)이라면 ?뒤에 이어지는 첫 번째 arralistMusic.size()를 수행하게 되고 반대로 거짓(false)이라면 0을 반환
.java 파일 만드는 법
좌측 app 폴더에 java 파일 클릭
com.example.recyclerview 폴더 우클릭
new > Java Class
-------------------------------------------------
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarFadeDuration="0"
android:scrollbarSize="5dp"
android:scrollbarThumbVertical="@android:color/darker_gray"
android:scrollbars="vertical"
android:layout_weight="1">
</androidx.recyclerview.widget.RecyclerView>
<Button
android:id="@+id/btn_add"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="추가"/>
</LinearLayout>
-------------------------------------------------
item_list.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="khon"/>
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="리사이클러뷰"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
-------------------------------------------------
MainData.java
package com.example.recyclerview;
public class MainData {
private int iv_profile;
private String tv_name;
private String tv_content;
// alt + ins 키 constructor
public MainData(int iv_profile, String tv_name, String tv_content) {
this.iv_profile = iv_profile;
this.tv_name = tv_name;
this.tv_content = tv_content;
}
// alt + ins 키 Getter and Setter
public int getIv_profile() {
return iv_profile;
}
public void setIv_profile(int iv_profile) {
this.iv_profile = iv_profile;
}
public String getTv_name() {
return tv_name;
}
public void setTv_name(String tv_name) {
this.tv_name = tv_name;
}
public String getTv_content() {
return tv_content;
}
public void setTv_content(String tv_content) {
this.tv_content = tv_content;
}
}
-------------------------------------------------
MainAdapter.java
package com.example.recyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.CusteomViewHolder> {
private ArrayList<MainData> arrayList;
// alt + ins 키 comstructor
public MainAdapter(ArrayList<MainData> arrayList) {
this.arrayList = arrayList;
}
@NonNull
@Override
public MainAdapter.CusteomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
CusteomViewHolder holder = new CusteomViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull final MainAdapter.CusteomViewHolder holder, int position) {
holder.iv_prifile.setImageResource(arrayList.get(position).getIv_profile());
holder.tv_name.setText(arrayList.get(position).getTv_name());
holder.tv_content.setText(arrayList.get(position).getTv_content());
holder.itemView.setTag(position);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String curName = holder.tv_name.getText().toString();
Toast.makeText(view.getContext(), curName, Toast.LENGTH_SHORT).show();
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
remove(holder.getAdapterPosition());
return true;
}
});
}
@Override
public int getItemCount() {
return (null != arrayList ? arrayList.size() : 0);
}
public void remove(int position) {
try {
arrayList.remove(position);
notifyItemRemoved(position);
}catch (IndexOutOfBoundsException ex) {
ex.printStackTrace(); // 예외 상황이 벌어졌을때 강제 실행
}
}
public class CusteomViewHolder extends RecyclerView.ViewHolder {
protected ImageView iv_prifile;
protected TextView tv_name;
protected TextView tv_content;
public CusteomViewHolder(@NonNull View itemView) {
super(itemView);
this.iv_prifile = (ImageView) itemView.findViewById(R.id.iv_profile);
this.tv_name = (TextView) itemView.findViewById(R.id.tv_name);
this.tv_content = (TextView) itemView.findViewById(R.id.tv_content);
}
}
}
-------------------------------------------------
build.gradle (:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "com.example.recyclerview"
minSdkVersion 15
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
-------------------------------------------------
MainActivity.java
package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<MainData> arrayList;
private MainAdapter mainAdapter;
private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.rv);
linearLayoutManger = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManger);
arrayList = new ArrayList<>();
mainAdapter = new MainAdapter(arrayList);
recyclerView.setAdapter(mainAdapter);
Button btn_add = (Button)findViewById(R.id.btn_add);
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainData mainData = new MainData(R.mipmap.ic_launcher,"khon", "리사이클러뷰");
arrayList.add(mainData);
mainAdapter.notifyDataSetChanged();
}
});
}
}
'안드로이드 앱 만들기' 카테고리의 다른 글
Log 출력 및 주석 (0) | 2020.08.17 |
---|---|
Fragment (0) | 2020.08.17 |
카메라 (0) | 2020.08.13 |
Navigation Menu 커스텀 (0) | 2020.08.09 |
Web View (0) | 2020.08.06 |