Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LocationTypeListAdapter extends DataBoundListAdapter<LocationType, ListItemLocationTypeBinding> {
- private final androidx.databinding.DataBindingComponent dataBindingComponent;
- private final LocationTypeClickCallback locationTypeClickCallback;
- public LocationTypeListAdapter(androidx.databinding.DataBindingComponent dataBindingComponent, LocationTypeClickCallback locationTypeClickCallback){
- this.dataBindingComponent = dataBindingComponent;
- this.locationTypeClickCallback = locationTypeClickCallback;
- }
- @Override
- protected ListItemLocationTypeBinding createBinding(ViewGroup parent) {
- ListItemLocationTypeBinding binding = DataBindingUtil
- .inflate(LayoutInflater.from(parent.getContext()), R.layout.list_item_location_type,
- parent, false, dataBindingComponent);
- // binding.getRoot().setOnClickListener(v -> {
- // LocationType locationType = binding.getLocationType();
- // if (locationType != null && locationTypeClickCallback != null) {
- // locationTypeClickCallback.onClick(locationType);
- // }
- // }); // method 1, works but would have to be outside of clicking switch and instead any blank area
- // binding.locationTypeItemSwitch.setOnClickListener(v -> {
- // LocationType locationType = binding.getLocationType();
- // if (locationType != null && locationTypeClickCallback != null) {
- // locationTypeClickCallback.onClick(locationType);
- // }
- // }); // method 2: clicks random ones
- binding.locationTypeItemSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- LocationType locationType = binding.getLocationType();
- if (locationType != null && locationTypeClickCallback != null) {
- locationTypeClickCallback.onClick(locationType);
- }
- notifyDataSetChanged();
- }
- }); // method 3 clicks random ones per click
- return binding;
- }
- @Override
- protected void bind(ListItemLocationTypeBinding binding, LocationType item) {
- binding.setLocationType(item);
- }
- @Override
- protected boolean areItemsTheSame(LocationType oldItem, LocationType newItem) {
- return Objects.equals(oldItem.getEntityId(), newItem.getEntityId());
- }
- @Override
- protected boolean areContentsTheSame(LocationType oldItem, LocationType newItem) {
- return Objects.equals(oldItem.getTitle(), newItem.getTitle()) &&
- Objects.equals(oldItem.getIcon(), newItem.getIcon());
- }
- public interface LocationTypeClickCallback {
- void onClick(LocationType locationType);
- }
- }
- LocationTypeListAdapter cvAdapter = new LocationTypeListAdapter(dataBindingComponent,
- locationType -> setLocationTypeViewModel(locationType.getEntityId()));
- binding.get().locationTypeList.setAdapter(cvAdapter);
- adapter = new AutoClearedValue<>(this, cvAdapter);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement