I Completed challenge without getItemViewType can some one explain what is the use of this method

My CimeListFragment class

public class CrimeListFragment extends Fragment {

private RecyclerView mRecyclerview;
CrimeAdapter mAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_crime_list,container,false);
    mRecyclerview = (RecyclerView) view.findViewById(R.id.crime_recycler_view);
    mRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));

    updateUI();
    return view;
}

private void updateUI() {
    CrimeLab crimeLab = CrimeLab.get(getActivity());
    List<Crime> crimes =  crimeLab.getCrimes();
    mAdapter = new CrimeAdapter(crimes);
    mRecyclerview.setAdapter(mAdapter);

}


private class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
   private TextView mTextview;
   private TextView mDateTextView;
    private Button mcontactPoliceButton;

    public CrimeHolder(LayoutInflater inflater,ViewGroup parent){
        super(inflater.inflate(R.layout.list_item_crime,parent,false));
        mTextview = (TextView) itemView.findViewById(R.id.title_item_view);
        mDateTextView = (TextView) itemView.findViewById(R.id.date_item_view);
        mcontactPoliceButton = (Button) itemView.findViewById(R.id.contact_police);
        itemView.setOnClickListener(this);
    }
    private void bind(Crime crime){
        mTextview.setText(crime.getmTitle());
        mDateTextView.setText(crime.getMdate().toString());
        if(crime.isMrequirePolice()){
            mcontactPoliceButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(),"contqacting police",Toast.LENGTH_SHORT).show();
                }
            });
        }
        else
            mcontactPoliceButton.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(getActivity(),"clicked an itemView",Toast.LENGTH_SHORT).show();
    }
}

private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{

    private List<Crime> mCrimes;

    public CrimeAdapter(List<Crime> crimes){
        mCrimes = crimes;
    }

    @Override
    public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        return new CrimeHolder(inflater,parent);
    }

    @Override
    public void onBindViewHolder(CrimeHolder holder, int position) {
          Crime crime = mCrimes.get(position);
        holder.bind(crime);
    }

    @Override
    public int getItemCount() {
        return mCrimes.size();
    }
}

}

I think the idea is that you have two layouts: list_item_crime and list_item_crime_police, and thus need two CrimeHolders. OnCreateViewHolder in CrimeAdapter returns a different CrimeHolder based on the viewType argument. You need to override the getItemViewType method to return the correct type based on the mRequiresPolice property you added to Crime. Hope this helps.

you are right beauty is that in adapter you can inflate the layout and pass in view directly to viewholder so when you use getIemViewType in adapter class depending on the result you get you inflate a respective view and pass it view holder so you have flexibility to use different layouts

HOW!? CrimeHolder has list_item_crime hard-coded into it, and there is no way to pass it a view. How do you do what you just said?

You know when you ask a question…and then you figure it out on your own…yeah. I totally missed that onCreateViewHolder calls CrimeHolder, so I can just add an argument to CrimeHolder to change views based on the getItemViewType logic

private int mNormalCrime = R.layout.list_item_crime;
private int mPoliceCrime = R.layout.police_required_crime;

@Override
    public int getItemViewType(int position) {
        if (mCrimes.get(position).isReqPolice()) {
            return mPoliceCrime;
        }
        else return mNormalCrime;
    }

    @Override
    public CrimeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        return new CrimeHolder(layoutInflater, parent, viewType);
    }

And while there may be a simpler way, I thought using the actual viewID was a nice simplification as I can use it both in the logic and as a parameter passed to CrimeHolder, simplifying the code.

And IT FREAKING WORKED. :slight_smile:

Thank you, I wasn’t getting getItemViewType at all! Maybe getItemViewId or maybe just getItemView would be a better name for the method.