Android Open Source - Android-CleanArchitecture Users Adapter






From Project

Back to project page Android-CleanArchitecture.

License

The source code is released under:

Apache License

If you think the Android project Android-CleanArchitecture listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright (C) 2014 android10.org. All rights reserved.
 * @author Fernando Cejas (the android10 coder)
 *///  w  w  w. jav  a 2s  .c om
package com.fernandocejas.android10.sample.presentation.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.fernandocejas.android10.sample.presentation.R;
import com.fernandocejas.android10.sample.presentation.model.UserModel;
import java.util.Collection;
import java.util.List;

/**
 * Adaptar that manages a collection of {@link UserModel}.
 */
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.UserViewHolder> {

  public interface OnItemClickListener {
    void onUserItemClicked(UserModel userModel);
  }

  private List<UserModel> usersCollection;
  private final LayoutInflater layoutInflater;

  private OnItemClickListener onItemClickListener;

  public UsersAdapter(Context context, Collection<UserModel> usersCollection) {
    this.validateUsersCollection(usersCollection);
    this.layoutInflater =
        (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.usersCollection = (List<UserModel>) usersCollection;
  }

  @Override public int getItemCount() {
    return (this.usersCollection != null) ? this.usersCollection.size() : 0;
  }

  @Override public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = this.layoutInflater.inflate(R.layout.row_user, parent, false);
    UserViewHolder userViewHolder = new UserViewHolder(view);

    return userViewHolder;
  }

  @Override public void onBindViewHolder(UserViewHolder holder, final int position) {
    final UserModel userModel = this.usersCollection.get(position);
    holder.textViewTitle.setText(userModel.getFullName());
    holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (UsersAdapter.this.onItemClickListener != null) {
          UsersAdapter.this.onItemClickListener.onUserItemClicked(userModel);
        }
      }
    });
  }

  @Override public long getItemId(int position) {
    return position;
  }

  public void setUsersCollection(Collection<UserModel> usersCollection) {
    this.validateUsersCollection(usersCollection);
    this.usersCollection = (List<UserModel>) usersCollection;
    this.notifyDataSetChanged();
  }

  public void setOnItemClickListener (OnItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
  }

  private void validateUsersCollection(Collection<UserModel> usersCollection) {
    if (usersCollection == null) {
      throw new IllegalArgumentException("The list cannot be null");
    }
  }

  static class UserViewHolder extends RecyclerView.ViewHolder {
    @InjectView(R.id.title) TextView textViewTitle;

    public UserViewHolder(View itemView) {
      super(itemView);
      ButterKnife.inject(this, itemView);
    }
  }
}




Java Source Code List

com.fernandocejas.android10.sample.data.cache.FileManager.java
com.fernandocejas.android10.sample.data.cache.UserCacheImpl.java
com.fernandocejas.android10.sample.data.cache.UserCache.java
com.fernandocejas.android10.sample.data.cache.serializer.JsonSerializer.java
com.fernandocejas.android10.sample.data.entity.UserEntity.java
com.fernandocejas.android10.sample.data.entity.mapper.UserEntityDataMapper.java
com.fernandocejas.android10.sample.data.entity.mapper.UserEntityJsonMapper.java
com.fernandocejas.android10.sample.data.exception.NetworkConnectionException.java
com.fernandocejas.android10.sample.data.exception.RepositoryErrorBundle.java
com.fernandocejas.android10.sample.data.exception.UserNotFoundException.java
com.fernandocejas.android10.sample.data.executor.JobExecutor.java
com.fernandocejas.android10.sample.data.net.ApiConnection.java
com.fernandocejas.android10.sample.data.net.RestApiImpl.java
com.fernandocejas.android10.sample.data.net.RestApi.java
com.fernandocejas.android10.sample.data.repository.UserDataRepository.java
com.fernandocejas.android10.sample.data.repository.datasource.CloudUserDataStore.java
com.fernandocejas.android10.sample.data.repository.datasource.DiskUserDataStore.java
com.fernandocejas.android10.sample.data.repository.datasource.UserDataStoreFactory.java
com.fernandocejas.android10.sample.data.repository.datasource.UserDataStore.java
com.fernandocejas.android10.sample.domain.User.java
com.fernandocejas.android10.sample.domain.exception.ErrorBundle.java
com.fernandocejas.android10.sample.domain.executor.PostExecutionThread.java
com.fernandocejas.android10.sample.domain.executor.ThreadExecutor.java
com.fernandocejas.android10.sample.domain.interactor.GetUserDetailsUseCaseImpl.java
com.fernandocejas.android10.sample.domain.interactor.GetUserDetailsUseCase.java
com.fernandocejas.android10.sample.domain.interactor.GetUserListUseCaseImpl.java
com.fernandocejas.android10.sample.domain.interactor.GetUserListUseCase.java
com.fernandocejas.android10.sample.domain.interactor.Interactor.java
com.fernandocejas.android10.sample.domain.repository.UserRepository.java
com.fernandocejas.android10.sample.presentation.UIThread.java
com.fernandocejas.android10.sample.presentation.exception.ErrorMessageFactory.java
com.fernandocejas.android10.sample.presentation.mapper.UserModelDataMapper.java
com.fernandocejas.android10.sample.presentation.model.UserModel.java
com.fernandocejas.android10.sample.presentation.navigation.Navigator.java
com.fernandocejas.android10.sample.presentation.presenter.Presenter.java
com.fernandocejas.android10.sample.presentation.presenter.UserDetailsPresenter.java
com.fernandocejas.android10.sample.presentation.presenter.UserListPresenter.java
com.fernandocejas.android10.sample.presentation.view.LoadDataView.java
com.fernandocejas.android10.sample.presentation.view.UserDetailsView.java
com.fernandocejas.android10.sample.presentation.view.UserListView.java
com.fernandocejas.android10.sample.presentation.view.activity.BaseActivity.java
com.fernandocejas.android10.sample.presentation.view.activity.MainActivity.java
com.fernandocejas.android10.sample.presentation.view.activity.UserDetailsActivity.java
com.fernandocejas.android10.sample.presentation.view.activity.UserListActivity.java
com.fernandocejas.android10.sample.presentation.view.adapter.UsersAdapter.java
com.fernandocejas.android10.sample.presentation.view.adapter.UsersLayoutManager.java
com.fernandocejas.android10.sample.presentation.view.component.AutoLoadImageView.java
com.fernandocejas.android10.sample.presentation.view.fragment.BaseFragment.java
com.fernandocejas.android10.sample.presentation.view.fragment.UserDetailsFragment.java
com.fernandocejas.android10.sample.presentation.view.fragment.UserListFragment.java