Android RecyclerView getChildAt() and getChildAdapterPosition()












-2















I saw a sample code and couldn't understand the meaning of the following method:



public int getAdapterPositionForIndex(RecyclerView parent, int index) {
final View child = parent.getChildAt(index);
return parent.getChildAdapterPosition(child);
}


My understanding is what's returned should always equal to index, but my debugger obviously doesn't say so. Since the docs are not explaining well the difference between getChildAt() and getChildAdapterPostion(), I hope I could get some expert insights here.










share|improve this question























  • getChildAt() give an index and return the corresponding item from adapter, getChildAdapterPostion give an item and return it's index, i hope this help you ;-)

    – Farrokh
    Nov 20 '18 at 4:59











  • Hi, @Farrokh Yes, I understand getChildAt() is returning the view and getChildAdapterPosition() is returning an index. Let me rephrase my question: how come the return value of the getAdapterPositionForIndex method does not always equal to index?

    – Alison Z
    Nov 29 '18 at 19:45
















-2















I saw a sample code and couldn't understand the meaning of the following method:



public int getAdapterPositionForIndex(RecyclerView parent, int index) {
final View child = parent.getChildAt(index);
return parent.getChildAdapterPosition(child);
}


My understanding is what's returned should always equal to index, but my debugger obviously doesn't say so. Since the docs are not explaining well the difference between getChildAt() and getChildAdapterPostion(), I hope I could get some expert insights here.










share|improve this question























  • getChildAt() give an index and return the corresponding item from adapter, getChildAdapterPostion give an item and return it's index, i hope this help you ;-)

    – Farrokh
    Nov 20 '18 at 4:59











  • Hi, @Farrokh Yes, I understand getChildAt() is returning the view and getChildAdapterPosition() is returning an index. Let me rephrase my question: how come the return value of the getAdapterPositionForIndex method does not always equal to index?

    – Alison Z
    Nov 29 '18 at 19:45














-2












-2








-2








I saw a sample code and couldn't understand the meaning of the following method:



public int getAdapterPositionForIndex(RecyclerView parent, int index) {
final View child = parent.getChildAt(index);
return parent.getChildAdapterPosition(child);
}


My understanding is what's returned should always equal to index, but my debugger obviously doesn't say so. Since the docs are not explaining well the difference between getChildAt() and getChildAdapterPostion(), I hope I could get some expert insights here.










share|improve this question














I saw a sample code and couldn't understand the meaning of the following method:



public int getAdapterPositionForIndex(RecyclerView parent, int index) {
final View child = parent.getChildAt(index);
return parent.getChildAdapterPosition(child);
}


My understanding is what's returned should always equal to index, but my debugger obviously doesn't say so. Since the docs are not explaining well the difference between getChildAt() and getChildAdapterPostion(), I hope I could get some expert insights here.







android android-recyclerview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 4:35









Alison ZAlison Z

207




207













  • getChildAt() give an index and return the corresponding item from adapter, getChildAdapterPostion give an item and return it's index, i hope this help you ;-)

    – Farrokh
    Nov 20 '18 at 4:59











  • Hi, @Farrokh Yes, I understand getChildAt() is returning the view and getChildAdapterPosition() is returning an index. Let me rephrase my question: how come the return value of the getAdapterPositionForIndex method does not always equal to index?

    – Alison Z
    Nov 29 '18 at 19:45



















  • getChildAt() give an index and return the corresponding item from adapter, getChildAdapterPostion give an item and return it's index, i hope this help you ;-)

    – Farrokh
    Nov 20 '18 at 4:59











  • Hi, @Farrokh Yes, I understand getChildAt() is returning the view and getChildAdapterPosition() is returning an index. Let me rephrase my question: how come the return value of the getAdapterPositionForIndex method does not always equal to index?

    – Alison Z
    Nov 29 '18 at 19:45

















getChildAt() give an index and return the corresponding item from adapter, getChildAdapterPostion give an item and return it's index, i hope this help you ;-)

– Farrokh
Nov 20 '18 at 4:59





getChildAt() give an index and return the corresponding item from adapter, getChildAdapterPostion give an item and return it's index, i hope this help you ;-)

– Farrokh
Nov 20 '18 at 4:59













Hi, @Farrokh Yes, I understand getChildAt() is returning the view and getChildAdapterPosition() is returning an index. Let me rephrase my question: how come the return value of the getAdapterPositionForIndex method does not always equal to index?

– Alison Z
Nov 29 '18 at 19:45





Hi, @Farrokh Yes, I understand getChildAt() is returning the view and getChildAdapterPosition() is returning an index. Let me rephrase my question: how come the return value of the getAdapterPositionForIndex method does not always equal to index?

– Alison Z
Nov 29 '18 at 19:45












2 Answers
2






active

oldest

votes


















1














Well as per my understanding getChildAt() is a method of ViewGroup . And it does Returns the view at the specified position in the group.



Since RecyclerView is an AdapterView i.e items get recycle when goes out of boundary it returns null for #getChildAt().



