1

I have code as :

@Override public CustomRatingHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate( R.layout.custom_rating_lyt, null); CustomRatingReviews ratingObj = (null != mRatingLst && position < mRatingLst.size()) ? mRatingLst.get(position) : null; CustomRatingHolder viewHolder = new CustomRatingHolder(mAdapContext, ratingObj, view); position++; return viewHolder; } 

I want current position inside the onCreateViewHolder of recycler adapter. Let anyone help me out of this. Thanks in Advance.

2 Answers 2

6

Not sure what you are expecting to achieve. onCreateViewHolder is called one time for each object in your collection in order to bind inflate the necessary layout and find all the views. You can inflate different layout and bind different ViewHolders based of the integer:onCreateViewHolder(ViewGroup parent, int viewType)

If you have different view types you need to override the getItemViewType method that will be called with the item position so you can determine what view type you need. This method returns an int that you will receive in onCreateViewHolder.

Here is a good example:

@Override public int getItemViewType(int position) { final Object dataObj = mDataSet.get(position); if (dataObj instanceof String) { return 0; } if (dataObj instanceof User) { return 1; } } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { switch(viewType) { case 0: return new StringViewHolder(...); case 1: return new UserViewHolder(...); } } 

I see in your code that you are sending the data in the constructor of the ViewHolder. That defeats the purpose of this class because you will have only one ViewHolder instance for an object type and it only needs to hold the layout view information so you don't need to find the views every time you need to bind an object. You actually need to use onBindViewHolder in order to bind your object, ex:

public void onBindViewHolder(UsersGroupsAdapter.ViewHolder holder, int position) { // or something similar holder.bind(mData.get(position)); } 

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

4

I think you need:

getAdapterPosition(); 

1 Comment

The getAdapterPosition is a method of the RecyclerView.ViewHolder abstract class not the RecyclerView.Adapter class.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.