Recyleview sometimes add new item to the topHow to add dividers and spaces between items in RecyclerView?How to Change the viewType of a RecyclerView item onClickChange Recyclerview row design when swiching between LayoutManger types (Grid - Linear)How to inform recyclerview adapter of new item inserted into dataAdding Items in RecycleView Dynamically using a buttonRecycle View scrolls slow . How to fix it?Adapter won't updatehow to add new view to recycleview dynamically?when one card is deleted from RecyclerView one more card disappears along with itRecycleview items value add

Is aluminum electrical wire used on aircraft?

Can I sign legal documents with a smiley face?

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?

Creepy dinosaur pc game identification

Multiplicative persistence

Not using 's' for he/she/it

Problem with TransformedDistribution

Fear of getting stuck on one programming language / technology that is not used in my country

New brakes for 90s road bike

Pre-modern battle - command it, or fight in it?

How could a planet have erratic days?

Is it possible to have a strip of cold climate in the middle of a planet?

Why did the EU agree to delay the Brexit deadline?

Did arcade monitors have same pixel aspect ratio as TV sets?

On a tidally locked planet, would time be quantized?

It grows, but water kills it

If a character has darkvision, can they see through an area of nonmagical darkness filled with lightly obscuring gas?

Symbol used to indicate indivisibility

How much character growth crosses the line into breaking the character

Are the IPv6 address space and IPv4 address space completely disjoint?

What is this called? Old film camera viewer?

How did Rebekah know that Esau was planning to kill his brother in Genesis 27:42?

The screen of my macbook suddenly broken down how can I do to recover

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?



Recyleview sometimes add new item to the top


How to add dividers and spaces between items in RecyclerView?How to Change the viewType of a RecyclerView item onClickChange Recyclerview row design when swiching between LayoutManger types (Grid - Linear)How to inform recyclerview adapter of new item inserted into dataAdding Items in RecycleView Dynamically using a buttonRecycle View scrolls slow . How to fix it?Adapter won't updatehow to add new view to recycleview dynamically?when one card is deleted from RecyclerView one more card disappears along with itRecycleview items value add













0















I'm adding a new layout to RecycleView on click of a plus button of the previous layout. At first, I'm adding empty data to model class and setting the adapter with one item. After that click of the button, I need to add the second item and so on.



Problem is . at first it works and recyclview with one item is display. on click of button second layout is also added below first. but a third time if I click on that button. it is adding a new item to the top of Recycleview. I need to generate a vertical hierarchy.



Adding First Data on onCreate



var arrayList:ArrayList<SubjectModel> = ArrayList()

var model = SubjectModel()
arrayList.add(model)

var adapter = SubjectListAdapter(arrayList)

var layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

rv_subject.layoutManager = layoutManager
rv_subject.adapter = adapter


And inside the adapter. I have a plus button which is adding new item vertically.



 holder.img_delete_subject.setTag(position);
holder.img_delete_subject.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)


if (if it is plus )

holder.img_delete_subject.setImageResource(minus image);

data.add(data.size(),new SubjectModel());
notifyItemInserted((data.size() - 1));

else

data.remove(Integer.parseInt(view.getTag().toString()));
notifyItemRemoved(Integer.parseInt(view.getTag().toString()));





);









share|improve this question
























  • How do you know which one is newly added? It is just a new SubjectModel() and are same to all rows.

    – AIMIN PAN
    Mar 7 at 8:00











  • @AIMINPAN i'm using edittext watcher and setting data at that position with onTextchangedListner

    – Jay333
    Mar 7 at 9:37















0















I'm adding a new layout to RecycleView on click of a plus button of the previous layout. At first, I'm adding empty data to model class and setting the adapter with one item. After that click of the button, I need to add the second item and so on.



Problem is . at first it works and recyclview with one item is display. on click of button second layout is also added below first. but a third time if I click on that button. it is adding a new item to the top of Recycleview. I need to generate a vertical hierarchy.



