Skip to content

Instantly share code, notes, and snippets.

@jonfhancock
Last active February 25, 2021 07:00
Show Gist options
  • Save jonfhancock/c3adc35d39f4473a398005dd40e17e4a to your computer and use it in GitHub Desktop.
Save jonfhancock/c3adc35d39f4473a398005dd40e17e4a to your computer and use it in GitHub Desktop.
This set of classes demonstrates what a Not Dumb ViewHolder should look like. It lightens the load on the Adapter, and places decisions about what to do with user interactions on the Activity where it belongs.
public class ExcellentAdventure {
@Retention(SOURCE)
@StringDef({ERA_BC, ERA_AD})
public @interface Era {
}
public static final String ERA_BC = "BC";
public static final String ERA_AD = "AD";
private int year;
@Era private String era;
private String title;
private String lat;
private String lon;
private String locationName;
private String wikipediaTitle;
public ExcellentAdventure(int year, String era, String title,
String lat, String lon,
String locationName,
String wikipediaTitle) {
this.year = year;
this.era = era;
this.title = title;
this.lat = lat;
this.lon = lon;
this.locationName = locationName;
this.wikipediaTitle = wikipediaTitle;
}
public int getYear() { return year; }
@Era public String getEra() { return era; }
public String getTitle() { return title; }
public String getLat() { return lat; }
public String getLon() { return lon; }
public String getLocationName() { return locationName; }
public String getWikipediaTitle() { return wikipediaTitle; }
}
public class MainActivity extends AppCompatActivity implements SmartViewHolder.ExcellentAdventureListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
SuperChillAdapter adapter = new SuperChillAdapter(getLayoutInflater(),this);
recyclerView.setAdapter(adapter);
List<ExcellentAdventure> adventures = new ArrayList<>();
adventures.add(new ExcellentAdventure(420,ExcellentAdventure.ERA_BC,"Visit Socrates","37.97155","23.7245479","Parthenon, Athens, Greece", "Socrates"));
adventures.add(new ExcellentAdventure(1815,ExcellentAdventure.ERA_AD,"Battle of Waterloo","50.7120367","4.4143367","Waterloo, Belgium", "Battle_of_Waterloo"));
adapter.updateItems(adventures);
}
@Override
public void onMapClicked(ExcellentAdventure item) {
String url = String.format("geo:%s,%s?q=%s",item.getLat(),item.getLon(),item.getLocationName());
Uri gmmIntentUri = Uri.parse(url);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
}
@Override
public void onTitleClicked(ExcellentAdventure item) {
String url = "http://en.wikipedia.com/wiki/" + item.getWikipediaTitle();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
}
}
public class SmartViewHolder extends RecyclerView.ViewHolder {
TextView textTitle;
TextView textLocation;
TextView textDate;
ImageView mapIcon;
ExcellentAdventure item;
ExcellentAdventureListener listener;
public interface ExcellentAdventureListener{
void onMapClicked(ExcellentAdventure item);
void onTitleClicked(ExcellentAdventure item);
}
public SmartViewHolder(View itemView, final ExcellentAdventureListener listener) {
super(itemView);
this.listener = listener;
textTitle = (TextView) itemView.findViewById(R.id.text_title);
textLocation = (TextView) itemView.findViewById(R.id.text_location);
textDate = (TextView) itemView.findViewById(R.id.text_date);
mapIcon = (ImageView) itemView.findViewById(R.id.map_icon);
textTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onTitleClicked(item);
}
});
mapIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onMapClicked(item);
}
});
}
public void setItem(ExcellentAdventure item) {
this.item = item;
textTitle.setText(item.getTitle());
textLocation.setText(item.getLocationName());
textDate.setText(getFormattedDate(item));
}
private String getFormattedDate(ExcellentAdventure item) {
String date = item.getYear() + " " + item.getEra();
return date;
}
}
public class SuperChillAdapter extends RecyclerView.Adapter {
private List<ExcellentAdventure> items;
private LayoutInflater inflater;
private SmartViewHolder.ExcellentAdventureListener adventureListener;
public SuperChillAdapter(LayoutInflater inflater, SmartViewHolder.ExcellentAdventureListener adventureListener) {
this.inflater = inflater;
this.adventureListener = adventureListener;
items = new ArrayList<>();
}
public void updateItems(final List<ExcellentAdventure> newItems) {
final List<ExcellentAdventure> oldItems = new ArrayList<>(this.items);
this.items.clear();
if (newItems != null) {
this.items.addAll(newItems);
}
DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return oldItems.size();
}
@Override
public int getNewListSize() {
return items.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldItems.get(oldItemPosition).equals(newItems.get(newItemPosition));
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldItems.get(oldItemPosition).equals(newItems.get(newItemPosition));
}
}).dispatchUpdatesTo(this);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.item_excellent_adventure, parent, false);
return new SmartViewHolder(v,adventureListener);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
SmartViewHolder vh = (SmartViewHolder) holder;
vh.setItem(items.get(position));
}
@Override
public int getItemCount() {
return items.size();
}
}
@hetbo
Copy link

hetbo commented Feb 28, 2019

I just fixed it.
You should replace the ClickListeners from public SmartViewHolder to void SetItem.

@ResonantEdge
Copy link

I have a question. If you have a value that you do not want to set in a Text View or Image View but rather to an Int. How would you go about doing that in the onBindViewHolder?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment