getItemViewType is returned only in onBindViewHolder but not in onCreateViewHolderRecyclerView inside ScrollView is not workingHow to add footer to recyclerView by means supportLibraryRecyclerView onCreateViewHolder Return Type Incompatibility With Multiple Custom ViewHoldersinflating view inside OnbindViewHolderchild recyclerview methods is not calling (onCreateViewHolder, onBindViewHolder). But getItemCount() method is callingReturn getItemViewType based on database attributeRecyclerView calling onCreateViewHolder and onBindViewHolder for all items at onceRecyclerView taking a lot of time when instaceiating in onCreateViewHolder and onBindViewHolder: Progressbar freezesRecycler View getItemViewType position issueWhy onBindViewHolder index isn't incrementing in Recycler View?

How to query field names from custom object by data type?

How does the search space affect the speed of an ILP solver?

What is the motivation behind designing a control stick that does not move?

How would a disabled person earn their living in a medieval-type town?

How smart contract transactions work?

New coworker has strange workplace requirements - how should I deal with them?

Moscow SVO airport, how to avoid scam taxis without pre-booking?

A vector is defined to have a magnitude and *a* direction, but the zero vector has no *single* direction. So, how is the zero vector a vector?

Can I leave a large suitcase at TPE during a 4-hour layover, and pick it up 4.5 days later when I come back to TPE on my way to Taipei downtown?

Divide Numbers by 0

Four day weekend?

Is this statement about a motion being simple harmonic in nature strong?

A word for the urge to do the opposite

How could reincarnation magic be limited to prevent overuse?

Can a pet cat attune to a magical item?

I was given someone else's visa, stamped in my passport

Fishing from underwater domes

Turn off Google Chrome's Notification for "Flash Player will no longer be supported after December 2020."

In what language did Túrin converse with Mím?

Do universities maintain secret textbooks?

How to get frequency counts using column breaks by row?

German equivalent to "going down the rabbit hole"

Does the telecom provider need physical access to the SIM card to clone it?

Calculate Landau's function



getItemViewType is returned only in onBindViewHolder but not in onCreateViewHolder


RecyclerView inside ScrollView is not workingHow to add footer to recyclerView by means supportLibraryRecyclerView onCreateViewHolder Return Type Incompatibility With Multiple Custom ViewHoldersinflating view inside OnbindViewHolderchild recyclerview methods is not calling (onCreateViewHolder, onBindViewHolder). But getItemCount() method is callingReturn getItemViewType based on database attributeRecyclerView calling onCreateViewHolder and onBindViewHolder for all items at onceRecyclerView taking a lot of time when instaceiating in onCreateViewHolder and onBindViewHolder: Progressbar freezesRecycler View getItemViewType position issueWhy onBindViewHolder index isn't incrementing in Recycler View?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a RecyclerView adapter that should inflate 4 different layouts depending on what getItemViewType returns.



Each view type should be returned when the view is triggered, but the problem is one of the types does not return inside the onCreateViewHolder, but returns only in onBindViewHolder, thus preventing the ViewHolder from being created. Also I assure you getItemCount returns just the correct size of the data, so that should not be the problem.



I figure if the view types can be returned successfully they should show up in both methods when called. So this issue just doesn't make any sense to me.



@NonNull
@Override
public HorizontalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i)
Log.d(SLIDER_TAG, "onCreateViewHolder: " + getItemViewType(i));
View cardView = LayoutInflater.from(parent.getContext()).inflate(
getItemViewType(i) == 0 ? R.layout.item_category_slider_viewed
: getItemViewType(i) == 1 ? R.layout.item_category_slider_added
: getItemViewType(i) == 2 ? R.layout.item_category_slider_browse_all
: R.layout.item_category_slider_regular
, parent, false);
return new HorizontalViewHolder(cardView, context);



When logging the getItemViewType(i) only 0, 1, and 3 are ever returned inside the onCreateViewHolder but not 2.



But strangely, logging that inside the onBindViewHolder returns all the view types from 0 - 3. Why is that the case?




EDIT



The RecyclerView displays a horizontal list of cards (about 20) while all but the last card (blank) uses the same layout, so only 2 view types are used in this specific list case, we can ignore the other 2 types for now. Here the last card is not inflated, thus was never called in the onCreateViewHolder. I'm suspecting that while the first many cards were inflated using the same layout, the layouts are not created again so it assumes that the last card uses the same layout.










