Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. public class LocationTypeListAdapter extends DataBoundListAdapter<LocationType, ListItemLocationTypeBinding> {
  2. private final androidx.databinding.DataBindingComponent dataBindingComponent;
  3. private final LocationTypeClickCallback locationTypeClickCallback;
  4.  
  5. public LocationTypeListAdapter(androidx.databinding.DataBindingComponent dataBindingComponent, LocationTypeClickCallback locationTypeClickCallback){
  6. this.dataBindingComponent = dataBindingComponent;
  7. this.locationTypeClickCallback = locationTypeClickCallback;
  8. }
  9.  
  10. @Override
  11. protected ListItemLocationTypeBinding createBinding(ViewGroup parent) {
  12. ListItemLocationTypeBinding binding = DataBindingUtil
  13. .inflate(LayoutInflater.from(parent.getContext()), R.layout.list_item_location_type,
  14. parent, false, dataBindingComponent);
  15. // binding.getRoot().setOnClickListener(v -> {
  16. // LocationType locationType = binding.getLocationType();
  17. // if (locationType != null && locationTypeClickCallback != null) {
  18. // locationTypeClickCallback.onClick(locationType);
  19. // }
  20. // }); // method 1, works but would have to be outside of clicking switch and instead any blank area
  21. // binding.locationTypeItemSwitch.setOnClickListener(v -> {
  22. // LocationType locationType = binding.getLocationType();
  23. // if (locationType != null && locationTypeClickCallback != null) {
  24. // locationTypeClickCallback.onClick(locationType);
  25. // }
  26. // }); // method 2: clicks random ones
  27. binding.locationTypeItemSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  28. @Override
  29. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  30. LocationType locationType = binding.getLocationType();
  31. if (locationType != null && locationTypeClickCallback != null) {
  32. locationTypeClickCallback.onClick(locationType);
  33. }
  34. notifyDataSetChanged();
  35. }
  36. }); // method 3 clicks random ones per click
  37. return binding;
  38. }
  39.  
  40. @Override
  41. protected void bind(ListItemLocationTypeBinding binding, LocationType item) {
  42. binding.setLocationType(item);
  43. }
  44.  
  45. @Override
  46. protected boolean areItemsTheSame(LocationType oldItem, LocationType newItem) {
  47. return Objects.equals(oldItem.getEntityId(), newItem.getEntityId());
  48. }
  49.  
  50. @Override
  51. protected boolean areContentsTheSame(LocationType oldItem, LocationType newItem) {
  52. return Objects.equals(oldItem.getTitle(), newItem.getTitle()) &&
  53. Objects.equals(oldItem.getIcon(), newItem.getIcon());
  54. }
  55.  
  56. public interface LocationTypeClickCallback {
  57. void onClick(LocationType locationType);
  58. }
  59. }
  60.  
  61. LocationTypeListAdapter cvAdapter = new LocationTypeListAdapter(dataBindingComponent,
  62. locationType -> setLocationTypeViewModel(locationType.getEntityId()));
  63. binding.get().locationTypeList.setAdapter(cvAdapter);
  64. adapter = new AutoClearedValue<>(this, cvAdapter);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement