리스트뷰 와 마찬가지로, 리싸이클러뷰 역시 어댑터 객체를 연결시켜줘야 이용 가능합니다. 리스트뷰 보다는 조금더 세팅해줘야 하는 부분들이 있어 단계별로 설명 드려 보겠습니다.
1) build.gradle (Module:app) 리싸이클러뷰 의존성 추가 현재 시점의 최신 버젼 등록 해주시면 됩니다.
dependencies {
...
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
2) XML 에 recyclerView 추가 하기 : id 와 방향 설정
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
/>
3) Activity 에서 Recycler 와 adpater 선언, recyclerAdpter 클래스 생성
RecyclerView recycler1;
recyclerAdpater adpater;
class recyclerAdpter extends RecyclerView.Adapter<recyclerAdpter.ViewHoler>{
}
4) Implement : onCreateViewHolder, onBindViewHolder, getItemCount
class recyclerAdpter extends RecyclerView.Adapter<recyclerAdpter.ViewHoler>{
@Override
public recyclerAdpter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(recyclerAdpter.ViewHoler holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}
5) ArrayList 객체 및 데이터 set
class recyclerAdpter extends RecyclerView.Adapter<recyclerAdpter.ViewHoler>{
ArrayList<TelItem> items = new ArrayList<TelItem>( );
@Override
public recyclerAdpter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(recyclerAdpter.ViewHoler holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}
6) 생성자 와 addItem 을 추가 합니다.
class recyclerAdpter extends RecyclerView.Adapter<recyclerAdpter.ViewHoler>{
ArrayList<TelItem> items = new ArrayList<TelItem>( );
public recyclerAdpter(ArrayList<TelItem> items) {
this.items = items;
}
public recyclerAdpter(){
;
}
public void addItem (TelItem item){
items.add( item );
}
@Override
public recyclerAdpter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(recyclerAdpter.ViewHoler holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}
7) public class ViewHolder extends RecyclerView.ViewHolder 를 만들고, ViewHolder를 overide 메소드한다.
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super( itemView );
}
}
8) ViewHolder 에 담을 item을 만들고 view 로 부터 전달받은 item을 ViewHolder에서 연결 한다.
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
EditText edName, edPhone;
public ViewHolder(@NonNull View itemView) {
super( itemView );
imageView = (ImageView) itemView.findViewById( R.id.imageView );
edName = (EditText) itemView.findViewById( R.id.edName );
edPhone = (EditText) itemView.findViewById( R.id.edPhone );
}
}
9) recyclerAdpter.ViewHolder 에 view 인플레이션 하기
public recyclerAdpter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService( LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.tellist, parent,false );
recyclerAdpter.ViewHolder viewHolder = new recyclerAdpter.ViewHolder( view );
return viewHolder;
}
10) onBindViewHolder : items 로부터 postion 값 인덱스로 holder로 돌려줌
public void onBindViewHolder(recyclerAdpter.ViewHolder holder, int position) {
TelItem item = items.get( position );
holder.imageView.setImageResource( item.getResId() );
holder.edName.setText( item.getName() );
holder.edPhone.setText( item.getPhone() );
}
11) getItemCount : items 사이즈
public int getItemCount() {
return items.size();
}
12) RecycleAdpater 전체 코드
class recyclerAdpter extends RecyclerView.Adapter<recyclerAdpter.ViewHolder>{
ArrayList<TelItem> items = new ArrayList<TelItem>( );
public recyclerAdpter(ArrayList<TelItem> items) {
this.items = items;
}
public recyclerAdpter(){
;
}
public void addItem (TelItem item){
items.add( item );
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
EditText edName, edPhone;
public ViewHolder(@NonNull View itemView) {
super( itemView );
imageView = (ImageView) itemView.findViewById( R.id.imageView );
edName = (EditText) itemView.findViewById( R.id.edName );
edPhone = (EditText) itemView.findViewById( R.id.edPhone );
}
}
@Override
public recyclerAdpter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService( LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.tellist, parent,false );
recyclerAdpter.ViewHolder viewHolder = new recyclerAdpter.ViewHolder( view );
return viewHolder;
}
@Override
public void onBindViewHolder(recyclerAdpter.ViewHolder holder, int position) {
TelItem item = items.get( position );
holder.imageView.setImageResource( item.getResId() );
holder.edName.setText( item.getName() );
holder.edPhone.setText( item.getPhone() );
}
@Override
public int getItemCount() {
return items.size();
}
}
13) 리싸이클러뷰 및 어댑터 설정 : 리싸이클러 LaytoutManager setting
RecyclerView recycler1;
recyclerAdpter adpter;
public onCreate(Bundle savedInstanceState) {
recycler1 =(RecyclerView) findViewById( R.id.recylerView );
recycler1.setLayoutManager( new LinearLayoutManager( getApplicationContext() ) );
adpter = new recyclerAdpter( );
recycler1.setAdapter( adpter );
}
14) adpter.addItem 을 통해서 item 을 추가 한다.
btEnter.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edName.getText().toString();
String phone = edPhone.getText().toString();
adpter.addItem( new TelItem( R.drawable.ic_launcher_foreground, name, phone ) );
}
} );
15) 전체 코드 뷰
public class Sub1Activity extends AppCompatActivity {
//TODO Define
FrameLayout container;
ImageView imageView;
Button btEnter;
EditText edName, edPhone;
ArrayList<TelItem> itemSet;
SharedPreferences pref;
SharedPreferences.Editor editor;
String name, phone;
Gson gson;
String json;
RecyclerView recycler1;
recyclerAdpter adpter;
@Override
protected void onPause() {
super.onPause();
String name = edName.getText().toString();
String phone = edPhone.getText().toString();
editor.putString( "name", name );
editor.putString( "phone", phone );
editor.apply();
}
@Override
protected void onResume() {
super.onResume();
//from pref string
edName.setText(pref.getString( "name", name));
edPhone.setText(pref.getString( "phone", phone ));
/*
//using json object
json = pref.getString( "list", null );
if(json != null) {
Type type = new TypeToken<ArrayList<TelItem>>() {
}.getType();
itemSet = gson.fromJson( json, type );
Object obj0 = itemSet.get( 0 );
Object obj1 = itemSet.get( 1 );
String names = (String) obj0;
String phones = (String) obj1;
edName.setText( names );
edPhone.setText( phones );
}
*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate( R.menu.main_menu, menu );
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int curId = item.getItemId();
switch (curId){
case R.id.refresh:
printToast( "새로고침 메뉴 선택됨" );
break;
case R.id.home:
printToast( "홈 메뉴 선택됨" );
screenChange(MainActivity.class, 100);
break;
case R.id.serch:
printToast( "검색 메뉴 선택됨" );
break;
default:
break;
}
return super.onOptionsItemSelected( item );
}
//TODO Oncreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_sub1 );
userEnterInfo();
}
//TODO SharedPreference 이용해서 저장하기, 추후 with Json and Gson 이용해서 저장하기
public void userEnterInfo(){
itemSet = new ArrayList<>();
pref = getSharedPreferences( "pref" , MODE_PRIVATE);
editor = pref.edit();
gson = new Gson();
imageView = (ImageView) findViewById( R.id.imageView );
edName = (EditText) findViewById( R.id.edName );
edPhone = (EditText) findViewById( R.id.edPhone );
btEnter = (Button) findViewById( R.id.btEnter );
recycler1 =(RecyclerView) findViewById( R.id.recylerView );
recycler1.setLayoutManager( new LinearLayoutManager( getApplicationContext() ) );
adpter = new recyclerAdpter( );
recycler1.setAdapter( adpter );
btEnter.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = edName.getText().toString();
String phone = edPhone.getText().toString();
adpter.addItem( new TelItem( R.drawable.ic_launcher_foreground, name, phone ) );
/* json and gson
json = gson.toJson(itemSet);
editor.putString( "list" , json);
editor.apply();
*/
}
} );
}
//TODO RecycleView Adpater
class recyclerAdpter extends RecyclerView.Adapter<recyclerAdpter.ViewHolder>{
ArrayList<TelItem> items = new ArrayList<TelItem>( );
public recyclerAdpter(ArrayList<TelItem> items) {
this.items = items;
}
public recyclerAdpter(){
;
}
public void addItem (TelItem item){
items.add( item );
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
EditText edName, edPhone;
public ViewHolder(@NonNull View itemView) {
super( itemView );
imageView = (ImageView) itemView.findViewById( R.id.imageView );
edName = (EditText) itemView.findViewById( R.id.edName );
edPhone = (EditText) itemView.findViewById( R.id.edPhone );
}
}
@Override
public recyclerAdpter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService( LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.tellist, parent,false );
recyclerAdpter.ViewHolder viewHolder = new recyclerAdpter.ViewHolder( view );
return viewHolder;
}
@Override
public void onBindViewHolder(recyclerAdpter.ViewHolder holder, int position) {
TelItem item = items.get( position );
holder.imageView.setImageResource( item.getResId() );
holder.edName.setText( item.getName() );
holder.edPhone.setText( item.getPhone() );
}
@Override
public int getItemCount() {
return items.size();
}
}
//TODO <1> UserAPI
public void screenChange(Class cls, int requsetCode){
Intent intent = new Intent(getApplicationContext(), cls );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
startActivityForResult( intent, requsetCode );
}
public void printToast(String data ){
Toast.makeText( this, data, Toast.LENGTH_LONG ).show();
}
}
5) 코드
'안드로이드 프로그래밍[JAVA Code] > 선택 위젯' 카테고리의 다른 글
선택 위젯 : 스피너 : 기본 Array Adapter 설정 (0) | 2019.12.04 |
---|---|
선택 위젯 : 리스트뷰&그리드뷰 : baseAdapter 정의 하기 (0) | 2019.12.04 |