share|improve this question


























  • Can you share more code from the recyclerView? are you implements multiple viewholders?

    – bensadiku
    Mar 28 at 0:20











  • No only one ViewHolder.

    – NocTurn
    Mar 28 at 0:25

















0















I have a RecyclerView adapter that should inflate 4 different layouts depending on what getItemViewType returns.



Each view type should be returned when the view is triggered, but the problem is one of the types does not return inside the onCreateViewHolder, but returns only in onBindViewHolder, thus preventing the ViewHolder from being created. Also I assure you getItemCount returns just the correct size of the data, so that should not be the problem.



I figure if the view types can be returned successfully they should show up in both methods when called. So this issue just doesn't make any sense to me.



@NonNull
@Override
public HorizontalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i)
Log.d(SLIDER_TAG, "onCreateViewHolder: " + getItemViewType(i));
View cardView = LayoutInflater.from(parent.getContext()).inflate(
getItemViewType(i) == 0 ? R.layout.item_category_slider_viewed
: getItemViewType(i) == 1 ? R.layout.item_category_slider_added
: getItemViewType(i) == 2 ? R.layout.item_category_slider_browse_all
: R.layout.item_category_slider_regular
, parent, false);
return new HorizontalViewHolder(cardView, context);



When logging the getItemViewType(i) only 0, 1, and 3 are ever returned inside the onCreateViewHolder but not 2.



But strangely, logging that inside the onBindViewHolder returns all the view types from 0 - 3. Why is that the case?




EDIT



The RecyclerView displays a horizontal list of cards (about 20) while all but the last card (blank) uses the same layout, so only 2 view types are used in this specific list case, we can ignore the other 2 types for now. Here the last card is not inflated, thus was never called in the onCreateViewHolder. I'm suspecting that while the first many cards were inflated using the same layout, the layouts are not created again so it assumes that the last card uses the same layout.










share|improve this question


























  • Can you share more code from the recyclerView? are you implements multiple viewholders?

    – bensadiku
    Mar 28 at 0:20











  • No only one ViewHolder.

    – NocTurn
    Mar 28 at 0:25













0












0








0








I have a RecyclerView adapter that should inflate 4 different layouts depending on what getItemViewType returns.



Each view type should be returned when the view is triggered, but the problem is one of the types does not return inside the onCreateViewHolder, but returns only in onBindViewHolder, thus preventing the ViewHolder from being created. Also I assure you getItemCount returns just the correct size of the data, so that should not be the problem.



I figure if the view types can be returned successfully they should show up in both methods when called. So this issue just doesn't make any sense to me.



@NonNull
@Override
public HorizontalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i)
Log.d(SLIDER_TAG, "onCreateViewHolder: " + getItemViewType(i));
View cardView = LayoutInflater.from(parent.getContext()).inflate(
getItemViewType(i) == 0 ? R.layout.item_category_slider_viewed
: getItemViewType(i) == 1 ? R.layout.item_category_slider_added
: getItemViewType(i) == 2 ? R.layout.item_category_slider_browse_all
: R.layout.item_category_slider_regular
, parent, false);
return new HorizontalViewHolder(cardView, context);



When logging the getItemViewType(i) only 0, 1, and 3 are ever returned inside the onCreateViewHolder but not 2.



But strangely, logging that inside the onBindViewHolder returns all the view types from 0 - 3. Why is that the case?




EDIT



The RecyclerView displays a horizontal list of cards (about 20) while all but the last card (blank) uses the same layout, so only 2 view types are used in this specific list case, we can ignore the other 2 types for now. Here the last card is not inflated, thus was never called in the onCreateViewHolder. I'm suspecting that while the first many cards were inflated using the same layout, the layouts are not created again so it assumes that the last card uses the same layout.










share|improve this question
















I have a RecyclerView adapter that should inflate 4 different layouts depending on what getItemViewType returns.



Each view type should be returned when the view is triggered, but the problem is one of the types does not return inside the onCreateViewHolder, but returns only in onBindViewHolder, thus preventing the ViewHolder from being created. Also I assure you getItemCount returns just the correct size of the data, so that should not be the problem.



I figure if the view types can be returned successfully they should show up in both methods when called. So this issue just doesn't make any sense to me.



@NonNull
@Override
public HorizontalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i)
Log.d(SLIDER_TAG, "onCreateViewHolder: " + getItemViewType(i));
View cardView = LayoutInflater.from(parent.getContext()).inflate(
getItemViewType(i) == 0 ? R.layout.item_category_slider_viewed
: getItemViewType(i) == 1 ? R.layout.item_category_slider_added
: getItemViewType(i) == 2 ? R.layout.item_category_slider_browse_all
: R.layout.item_category_slider_regular
, parent, false);
return new HorizontalViewHolder(cardView, context);



