Last active
December 20, 2017 18:32
-
-
Save dinorahtovar/bfc8bf3b41f70838beaa5d9089b6bda9 to your computer and use it in GitHub Desktop.
RecyclerView implementation with different holders and different files
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
/** | |
* Created by Dinorah Tovar on 12/20/17. | |
* RecyclerView Implementation inside Activity/Fragment | |
*/ | |
//... | |
RecyclerViewAdapter adapter = new RecyclerViewAdapter(); | |
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); | |
layoutManager.setOrientation(LinearLayoutManager.VERTICAL); | |
recyclerView.setLayoutManager(layoutManager); | |
recyclerView.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
/** | |
* Created by Dinorah Tovar on 12/20/17. | |
* RecyclerViewAdapter, for this example, the RecyclerViewAdapter need to fill two diferents Holders, thats the reason | |
* that getItemCount returns 2 | |
*/ | |
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { | |
public RecyclerViewAdapter () { | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return position; | |
} | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
if (viewType == 0) | |
return new FirstHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.holder, parent, false)); | |
else | |
return new FirstHoler(LayoutInflater.from(parent.getContext()).inflate(R.layout.holder, parent, false)); | |
} | |
@Override | |
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | |
if (position == 0) { | |
FirstHolder holderHeader = (FirstHolder) holder; | |
holderHeader.setValues(); | |
} else { | |
FirstHolder holderBody = (FirstHolder) holder; | |
holderBody.setValues(); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return 2; | |
} |
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
/** | |
* Created by Dinorah Tovar on 12/20/17. | |
* RecyclerViewHolder in separated files | |
* Butterknife options for inflate holder is avaliable with comments | |
*/ | |
public class FirstHolder extends RecyclerView.ViewHolder { | |
private TextView textView; | |
// @BindView(R.id.textView2) TextView ButterknifeTextView; | |
public FirstHolder(View itemView) { | |
super(itemView); | |
// ButterKnife.bind(this, itemView); | |
textView = (TextView) view.findViewById(R.id.textView1); | |
} | |
public void setValues () { | |
textView.setText ("Hello this is a RecyclerViewHolder") | |
// textView.setText ("Hello this is a RecyclerViewHolder inflated with Butteknife") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment