Ayush Chauhan
Ayush Chauhan

Reputation: 459

Heterogenous Layouts inside RecyclerView

I am developing a weather app in which i wanted to use two Views inside RecyclerView which is having CursorAdapter as its member. I want to use one View to display todays weather and other view to display other days weathers. My RecyclerView is working perfectly if I only use one View to display weather. I have also overwritten getItemViewType()? to get to know whichView` type I should inflate.

Code for getItemViewType():

private static int VIEW_TYPE_TODAY = 0;
private static int VIEW_TYPE_FUTURE_DAY = 1;

@Override
    public int getItemViewType(int position) {
        if(position == VIEW_TYPE_TODAY)
            return VIEW_TYPE_TODAY;
        else
            return VIEW_TYPE_FUTURE_DAY;
}

Code for the newView() of CursorAdapter which I have overwritten:

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    int viewType = getItemViewType(cursor.getPosition());
    int layoutId = -1;
    if(viewType==VIEW_TYPE_TODAY)
        layoutId = R.layout.list_item_forecast_today;
    else if(viewType==VIEW_TYPE_FUTURE_DAY)
        layoutId = R.layout.list_item_forecast;
    View view = LayoutInflater.from(context).inflate(layoutId, parent, false);
    return view;
}

No matter what the value of position in getItemViewType() is, the function is always returning VIEW_TYPE_TODAY.

Can some please tell what I am doing wrong?

Upvotes: 0

Views: 1272

Answers (2)

Bipin
Bipin

Reputation: 161

Here is the nice example of heterogenous recyclerview. I am not posting as solution but for those who are looking for this kind of recyclerview.Hope it helps.

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157487

overriding getItemViewType is not enough to have a ListView with heterogeneous rows. You need to override getViewTypeCount() as well, in order to return the number of heterogeneous rows you want to have (2 in your case). Please remember that your getItemViewType has to return continuos integers in the range [0, getViewTypeCount() -1]

Upvotes: 1

Related Questions