Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/src/main/java/com/example/yeseul/movieapp/pojo/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,12 @@ public String getUserRating() {
public void setUserRating(String userRating) {
this.userRating = userRating;
}

@Override
public boolean equals(Object obj) {
if(obj instanceof Movie) {
return this.getTitle().equals(((Movie) obj).getTitle());
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.yeseul.movieapp.utils;

import android.support.v7.util.DiffUtil;

import java.util.List;

public class POJODiffUtil<T> extends DiffUtil.Callback {

private final List<T> mOldList;
private final List<T> mNewList;

public POJODiffUtil(List<T> oldList, List<T> newList) {
this.mOldList = oldList;
this.mNewList = newList;
}

@Override
public int getOldListSize() {
return mOldList.size();
}

@Override
public int getNewListSize() {
return mNewList.size();
}

@Override
public boolean areItemsTheSame(int i, int i1) {
return mOldList.get(i).equals(mNewList.get(i1));
}

@Override
public boolean areContentsTheSame(int i, int i1) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;

import com.example.yeseul.movieapp.utils.POJODiffUtil;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseRecyclerAdapter<T, H extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements AdapterContract.View, AdapterContract.Model<T>{
public abstract class BaseRecyclerAdapter<T, H extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements AdapterContract.View, AdapterContract.Model<T> {

protected List<T> itemList;
protected OnItemClickListener onItemClickListener;
protected Context context;

public BaseRecyclerAdapter(Context context){
public BaseRecyclerAdapter(Context context) {
this.context = context;
}

Expand All @@ -22,7 +25,7 @@ public BaseRecyclerAdapter(Context context, List<T> itemList) {
this.itemList = itemList;
}

public Context getContext(){
public Context getContext() {
return context;
}

Expand All @@ -38,19 +41,29 @@ public long getItemId(int position) {

@Override
public int getItemCount() {
if(this.itemList == null) {
if (this.itemList == null) {
return 0;
}

return this.itemList.size();
}

private void diffUtilUpdate(List<T> oldList, List<T> newList) {
POJODiffUtil<T> diffUTilCallback = new POJODiffUtil<>(oldList, newList);
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffUTilCallback);

this.itemList.clear();
this.itemList.addAll(newList);

diffResult.dispatchUpdatesTo(this);
}

/**
* 해당 position 의 item 반환
* */
*/
@Override
public T getItem(int position){
if(this.itemList == null){
public T getItem(int position) {
if (this.itemList == null) {
return null;
}

Expand All @@ -59,10 +72,10 @@ public T getItem(int position){

/**
* 전체 item list 반환
* */
*/
@Override
public List<T> getItemList(){
if(this.itemList == null){
public List<T> getItemList() {
if (this.itemList == null) {
return null;
}

Expand All @@ -71,112 +84,114 @@ public List<T> getItemList(){

/**
* item list 전체 수정
* */
public void updateItems(List<T> items){
if(this.itemList == null){
itemList = new ArrayList<>();
}
this.itemList.clear();
this.itemList.addAll(items);

notifyDataSetChanged();
*/
public void updateItems(List<T> items) {
diffUtilUpdate(this.itemList, items);
}

/**
* 해당 position 의 item 수정
* */
public void updateItem(int position, T item){
if(this.itemList == null){
*/
public void updateItem(int position, T item) {
if (this.itemList == null) {
return;
}
if(position > this.itemList.size()){
if (position > this.itemList.size()) {
return;
}
this.itemList.remove(position);
this.itemList.add(position, item);
List<T> newItemsList = new ArrayList<>(itemList);
newItemsList.remove(position);
newItemsList.add(position, item);

notifyItemChanged(position);
diffUtilUpdate(this.itemList, newItemsList);
}

/**
* 맨 처음 item list 초기화 또는 ,
* item list 마지막 position 뒤에 추가
* */
*/
@Override
public void addItems(List<T> items){
public void addItems(List<T> items) {
if (this.itemList == null) {
this.itemList = items;
notifyDataSetChanged();
} else {
int position = this.itemList.size();
this.itemList.addAll(items);
notifyItemRangeInserted(position, items.size());
List<T> newItemList = new ArrayList<>(this.itemList);
newItemList.addAll(items);
diffUtilUpdate(this.itemList, newItemList);
}
}

/**
* position 위치에 items 추가
* */
public void addItems(int position, List<T> items){
if(this.itemList == null){
*/
public void addItems(int position, List<T> items) {
if (this.itemList == null) {
this.itemList = new ArrayList<>();
}
if(position > this.itemList.size()){
if (position > this.itemList.size()) {
return;
}
this.itemList.addAll(position, items);
List<T> newItemList = new ArrayList<>(this.itemList);
newItemList.addAll(position, items);

notifyItemRangeInserted(position, items.size());
diffUtilUpdate(this.itemList, newItemList);
}

/**
* item list 마지막 position 뒤에 item 추가
* */
*/
@Override
public void addItem(T item){
public void addItem(T item) {
if (this.itemList == null) {
this.itemList = new ArrayList<>();
itemList.add(item);
notifyDataSetChanged();
} else {
int position = this.itemList.size();
this.itemList.add(item);
notifyItemInserted(position);
List<T> newItemList = new ArrayList<>(this.itemList);
newItemList.add(item);

diffUtilUpdate(this.itemList, newItemList);
}
}

/**
* position 위치에 item 추가
* */
public void addItem(int position, T item){
if(this.itemList == null){
*/
public void addItem(int position, T item) {
if (this.itemList == null) {
return;
}
if(position > this.itemList.size()){
if (position > this.itemList.size()) {
return;
}
this.itemList.add(position, item);
notifyItemInserted(position);
List<T> newItemList = new ArrayList<>(this.itemList);
newItemList.add(position, item);

diffUtilUpdate(this.itemList, newItemList);
}

/**
* item list 전체 삭제
* */
*/
@Override
public void clearItems(){
if(itemList != null){
public void clearItems() {
if (itemList != null) {
itemList.clear();
notifyDataSetChanged();
}
}

/**
* position 위치의 item 삭제
* */
*/
@Override
public void removeItem(int position) {
if (this.itemList != null && position < this.itemList.size()) {
this.itemList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, this.itemList.size());
List<T> newItemList = new ArrayList<>(this.itemList);
newItemList.remove(position);

diffUtilUpdate(this.itemList, newItemList);
}
}

Expand All @@ -187,7 +202,7 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int
holder.itemView.setOnClickListener(view -> {

// item click listener 등록
if(onItemClickListener != null) {
if (onItemClickListener != null) {
onItemClickListener.onItemClick(position);
}

Expand All @@ -198,3 +213,4 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int

protected abstract void onBindView(H holder, int position);
}