Adding First Data on onCreate



var arrayList:ArrayList<SubjectModel> = ArrayList()

var model = SubjectModel()
arrayList.add(model)

var adapter = SubjectListAdapter(arrayList)

var layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

rv_subject.layoutManager = layoutManager
rv_subject.adapter = adapter


And inside the adapter. I have a plus button which is adding new item vertically.



 holder.img_delete_subject.setTag(position);
holder.img_delete_subject.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)


if (if it is plus )

holder.img_delete_subject.setImageResource(minus image);

data.add(data.size(),new SubjectModel());
notifyItemInserted((data.size() - 1));

else

data.remove(Integer.parseInt(view.getTag().toString()));
notifyItemRemoved(Integer.parseInt(view.getTag().toString()));





);









share|improve this question
























  • How do you know which one is newly added? It is just a new SubjectModel() and are same to all rows.

    – AIMIN PAN
    Mar 7 at 8:00











  • @AIMINPAN i'm using edittext watcher and setting data at that position with onTextchangedListner

    – Jay333
    Mar 7 at 9:37













0












0








0








I'm adding a new layout to RecycleView on click of a plus button of the previous layout. At first, I'm adding empty data to model class and setting the adapter with one item. After that click of the button, I need to add the second item and so on.



Problem is . at first it works and recyclview with one item is display. on click of button second layout is also added below first. but a third time if I click on that button. it is adding a new item to the top of Recycleview. I need to generate a vertical hierarchy.



Adding First Data on onCreate



var arrayList:ArrayList<SubjectModel> = ArrayList()

var model = SubjectModel()
arrayList.add(model)

var adapter = SubjectListAdapter(arrayList)

var layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

rv_subject.layoutManager = layoutManager
rv_subject.adapter = adapter


And inside the adapter. I have a plus button which is adding new item vertically.



 holder.img_delete_subject.setTag(position);
holder.img_delete_subject.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)


if (if it is plus )

holder.img_delete_subject.setImageResource(minus image);

data.add(data.size(),new SubjectModel());
notifyItemInserted((data.size() - 1));

else

data.remove(Integer.parseInt(view.getTag().toString()));
notifyItemRemoved(Integer.parseInt(view.getTag().toString()));





);









share|improve this question
















I'm adding a new layout to RecycleView on click of a plus button of the previous layout. At first, I'm adding empty data to model class and setting the adapter with one item. After that click of the button, I need to add the second item and so on.



Problem is . at first it works and recyclview with one item is display. on click of button second layout is also added below first. but a third time if I click on that button. it is adding a new item to the top of Recycleview. I need to generate a vertical hierarchy.



Adding First Data on onCreate



var arrayList:ArrayList<SubjectModel> = ArrayList()

var model = SubjectModel()
arrayList.add(model)

var adapter = SubjectListAdapter(arrayList)

var layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

rv_subject.layoutManager = layoutManager
rv_subject.adapter = adapter


And inside the adapter. I have a plus button which is adding new item vertically.



 holder.img_delete_subject.setTag(position);
holder.img_delete_subject.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)


if (if it is plus )

holder.img_delete_subject.setImageResource(minus image);

data.add(data.size(),new SubjectModel());
notifyItemInserted((data.size() - 1));

else

data.remove(Integer.parseInt(view.getTag().toString()));
notifyItemRemoved(Integer.parseInt(view.getTag().toString()));





);






android kotlin android-recyclerview recycler-adapter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 12:20







Jay333

















asked Mar 7 at 7:35









Jay333Jay333

5810




5810












  • How do you know which one is newly added? It is just a new SubjectModel() and are same to all rows.

    – AIMIN PAN
    Mar 7 at 8:00











  • @AIMINPAN i'm using edittext watcher and setting data at that position with onTextchangedListner

    – Jay333
    Mar 7 at 9:37

















  • How do you know which one is newly added? It is just a new SubjectModel() and are same to all rows.

    – AIMIN PAN
    Mar 7 at 8:00











  • @AIMINPAN i'm using edittext watcher and setting data at that position with onTextchangedListner

    – Jay333
    Mar 7 at 9:37
