When logging the getItemViewType(i) only 0, 1, and 3 are ever returned inside the onCreateViewHolder but not 2.



But strangely, logging that inside the onBindViewHolder returns all the view types from 0 - 3. Why is that the case?




EDIT



The RecyclerView displays a horizontal list of cards (about 20) while all but the last card (blank) uses the same layout, so only 2 view types are used in this specific list case, we can ignore the other 2 types for now. Here the last card is not inflated, thus was never called in the onCreateViewHolder. I'm suspecting that while the first many cards were inflated using the same layout, the layouts are not created again so it assumes that the last card uses the same layout.







java android android-recyclerview recycler-adapter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 2:31









ישו אוהב אותך

18.2k8 gold badges44 silver badges65 bronze badges




18.2k8 gold badges44 silver badges65 bronze badges










asked Mar 28 at 0:06









NocTurnNocTurn

3622 silver badges10 bronze badges




3622 silver badges10 bronze badges















  • Can you share more code from the recyclerView? are you implements multiple viewholders?

    – bensadiku
    Mar 28 at 0:20











  • No only one ViewHolder.

    – NocTurn
    Mar 28 at 0:25

















  • Can you share more code from the recyclerView? are you implements multiple viewholders?

    – bensadiku
    Mar 28 at 0:20











  • No only one ViewHolder.

    – NocTurn
    Mar 28 at 0:25
















Can you share more code from the recyclerView? are you implements multiple viewholders?

– bensadiku
Mar 28 at 0:20





Can you share more code from the recyclerView? are you implements multiple viewholders?

– bensadiku
Mar 28 at 0:20













No only one ViewHolder.

– NocTurn
Mar 28 at 0:25





No only one ViewHolder.

– NocTurn
Mar 28 at 0:25












1 Answer
1






active

oldest

votes


















1















The problem probably because you're rechecking for the itemViewType with getItemViewType(i)); inside of onCreateViewHolder. You shouldn't do that because onCreateViewHolder already giving you the itemViewType from its parameters. And you should use a switch case instead of if ? : to make your code more readable.



So, change your code to something like this:



@Override
public HorizontalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)

int layoutId;
switch(viewType)
case 0:
layoutId = R.layout.item_category_slider_viewed;
break;
case 1:
layoutId = R.layout.item_category_slider_added;
break;
case 2:
layoutId = R.layout.item_category_slider_browse_all;
break;
default:
layoutId = R.layout.item_category_slider_regular;



View cardView = LayoutInflater.from(parent.getContext()).inflate(
layoutId, parent, false);

return new HorizontalViewHolder(cardView, context);






share|improve this answer



























  • Oh my, thank you so much! I've never noticed that onCreateViewHolder actually returns the view type. I always believe it was the position of the adapter returned. Wow my mind is blown. Thanks again!

    – NocTurn
    Mar 28 at 1:33











  • Great that it helps you! And you're welcome ;)

    – ישו אוהב אותך
    Mar 28 at 2:28










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388304%2fgetitemviewtype-is-returned-only-in-onbindviewholder-but-not-in-oncreateviewhold%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















The problem probably because you're rechecking for the itemViewType with getItemViewType(i)); inside of onCreateViewHolder. You shouldn't do that because onCreateViewHolder already giving you the itemViewType from its parameters. And you should use a switch case instead of if ? : to make your code more readable.



So, change your code to something like this:



@Override
public HorizontalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)

int layoutId;
switch(viewType)
case 0:
layoutId = R.layout.item_category_slider_viewed;
break;
case 1:
layoutId = R.layout.item_category_slider_added;
break;
case 2:
layoutId = R.layout.item_category_slider_browse_all;
break;
default:
layoutId = R.layout.item_category_slider_regular;



View cardView = LayoutInflater.from(parent.getContext()).inflate(
layoutId, parent, false);

return new HorizontalViewHolder(cardView, context);






share|improve this answer



























  • Oh my, thank you so much! I've never noticed that onCreateViewHolder actually returns the view type. I always believe it was the position of the adapter returned. Wow my mind is blown. Thanks again!

    – NocTurn
    Mar 28 at 1:33











  • Great that it helps you! And you're welcome ;)

    – ישו אוהב אותך
    Mar 28 at 2:28















