꾸준히 안타치기
리사이클러뷰 본문
반응형
액티비티
package com.example.myapplication.theme;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
// 메뉴 - 테마 설정
// 유저가 배경 컬러를 선택할 수 있다.
// 리사이클러뷰 사용
public class theme_activity extends AppCompatActivity {
RecyclerView recyclerView;
theme_adapter adapter;
ImageView iv_back;
TextView tv_circle_purple;
TextView tv_circle_pink;
TextView tv_circle_orange;
TextView tv_circle_yellow;
TextView tv_circle_blue;
TextView tv_circle_green;
// 리스트 추가 카운트
int num = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_theme);
// 뒤로가기버튼 클릭시
iv_back = findViewById(R.id.iv_back);
iv_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
//리사이클러뷰 설정
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
// 리사이클러뷰 매니저 설정 (세로방향 리스트)
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
//theme_adapter 생성
adapter = new theme_adapter(getApplicationContext());
// 초기에 만들어져있는 리스트
adapter.addItem(new theme_item("기본컬러", R.drawable.circle_purple));
recyclerView.setAdapter(adapter);
// 한번 클릭시 수정
adapter.setOnItemClickListener(new theme_adapter.OnItemClickListener() {
@Override
public void onItemClick(theme_adapter.ViewHolder holder, View view, int position) {
// 아이템의 위치를 받아온다.
theme_item item = adapter.getItem(position);
// 수정하기 팝업 생성
AlertDialog.Builder modify_pop = new AlertDialog.Builder(theme_activity.this);
modify_pop.setTitle("리스트 수정");
modify_pop.setMessage("리스트를 수정하세요.");
// 목표명 적는 edit
EditText et_modify = new EditText(theme_activity.this);
modify_pop.setView(et_modify);
Log.e(" 디버깅.", "positon : " + position + " ");
// 확인 버튼
modify_pop.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e(" 디버깅.", "positon : " + position + " ");
// 수정하기 작성창에 작성한 내용.
String title = et_modify.getText().toString();
// 수정내용 + 현재 클릭한 위치값의 이미지가져와서 넣기
theme_item data = new theme_item(title, (adapter.getItem(position).resId));
adapter.modify(position, data);
// 어답터 갱신
adapter.notifyDataSetChanged();
// 창끄기
dialogInterface.dismiss();
}
});
// 취소하기 버튼
modify_pop.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
modify_pop.show();
}
});
// 롱클릭시 삭제
adapter.setOnItemLongClickListener(new theme_adapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(int position) {
// 삭제할지 팝업으로 묻기
AlertDialog.Builder del_pop = new AlertDialog.Builder(theme_activity.this);
del_pop.setTitle("리스트 삭제");
del_pop.setMessage("삭제하시겠습니까?");
// 확인 버튼
del_pop.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// 리스트 삭제
adapter.delete(theme_adapter.position);
// 어답터 갱신
adapter.notifyDataSetChanged();
dialogInterface.dismiss();
}
});
// 취소하기 버튼
del_pop.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
// 시간을 기록하는 코드넣기
}
});
del_pop.show();
// 롱클릭후 클릭은 처리 안되도록함, 중복사용하려면 false
return true;
}
});
// 하단 컬러 추가 버튼 모음 ------------------------------------------------------------------------------------------------------------
// 보라색 컬러 추가 버튼
tv_circle_purple = (TextView) findViewById(R.id.tv_circle_purple);
tv_circle_purple.setClickable(true);
tv_circle_purple.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(" 디버깅.", "positon : " + theme_adapter.position + " ");
num++;
// 리스트를 추가하고, 갱신
adapter.addItem(new theme_item("추가" + num, R.drawable.circle_purple));
adapter.notifyDataSetChanged();
// 토스트 메시지 - 수정완료
//theme_item item = adapter.getItem(theme_adapter.position);
Toast.makeText(getApplicationContext(), "보라 추가", Toast.LENGTH_LONG).show();
}
});
// 그린컬러 추가 버튼
tv_circle_green = (TextView) findViewById(R.id.tv_circle_green);
tv_circle_green.setClickable(true);
tv_circle_green.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(" 디버깅.", "positon : " + theme_adapter.position + " ");
num++;
// 리스트를 추가하고, 갱신
adapter.addItem(new theme_item("추가" + num, R.drawable.circle_green));
adapter.notifyDataSetChanged();
// 토스트 메시지 - 수정완료
//theme_item item = adapter.getItem(theme_adapter.position);
Toast.makeText(getApplicationContext(), "녹색 추가", Toast.LENGTH_LONG).show();
}
});
// 노랑 컬러 추가 버튼
tv_circle_yellow = (TextView) findViewById(R.id.tv_circle_yellow);
tv_circle_yellow.setClickable(true);
tv_circle_yellow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(" 디버깅.", "positon : " + theme_adapter.position + " ");
num++;
// 리스트를 추가하고, 갱신
adapter.addItem(new theme_item("추가" + num, R.drawable.circle_yellow));
adapter.notifyDataSetChanged();
// 토스트 메시지 - 수정완료
//theme_item item = adapter.getItem(theme_adapter.position);
Toast.makeText(getApplicationContext(), "노랑 추가", Toast.LENGTH_LONG).show();
}
});
// 주황 컬러 추가 버튼
tv_circle_orange = (TextView) findViewById(R.id.tv_circle_orange);
tv_circle_orange.setClickable(true);
tv_circle_orange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(" 디버깅.", "positon : " + theme_adapter.position + " ");
num++;
// 리스트를 추가하고, 갱신
adapter.addItem(new theme_item("추가" + num, R.drawable.circle_orange));
adapter.notifyDataSetChanged();
// 토스트 메시지 - 수정완료
//theme_item item = adapter.getItem(theme_adapter.position);
Toast.makeText(getApplicationContext(), "오렌지 추가", Toast.LENGTH_LONG).show();
}
});
// 핑크 컬러 추가 버튼
tv_circle_pink = (TextView) findViewById(R.id.tv_circle_pink);
tv_circle_pink.setClickable(true);
tv_circle_pink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(" 디버깅.", "positon : " + theme_adapter.position + " ");
num++;
// 리스트를 추가하고, 갱신
adapter.addItem(new theme_item("추가" + num, R.drawable.circle_pink));
adapter.notifyDataSetChanged();
// 토스트 메시지 - 수정완료
//theme_item item = adapter.getItem(theme_adapter.position);
Toast.makeText(getApplicationContext(), "핑크 추가", Toast.LENGTH_LONG).show();
}
});
// 블루 컬러 추가 버튼
tv_circle_blue = (TextView) findViewById(R.id.tv_circle_blue);
tv_circle_blue.setClickable(true);
tv_circle_blue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(" 디버깅.", "positon : " + theme_adapter.position + " ");
num++;
// 리스트를 추가하고, 갱신
adapter.addItem(new theme_item("추가" + num, R.drawable.circle_blue));
adapter.notifyDataSetChanged();
// 토스트 메시지 - 수정완료
//theme_item item = adapter.getItem(theme_adapter.position);
Toast.makeText(getApplicationContext(), "블루 추가", Toast.LENGTH_LONG).show();
}
});
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
// 테먀가 ~로 설정되었습니다. 토스트
// 테마 컬러를 ~로 변경하시겠습니까? 팝업
}
@Override
protected void onPause() {
super.onPause();
// 저장안된 변경 내용들을 저장한다.
// 테마변경 사항 저장
}
@Override
protected void onStop() {
super.onStop();
// onCreate에 작성된 내용 종료
}
@Override
protected void onDestroy() {
super.onDestroy();
// 강제종료
}
@Override
protected void onRestart() {
super.onRestart();
// 마지막으로 설정된 컬러 테마를 가져옴
}
}
어답터
package com.example.myapplication.theme;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.R;
import java.util.ArrayList;
// 테마 리사이클러뷰 어답터
public class theme_adapter extends RecyclerView.Adapter<theme_adapter.ViewHolder> {
public static int position;
Context context;
ArrayList<theme_item> items = new ArrayList<theme_item>();
OnItemClickListener listener;
OnItemLongClickListener listener_long;
// 인터페이스로 정의 (한번 클릭)
public static interface OnItemClickListener {
public void onItemClick(ViewHolder holder, View view, int position);
}
// 인터페이스로 정의 (롱클릭)
public static interface OnItemLongClickListener {
boolean onItemLongClick(int position);
}
public theme_adapter(Context context) {
this.context = context;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.theme_item, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
theme_item item = items.get(position);
holder.setItem(item);
holder.setOnItemClickListener(listener);
holder.setOnItemLongClickListener(listener_long);
}
public void addItem(theme_item item) {
items.add(item);
}
public void addItems(ArrayList<theme_item> items) {
this.items = items;
}
public theme_item getItem(int position) {
return items.get(position);
}
// 삭제하기
public void delete(int position) {
items.remove(position);
}
// 수정하기
// 현재 위치값과. 새로쓴 텍스트내용 가져옴
public void modify(int position, theme_item data) {
// 새로 작성한 내용을, 선택한 아이템의 포지션값에 넣는다.
// items.get(position).name = write.name;
for (int i = 0; i < items.size(); i++) {
if (i == (position)) {
items.set(i, data);
}
}
}
// 리스너 등록하기
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public void setOnItemLongClickListener(OnItemLongClickListener listener_long) {
this.listener_long = listener_long;
}
// 뷰홀더 설정
static class ViewHolder extends RecyclerView.ViewHolder {
TextView tv_title;
ImageView tv_circle;
// 클릭 리스너 등록하기
OnItemClickListener listener;
OnItemLongClickListener listener_long;
public ViewHolder(View itemView) {
super(itemView);
// theme_item 에서 텍스트뷰 찾아오기
tv_title = (TextView) itemView.findViewById(R.id.tv_title);
tv_circle = (ImageView) itemView.findViewById(R.id.tv_circle);
// theme_item 아이템뷰 박스 영역 아이디값을 정해주고 가져온다.
itemView.findViewById(R.id.tv_container);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if (listener != null) {
listener.onItemClick(ViewHolder.this, view, position);
}
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int position = getAdapterPosition();
if (listener_long != null) {
listener_long.onItemLongClick(position);
Log.e("lee", position + "");
}
return true;
}
});
}
// 데이터 담아주기
public void setItem(theme_item item) {
// 컬러텍스트
tv_title.setText(item.getTitle());
// 컬러이미지
tv_circle.setImageResource(item.getCircle());
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public void setOnItemLongClickListener(OnItemLongClickListener listener_long) {
this.listener_long = listener_long;
}
}
}
아이템 데이터
package com.example.myapplication.theme;
// 테마 컬러 리스트에 대한 아이템 데이터
public class theme_item {
String title; // 테마타이틀
int resId; // 상단 원 이미지
public theme_item(String title, int resId) {
this.title = title;
this.resId = resId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getCircle() {
return resId;
}
public void setCircle(int resId) {
this.resId = resId;
}
@Override
public String toString() {
return "theme_item{" +
"title='" + title + '\'' +
'}';
}
}
아이템 xml 리스트
<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:clipToPadding="false"
android:keepScreenOn="true"
android:orientation="vertical"
tools:ignore="ExtraText,MissingDefaultResource"
tools:layout_editor_absoluteY="25dp"
>
<RelativeLayout
android:id="@+id/note_title_area"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#ffffff"
tools:ignore="MissingConstraints">
<!-- 뒤로가기 아이콘-->
<ImageView
android:id="@+id/iv_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp"
android:src="@drawable/ic_back_arrow_gray" />
<!--타이틀-->
<TextView
android:id="@+id/tv_note_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:gravity="center"
android:paddingTop="20dp"
android:text="@string/theme_title"
android:textColor="#333333"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<!-- 리사이클러뷰 설정 -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:background="#f2f2f2"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="400dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:gravity="center">
<!--녹색원-->
<TextView
android:id="@+id/tv_circle_green"
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:layout_marginTop="20dp"
android:background="@drawable/circle_green"
android:gravity="center"
android:text="녹색"
android:textColor="#ffffff" />
<!--보라원-->
<TextView
android:id="@+id/tv_circle_purple"
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="@drawable/circle_purple"
android:gravity="center"
android:text="보라"
android:textColor="#ffffff" />
<!--주황원-->
<TextView
android:id="@+id/tv_circle_orange"
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="@drawable/circle_orange"
android:gravity="center"
android:text="주황"
android:textColor="#ffffff" />
<!--노랑원-->
<TextView
android:id="@+id/tv_circle_yellow"
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="@drawable/circle_yellow"
android:gravity="center"
android:text="노랑"
android:textColor="#ffffff" />
<!--핑크원-->
<TextView
android:id="@+id/tv_circle_pink"
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="@drawable/circle_pink"
android:gravity="center"
android:text="핑크"
android:textColor="#ffffff" />
<!--노랑원-->
<TextView
android:id="@+id/tv_circle_blue"
android:layout_width="50dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_gravity="left"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:background="@drawable/circle_blue"
android:gravity="center"
android:text="파랑"
android:textColor="#ffffff" />
</LinearLayout>
<!--추가하기 버튼-->
<TextView
android:id="@+id/tv_color_add"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/btn_line"
android:drawableLeft="@drawable/ic_add"
android:drawablePadding="5dip"
android:gravity="left|center_vertical"
android:paddingStart="18dp"
android:paddingLeft="18dp"
android:text="컬러 추가하기"
android:textColor="#999999"
android:textSize="16sp"
android:textStyle="bold"
android:visibility="invisible"
/>
</LinearLayout>
아이템 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"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/tv_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:background="@drawable/btn_login"
>
<!--원-->
<ImageView
android:id="@+id/tv_circle"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="@drawable/circle_green"
android:gravity="center"
android:textColor="#ffffff" />
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:gravity="center"
android:text="테마 컬러"
android:textColor="#333333"
android:textSize="24dp" />
</RelativeLayout>
</LinearLayout>
반응형
'Android _ 서버연동_채팅 > Android - UI' 카테고리의 다른 글
팝업 메뉴 /popupmenu (0) | 2021.06.16 |
---|---|
가입 체크, 이메일, 이름, 비번 (0) | 2021.05.17 |
Bottom popup / color 선택 변경하기 / 노트 (0) | 2021.02.24 |
카카오톡 연동 API사용 (0) | 2021.02.22 |
달력 / 오늘날짜 표기 / 특정날짜 표기 / (0) | 2021.02.21 |
Comments