How do you know which one is newly added? It is just a new SubjectModel() and are same to all rows.

– AIMIN PAN
Mar 7 at 8:00





How do you know which one is newly added? It is just a new SubjectModel() and are same to all rows.

– AIMIN PAN
Mar 7 at 8:00













@AIMINPAN i'm using edittext watcher and setting data at that position with onTextchangedListner

– Jay333
Mar 7 at 9:37





@AIMINPAN i'm using edittext watcher and setting data at that position with onTextchangedListner

– Jay333
Mar 7 at 9:37












1 Answer
1






active

oldest

votes


















1














The problem is with notifiyDataSetChanged(). As documentation says:



 * <p>This event does not specify what about the data set has changed, forcing
* any observers to assume that all existing items and structure may no longer be valid.
* LayoutManagers will be forced to fully rebind and relayout all visible views.</p>


When you add single item to the list, you should be using



notifyItemInsterted(position: Int)


 /**
* Notify any registered observers that the item reflected at <code>position</code>
* has been newly inserted. The item previously at <code>position</code> is now at
* position <code>position + 1</code>.
*
* <p>This is a structural change event. Representations of other existing items in the
* data set are still considered up to date and will not be rebound, though their
* positions may be altered.</p>
*
* @param position Position of the newly inserted item in the data set





share|improve this answer























  • I'll try and let you know

    – Jay333
    Mar 7 at 9:37











  • I've added notifyItemInsterted and its working now . one last question . I've updated my adapter code. when i remove data from my data and click on plus .it adding new item with old data again .(I've made sure it removed from the list properly). would be very helpful if you can help . nevertheless i'll accept this answer.

    – Jay333
    Mar 7 at 12:23











  • I'll add other info if you need.

    – Jay333
    Mar 7 at 12:47










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%2f55038376%2frecyleview-sometimes-add-new-item-to-the-top%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 is with notifiyDataSetChanged(). As documentation says:



 * <p>This event does not specify what about the data set has changed, forcing
* any observers to assume that all existing items and structure may no longer be valid.
* LayoutManagers will be forced to fully rebind and relayout all visible views.</p>


When you add single item to the list, you should be using



notifyItemInsterted(position: Int)


 /**
* Notify any registered observers that the item reflected at <code>position</code>
* has been newly inserted. The item previously at <code>position</code> is now at
* position <code>position + 1</code>.
*
* <p>This is a structural change event. Representations of other existing items in the
* data set are still considered up to date and will not be rebound, though their
* positions may be altered.</p>
*
* @param position Position of the newly inserted item in the data set





share|improve this answer























  • I'll try and let you know

    – Jay333
    Mar 7 at 9:37











  • I've added notifyItemInsterted and its working now . one last question . I've updated my adapter code. when i remove data from my data and click on plus .it adding new item with old data again .(I've made sure it removed from the list properly). would be very helpful if you can help . nevertheless i'll accept this answer.

    – Jay333
    Mar 7 at 12:23











  • I'll add other info if you need.

    – Jay333
    Mar 7 at 12:47















1














The problem is with notifiyDataSetChanged(). As documentation says:



 * <p>This event does not specify what about the data set has changed, forcing
* any observers to assume that all existing items and structure may no longer be valid.
* LayoutManagers will be forced to fully rebind and relayout all visible views.</p>


When you add single item to the list, you should be using



notifyItemInsterted(position: Int)


 /**
* Notify any registered observers that the item reflected at <code>position</code>
* has been newly inserted. The item previously at <code>position</code> is now at
* position <code>position + 1</code>.
*
* <p>This is a structural change event. Representations of other existing items in the
* data set are still considered up to date and will not be rebound, though their
* positions may be altered.</p>
*
* @param position Position of the newly inserted item in the data set





share|improve this answer























  • I'll try and let you know

    – Jay333
    Mar 7 at 9:37











  • I've added notifyItemInsterted and its working now . one last question . I've updated my adapter code. when i remove data from my data and click on plus .it adding new item with old data again .(I've made sure it removed from the list properly). would be very helpful if you can help . nevertheless i'll accept this answer.

    – Jay333
    Mar 7 at 12:23











  • I'll add other info if you need.

    – Jay333
    Mar 7 at 12:47













1












1








1







The problem is with notifiyDataSetChanged(). As documentation says:



 * <p>This event does not specify what about the data set has changed, forcing
* any observers to assume that all existing items and structure may no longer be valid.
* LayoutManagers will be forced to fully rebind and relayout all visible views.</p>


When you add single item to the list, you should be using



notifyItemInsterted(position: Int)


 /**
* Notify any registered observers that the item reflected at <code>position</code>
* has been newly inserted. The item previously at <code>position</code> is now at
* position <code>position + 1</code>.
*
* <p>This is a structural change event. Representations of other existing items in the
* data set are still considered up to date and will not be rebound, though their
* positions may be altered.</p>
*
* @param position Position of the newly inserted item in the data set





share|improve this answer













The problem is with notifiyDataSetChanged(). As documentation says:



 * <p>This event does not specify what about the data set has changed, forcing
* any observers to assume that all existing items and structure may no longer be valid.
* LayoutManagers will be forced to fully rebind and relayout all visible views.</p>


When you add single item to the list, you should be using



notifyItemInsterted(position: Int)


 /**
* Notify any registered observers that the item reflected at <code>position</code>
* has been newly inserted. The item previously at <code>position</code> is now at
* position <code>position + 1</code>.
*
* <p>This is a structural change event. Representations of other existing items in the
* data set are still considered up to date and will not be rebound, though their
* positions may be altered.</p>
*
* @param position Position of the newly inserted item in the data set






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 7:49









Kuba PawłowskiKuba Pawłowski

1106




1106












  • I'll try and let you know

    – Jay333
    Mar 7 at 9:37











  • I've added notifyItemInsterted and its working now . one last question . I've updated my adapter code. when i remove data from my data and click on plus .it adding new item with old data again .(I've made sure it removed from the list properly). would be very helpful if you can help . nevertheless i'll accept this answer.

    – Jay333
    Mar 7 at 12:23











  • I'll add other info if you need.

    – Jay333
    Mar 7 at 12:47

















  • I'll try and let you know

    – Jay333
    Mar 7 at 9:37











  • I've added notifyItemInsterted and its working now . one last question . I've updated my adapter code. when i remove data from my data and click on plus .it adding new item with old data again .(I've made sure it removed from the list properly). would be very helpful if you can help . nevertheless i'll accept this answer.

    – Jay333
    Mar 7 at 12:23











  • I'll add other info if you need.

    – Jay333
    Mar 7 at 12:47
















I'll try and let you know

– Jay333
Mar 7 at 9:37





I'll try and let you know

– Jay333
Mar 7 at 9:37













I've added notifyItemInsterted and its working now . one last question . I've updated my adapter code. when i remove data from my data and click on plus .it adding new item with old data again .(I've made sure it removed from the list properly). would be very helpful if you can help . nevertheless i'll accept this answer.

– Jay333
Mar 7 at 12:23





I've added notifyItemInsterted and its working now . one last question . I've updated my adapter code. when i remove data from my data and click on plus .it adding new item with old data again .(I've made sure it removed from the list properly). would be very helpful if you can help . nevertheless i'll accept this answer.

– Jay333
Mar 7 at 12:23













I'll add other info if you need.

– Jay333
Mar 7 at 12:47





I'll add other info if you need.

– Jay333
Mar 7 at 12:47



















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%2f55038376%2frecyleview-sometimes-add-new-item-to-the-top%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

AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

Алба-Юлія

Захаров Федір Захарович