I am not sure whats the exact reason may be some should explain this



On other hand #getChildAdapterPosition() Return the adapter position that the given child view added to.
Look at the code below :(Only adding the essential)



findViewById(R.id.b1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager layoutManager= (LinearLayoutManager) rvNumber.getLayoutManager();
final View child = layoutManager.findViewByPosition(30);
if(child!=null) {
int i = rvNumber.getChildAdapterPosition(child);
Log.i("pos", i + "");
}else{
Log.i("pos", "View is null");
}
}
});


Nothing complex here! I laid down 60 items in RecyclerView just a TextView. In which 10 items are showing at a time in list . So the first time 10 views will be laid down (0-9).



When i call the above code on clicking on button it gives me a null view . Cause Views is not inflated yet for position 30. But after scrolling to position 30 it returns the view and hence its position by getChildAdapterPosition() which will be 30 also .



I think you should make a sample and play around with it for better understanding.






share|improve this answer































    0














    FROM DOCS



    getChildAdapterPosition()




    • Return the adapter position that the given child view corresponds to.


    getChildAt()




    • Returns the view at the specified position in the group.



    The Difference




    It Means the getChildAdapterPosition() method return the the position of View inside recyclerview adapter



    AND



    the getChildAt() method returns the View from a viewGroup of specific position



    In short
    The Both method are different getChildAt() is retuning view from a viewGroup while the other getChildAdapterPosition() retuning the postion of a view in recyclerview adapter






    share|improve this answer


























    • @Downvoters pls. explain exactly what's wrong with this answer so that I can address?

      – Nilesh Rathod
      Nov 20 '18 at 4: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%2f53386306%2fandroid-recyclerview-getchildat-and-getchildadapterposition%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Well as per my understanding getChildAt() is a method of ViewGroup . And it does Returns the view at the specified position in the group.



    Since RecyclerView is an AdapterView i.e items get recycle when goes out of boundary it returns null for #getChildAt().



    I am not sure whats the exact reason may be some should explain this



    On other hand #getChildAdapterPosition() Return the adapter position that the given child view added to.
    Look at the code below :(Only adding the essential)



    findViewById(R.id.b1).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    LinearLayoutManager layoutManager= (LinearLayoutManager) rvNumber.getLayoutManager();
    final View child = layoutManager.findViewByPosition(30);
    if(child!=null) {
    int i = rvNumber.getChildAdapterPosition(child);
    Log.i("pos", i + "");
    }else{
    Log.i("pos", "View is null");
    }
    }
    });


    Nothing complex here! I laid down 60 items in RecyclerView just a TextView. In which 10 items are showing at a time in list . So the first time 10 views will be laid down (0-9).



    When i call the above code on clicking on button it gives me a null view . Cause Views is not inflated yet for position 30. But after scrolling to position 30 it returns the view and hence its position by getChildAdapterPosition() which will be 30 also .



    I think you should make a sample and play around with it for better understanding.






    share|improve this answer




























      1














      Well as per my understanding getChildAt() is a method of ViewGroup . And it does Returns the view at the specified position in the group.



      Since RecyclerView is an AdapterView i.e items get recycle when goes out of boundary it returns null for #getChildAt().



      I am not sure whats the exact reason may be some should explain this



      On other hand #getChildAdapterPosition() Return the adapter position that the given child view added to.
      Look at the code below :(Only adding the essential)



      findViewById(R.id.b1).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      LinearLayoutManager layoutManager= (LinearLayoutManager) rvNumber.getLayoutManager();
      final View child = layoutManager.findViewByPosition(30);
      if(child!=null) {
      int i = rvNumber.getChildAdapterPosition(child);
      Log.i("pos", i + "");
      }else{
      Log.i("pos", "View is null");
      }
      }
      });


      Nothing complex here! I laid down 60 items in RecyclerView just a TextView. In which 10 items are showing at a time in list . So the first time 10 views will be laid down (0-9).



      When i call the above code on clicking on button it gives me a null view . Cause Views is not inflated yet for position 30. But after scrolling to position 30 it returns the view and hence its position by getChildAdapterPosition() which will be 30 also .



      I think you should make a sample and play around with it for better understanding.






      share|improve this answer


























        1












        1








        1







        Well as per my understanding getChildAt() is a method of ViewGroup . And it does Returns the view at the specified position in the group.



        Since RecyclerView is an AdapterView i.e items get recycle when goes out of boundary it returns null for #getChildAt().



        I am not sure whats the exact reason may be some should explain this



        On other hand #getChildAdapterPosition() Return the adapter position that the given child view added to.
        Look at the code below :(Only adding the essential)



        findViewById(R.id.b1).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        LinearLayoutManager layoutManager= (LinearLayoutManager) rvNumber.getLayoutManager();
        final View child = layoutManager.findViewByPosition(30);
        if(child!=null) {
        int i = rvNumber.getChildAdapterPosition(child);
        Log.i("pos", i + "");
        }else{
        Log.i("pos", "View is null");
        }
        }
        });


        Nothing complex here! I laid down 60 items in RecyclerView just a TextView. In which 10 items are showing at a time in list . So the first time 10 views will be laid down (0-9).



        When i call the above code on clicking on button it gives me a null view . Cause Views is not inflated yet for position 30. But after scrolling to position 30 it returns the view and hence its position by getChildAdapterPosition() which will be 30 also .



        I think you should make a sample and play around with it for better understanding.






        share|improve this answer













        Well as per my understanding getChildAt() is a method of ViewGroup . And it does Returns the view at the specified position in the group.



        Since RecyclerView is an AdapterView i.e items get recycle when goes out of boundary it returns null for #getChildAt().



        I am not sure whats the exact reason may be some should explain this



        On other hand #getChildAdapterPosition() Return the adapter position that the given child view added to.
        Look at the code below :(Only adding the essential)



        findViewById(R.id.b1).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        LinearLayoutManager layoutManager= (LinearLayoutManager) rvNumber.getLayoutManager();
        final View child = layoutManager.findViewByPosition(30);
        if(child!=null) {
        int i = rvNumber.getChildAdapterPosition(child);
        Log.i("pos", i + "");
        }else{
        Log.i("pos", "View is null");
        }
        }
        });


        Nothing complex here! I laid down 60 items in RecyclerView just a TextView. In which 10 items are showing at a time in list . So the first time 10 views will be laid down (0-9).



        When i call the above code on clicking on button it gives me a null view . Cause Views is not inflated yet for position 30. But after scrolling to position 30 it returns the view and hence its position by getChildAdapterPosition() which will be 30 also .



        I think you should make a sample and play around with it for better understanding.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 5:28









        ADMADM

        8,642102351




        8,642102351

























            0














            FROM DOCS



            getChildAdapterPosition()




            • Return the adapter position that the given child view corresponds to.


            getChildAt()




            • Returns the view at the specified position in the group.



            The Difference




            It Means the getChildAdapterPosition() method return the the position of View inside recyclerview adapter



            AND



            the getChildAt() method returns the View from a viewGroup of specific position



            In short
            The Both method are different getChildAt() is retuning view from a viewGroup while the other getChildAdapterPosition() retuning the postion of a view in recyclerview adapter






            share|improve this answer


























            • @Downvoters pls. explain exactly what's wrong with this answer so that I can address?

              – Nilesh Rathod
              Nov 20 '18 at 4:47
















            0














            FROM DOCS



            getChildAdapterPosition()




            • Return the adapter position that the given child view corresponds to.


            getChildAt()




            • Returns the view at the specified position in the group.



            The Difference




            It Means the getChildAdapterPosition() method return the the position of View inside recyclerview adapter



            AND



            the getChildAt() method returns the View from a viewGroup of specific position



            In short
            The Both method are different getChildAt() is retuning view from a viewGroup while the other getChildAdapterPosition() retuning the postion of a view in recyclerview adapter






            share|improve this answer


























            • @Downvoters pls. explain exactly what's wrong with this answer so that I can address?

              – Nilesh Rathod
              Nov 20 '18 at 4:47














            0












            0








            0







            FROM DOCS



            getChildAdapterPosition()




            • Return the adapter position that the given child view corresponds to.


            getChildAt()




            • Returns the view at the specified position in the group.



            The Difference




            It Means the getChildAdapterPosition() method return the the position of View inside recyclerview adapter



            AND



            the getChildAt() method returns the View from a viewGroup of specific position



            In short
            The Both method are different getChildAt() is retuning view from a viewGroup while the other getChildAdapterPosition() retuning the postion of a view in recyclerview adapter






            share|improve this answer















            FROM DOCS



            getChildAdapterPosition()




            • Return the adapter position that the given child view corresponds to.


            getChildAt()




            • Returns the view at the specified position in the group.



            The Difference




            It Means the getChildAdapterPosition() method return the the position of View inside recyclerview adapter



            AND



            the getChildAt() method returns the View from a viewGroup of specific position



            In short
            The Both method are different getChildAt() is retuning view from a viewGroup while the other getChildAdapterPosition() retuning the postion of a view in recyclerview adapter







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 4:46

























            answered Nov 20 '18 at 4:39









            Nilesh RathodNilesh Rathod

            31.9k82956




            31.9k82956













            • @Downvoters pls. explain exactly what's wrong with this answer so that I can address?

              – Nilesh Rathod
              Nov 20 '18 at 4:47



















            • @Downvoters pls. explain exactly what's wrong with this answer so that I can address?

              – Nilesh Rathod
              Nov 20 '18 at 4:47

















            @Downvoters pls. explain exactly what's wrong with this answer so that I can address?

            – Nilesh Rathod
            Nov 20 '18 at 4:47





            @Downvoters pls. explain exactly what's wrong with this answer so that I can address?

            – Nilesh Rathod
            Nov 20 '18 at 4: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%2f53386306%2fandroid-recyclerview-getchildat-and-getchildadapterposition%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

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?