0

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?

yennsarah
  • 5,467
  • 2
  • 27
  • 48
Ayush Chauhan
  • 441
  • 7
  • 25

2 Answers2

1

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]

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thanks,but now instead of today weather tomorrow weather is inflating the Layout list_item_forecast_today because cursor.getPosition() is returning -1 instead of 0 – Ayush Chauhan Jul 11 '16 at 13:31
  • it is a bug in your current implementation. Overriding `getViewTypeCount` won't change the normal behaviour – Blackbelt Jul 11 '16 at 13:33
  • hey check out my code for recycler view adapter and please point out the bug in my implementation [link](http://stackoverflow.com/questions/38309177/recyclerview-not-correctly-displaying-heterogenous-layouts) – Ayush Chauhan Jul 11 '16 at 14:15
  • how are you loading the cursor? – Blackbelt Jul 11 '16 at 14:20
  • i am loading the cursor using loaders – Ayush Chauhan Jul 11 '16 at 14:36
  • `cursor.getPosition()` is returning -1 ? Try adding `if (cursor.getPosition() == -1) { cursor.moveToFirst(); }` as first thing in `newView` and check if it behaves correctly – Blackbelt Jul 11 '16 at 14:41
  • Now today weather is getting the correct layout but tomorrow and some random days are also getting the layout designed for today weather – Ayush Chauhan Jul 11 '16 at 14:52
0

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.

Bipin
  • 151
  • 1
  • 9