0

I have a rank of players in a viewgroup displayed programmatically. A player has a class and a method to show it inside the viewgroup:

public void show(Activity a, ViewGroup container)
{
    LayoutInflater inflater = (LayoutInflater) a.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    int res = position == 1 ? R.layout.ins_rank_first : R.layout.ins_rank;
    View view = inflater.inflate(res, container, false);
    CircleImageView imgUser = view.findViewById(R.id.img_rank_user);
    TextView txtUser = view.findViewById(R.id.txt_rank_user);
    TextView txtPosition = view.findViewById(R.id.txt_rank_position);
    TextView txtScore = view.findViewById(R.id.txt_rank_score);

    //load image inside imageview
    a.loadImage(userImage, imgUser, R.drawable.ic_profile);
    if(position == 2)
        imgUser.setBorderColor(a.getResources().getColor(R.color.silver));
    else if(position == 3)
        imgUser.setBorderColor(a.getResources().getColor(R.color.bronze));
    txtUser.setText(username);
    txtPosition.setText(String.valueOf(position));
    txtScore.setText(String.valueOf(score));

    container.addView(view);
}

So I want to ask the server every 5 seconds to generate a new ranking to make it real-time, but I don't want to remove all ViewGroup's views because the list could be long. Therefor I would like to compare new ranking positions to older ones and then exchange views when needed. How could I achieve this?

Twenty
  • 5,234
  • 4
  • 32
  • 67
geggiamarti
  • 129
  • 1
  • 12
  • Sounds like you are describing the functionality of a RecyclerView and Adapter https://developer.android.com/guide/topics/ui/layout/recyclerview – Blundell Feb 10 '20 at 13:42
  • @Blundell I have heard about it, can you make me a simple code example? I really hate android docs :( – geggiamarti Feb 10 '20 at 14:23
  • If you aren't for the docs :-) I'd recommend any one of these blogs: https://proandroiddev.com/tagged/recyclerview – Blundell Feb 10 '20 at 15:33
  • Forgive the assumption but this could be a good start: https://proandroiddev.com/android-developer-beginner-step-2-lists-55c32aafce70 – Blundell Feb 10 '20 at 15:34
  • @Blundell thank you :) I will give a look, in the meantime I found a solution without move anything, only by substitute contents of view... but it's too long to explain and I don't think it will be helpfull to someone :) I have never used RecyclerView (I'm not a fan of Material) but I'm not an Android beginner, only a stupid Italian woman :) Thank you again – geggiamarti Feb 10 '20 at 17:48
  • Awesome have fun! (If I was going to do what you asked without RecyclerView, I would store an id in the getTag method of the View, and use that to compare if its the same view next time ;-) ) – Blundell Feb 10 '20 at 22:19

0 Answers0