1















The problem probably because you're rechecking for the itemViewType with getItemViewType(i)); inside of onCreateViewHolder. You shouldn't do that because onCreateViewHolder already giving you the itemViewType from its parameters. And you should use a switch case instead of if ? : to make your code more readable.



So, change your code to something like this:



@Override
public HorizontalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)

int layoutId;
switch(viewType)
case 0:
layoutId = R.layout.item_category_slider_viewed;
break;
case 1:
layoutId = R.layout.item_category_slider_added;
break;
case 2:
layoutId = R.layout.item_category_slider_browse_all;
break;
default:
layoutId = R.layout.item_category_slider_regular;



View cardView = LayoutInflater.from(parent.getContext()).inflate(
layoutId, parent, false);

return new HorizontalViewHolder(cardView, context);






share|improve this answer



























  • Oh my, thank you so much! I've never noticed that onCreateViewHolder actually returns the view type. I always believe it was the position of the adapter returned. Wow my mind is blown. Thanks again!

    – NocTurn
    Mar 28 at 1:33











  • Great that it helps you! And you're welcome ;)

    – ישו אוהב אותך
    Mar 28 at 2:28













1














1










1









The problem probably because you're rechecking for the itemViewType with getItemViewType(i)); inside of onCreateViewHolder. You shouldn't do that because onCreateViewHolder already giving you the itemViewType from its parameters. And you should use a switch case instead of if ? : to make your code more readable.



So, change your code to something like this:



@Override
public HorizontalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)

int layoutId;
switch(viewType)
case 0:
layoutId = R.layout.item_category_slider_viewed;
break;
case 1:
layoutId = R.layout.item_category_slider_added;
break;
case 2:
layoutId = R.layout.item_category_slider_browse_all;
break;
default:
layoutId = R.layout.item_category_slider_regular;



View cardView = LayoutInflater.from(parent.getContext()).inflate(
layoutId, parent, false);

return new HorizontalViewHolder(cardView, context);






share|improve this answer















The problem probably because you're rechecking for the itemViewType with getItemViewType(i)); inside of onCreateViewHolder. You shouldn't do that because onCreateViewHolder already giving you the itemViewType from its parameters. And you should use a switch case instead of if ? : to make your code more readable.



So, change your code to something like this:



@Override
public HorizontalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)

int layoutId;
switch(viewType)
case 0:
layoutId = R.layout.item_category_slider_viewed;
break;
case 1:
layoutId = R.layout.item_category_slider_added;
break;
case 2:
layoutId = R.layout.item_category_slider_browse_all;
break;
default:
layoutId = R.layout.item_category_slider_regular;



View cardView = LayoutInflater.from(parent.getContext()).inflate(
layoutId, parent, false);

return new HorizontalViewHolder(cardView, context);







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 2:29

























answered Mar 28 at 1:24









ישו אוהב אותךישו אוהב אותך

18.2k8 gold badges44 silver badges65 bronze badges




18.2k8 gold badges44 silver badges65 bronze badges















  • Oh my, thank you so much! I've never noticed that onCreateViewHolder actually returns the view type. I always believe it was the position of the adapter returned. Wow my mind is blown. Thanks again!

    – NocTurn
    Mar 28 at 1:33











  • Great that it helps you! And you're welcome ;)

    – ישו אוהב אותך
    Mar 28 at 2:28

















  • Oh my, thank you so much! I've never noticed that onCreateViewHolder actually returns the view type. I always believe it was the position of the adapter returned. Wow my mind is blown. Thanks again!

    – NocTurn
    Mar 28 at 1:33











  • Great that it helps you! And you're welcome ;)

    – ישו אוהב אותך
    Mar 28 at 2:28
















Oh my, thank you so much! I've never noticed that onCreateViewHolder actually returns the view type. I always believe it was the position of the adapter returned. Wow my mind is blown. Thanks again!

– NocTurn
Mar 28 at 1:33





Oh my, thank you so much! I've never noticed that onCreateViewHolder actually returns the view type. I always believe it was the position of the adapter returned. Wow my mind is blown. Thanks again!

– NocTurn
Mar 28 at 1:33













Great that it helps you! And you're welcome ;)

– ישו אוהב אותך
Mar 28 at 2:28





Great that it helps you! And you're welcome ;)

– ישו אוהב אותך
Mar 28 at 2:28








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55388304%2fgetitemviewtype-is-returned-only-in-onbindviewholder-but-not-in-oncreateviewhold%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현