Last active
August 29, 2015 14:20
-
-
Save silmood/1fdbbec30e9e5295cb3d to your computer and use it in GitHub Desktop.
Marvel RecyclerView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Character { | |
| int id; | |
| String name; | |
| String description; | |
| Uri urlImage; | |
| int availableComics; | |
| int availableSeries; | |
| int availableStories; | |
| public int getId() { | |
| return id; | |
| } | |
| public void setId(int id) { | |
| this.id = id; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public String getDescription() { | |
| return description; | |
| } | |
| public void setDescription(String description) { | |
| this.description = description; | |
| } | |
| public Uri getUrlImage() { | |
| return urlImage; | |
| } | |
| public void setUrlImage(Uri urlImage) { | |
| this.urlImage = urlImage; | |
| } | |
| public int getAvailableComics() { | |
| return availableComics; | |
| } | |
| public void setAvailableComics(int availableComics) { | |
| this.availableComics = availableComics; | |
| } | |
| public int getAvailableSeries() { | |
| return availableSeries; | |
| } | |
| public void setAvailableSeries(int availableSeries) { | |
| this.availableSeries = availableSeries; | |
| } | |
| public int getAvailableStories() { | |
| return availableStories; | |
| } | |
| public void setAvailableStories(int availableStories) { | |
| this.availableStories = availableStories; | |
| } | |
| @Override | |
| public String toString() { | |
| String character = name + | |
| "\n Description:" + description + | |
| "\n img:"+ urlImage + | |
| "\n available comics:"+ availableComics + | |
| "\n available series:"+ availableSeries + | |
| "\n available stories:"+ availableStories; | |
| return character; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CharactersFragment extends Fragment{ | |
| private static final String LOG_TAG = CharactersFragment.class.getCanonicalName(); | |
| public Context CONTEXT; | |
| @InjectView(R.id.list_heroes) | |
| RecyclerView mListHeroes; | |
| CharactersListAdapter adapter; | |
| public CharactersFragment() { | |
| // Required empty public constructor | |
| } | |
| @Override | |
| public void onAttach(Activity activity) { | |
| super.onAttach(activity); | |
| CONTEXT = activity; | |
| } | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| } | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| Bundle savedInstanceState) { | |
| View rootView = inflater.inflate(R.layout.fragment_characters, container, false); | |
| ButterKnife.inject(this, rootView); | |
| initListHeroes(); | |
| return rootView; | |
| } | |
| @Override | |
| public void onResume() { | |
| super.onResume(); | |
| } | |
| //================================================================================ | |
| //Init Methods | |
| //================================================================================ | |
| private void initListHeroes() { | |
| LinearLayoutManager lm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL , false); | |
| adapter = new CharactersListAdapter(CONTEXT); | |
| mListHeroes.setLayoutManager(lm); | |
| mListHeroes.setAdapter(adapter); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context; | |
| import android.net.Uri; | |
| import android.support.v7.widget.RecyclerView; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.TextView; | |
| import com.facebook.drawee.view.SimpleDraweeView; | |
| import java.util.Collections; | |
| import java.util.List; | |
| import butterknife.ButterKnife; | |
| import butterknife.InjectView; | |
| import devf.co.devfmarvelapplication.R; | |
| import devf.co.devfmarvelapplication.model.Character; | |
| import devf.co.devfmarvelapplication.rest.Constants; | |
| import devf.co.devfmarvelapplication.rest.MarvelApiClient; | |
| import devf.co.devfmarvelapplication.rest.models.CharactersListResponse; | |
| import retrofit.Callback; | |
| import retrofit.RetrofitError; | |
| import retrofit.client.Response; | |
| import static java.util.Collections.*; | |
| public class CharactersListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
| List <Character> characters = EMPTY_LIST; | |
| Context context; | |
| /** | |
| * @param context Context needed to access {@link android.view.LayoutInflater} | |
| * */ | |
| public CharactersListAdapter(Context context) { | |
| this.context = context; | |
| } | |
| public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { | |
| View itemView = LayoutInflater.from(context) | |
| .inflate(R.layout.item_character, viewGroup, false); | |
| return new CharacterViewHolder(itemView); | |
| } | |
| @Override | |
| public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { | |
| if(viewHolder instanceof CharacterViewHolder) { | |
| Character currentCharacter = characters.get(position); | |
| ((CharacterViewHolder)viewHolder).setImg(currentCharacter.getUrlImage()); | |
| ((CharacterViewHolder)viewHolder).setName(currentCharacter.getName()); | |
| } | |
| } | |
| @Override | |
| public int getItemCount() { | |
| if (characters == null) | |
| return 0; | |
| return characters.size(); | |
| } | |
| /** | |
| * Replace the current adapter data and replace it with a new collection. | |
| * @param characters New {@link devf.co.devfmarvelapplication.model.Character} collection | |
| * */ | |
| public void updateList(List<Character> characters) { | |
| this.characters = characters; | |
| notifyDataSetChanged(); | |
| } | |
| /** | |
| * Add a bunch of items to the current characters list | |
| * @param characters items to add | |
| * */ | |
| public void addItemCollection(List<Character> characters) { | |
| this.characters.addAll(characters); | |
| notifyDataSetChanged(); | |
| } | |
| public class CharacterViewHolder extends RecyclerView.ViewHolder{ | |
| @InjectView(R.id.img_character) | |
| SimpleDraweeView imgCharacter; | |
| @InjectView(R.id.txt_character_name) | |
| TextView txtName; | |
| public CharacterViewHolder(View itemView) { | |
| super(itemView); | |
| ButterKnife.inject(this,itemView); | |
| } | |
| public void setImg(Uri urlImage) { | |
| if(!urlImage.equals(Uri.EMPTY)) | |
| imgCharacter.setImageURI(urlImage); | |
| } | |
| public void setName(String name){ | |
| txtName.setText(name); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| compile 'com.android.support:recyclerview-v7:+' | |
| compile 'com.android.support:cardview-v7:21.0.+' | |
| compile 'com.android.support:appcompat-v7:22.0.0' | |
| compile 'com.squareup.retrofit:retrofit:1.9.0' | |
| compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0' | |
| compile 'com.squareup.okhttp:okhttp:2.0.0' | |
| compile 'com.jakewharton:butterknife:6.1.0' | |
| compile 'com.facebook.fresco:fresco:0.3.0+' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment