Guest User

Untitled

a guest
Jun 22nd, 2017
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. // code:
  2.  
  3. RecyclerView rv = new RecyclerView(this);
  4. setContentView(rv);
  5. rv.setLayoutManager(new LinearLayoutManager(this));
  6. final ScoreModel[] list = {
  7. new ScoreModel("one"), new ScoreModel("two"), new ScoreModel("three"),
  8. new ScoreModel("4"), new ScoreModel("5"), new ScoreModel("6"),
  9. new ScoreModel("7"), new ScoreModel("8"), new ScoreModel("9"),
  10. new ScoreModel("10"), new ScoreModel("11"), new ScoreModel("12"),
  11. };
  12. ScoreAdapter adapter = new ScoreAdapter(this, list);
  13. rv.setAdapter(adapter);
  14. final Handler h = new Handler();
  15. Runnable r = new Runnable() {
  16. int cnt = 0;
  17. @Override
  18. public void run() {
  19. if (cnt++ < 10) {
  20. list[2].setScore("three #" + cnt);
  21. h.postDelayed(this, 3000);
  22. }
  23. }
  24. };
  25. h.postDelayed(r, 3000);
  26.  
  27. // adapter
  28. class ScoreAdapter extends RecyclerView.Adapter<ScoreAdapter.VH> {
  29. private static final String TAG = "Adapter";
  30.  
  31. private final LayoutInflater inflater;
  32. private final ScoreModel[] list;
  33.  
  34. public ScoreAdapter(Context ctx, ScoreModel[] list) {
  35. inflater = LayoutInflater.from(ctx);
  36. this.list = list;
  37. }
  38.  
  39. @Override
  40. public VH onCreateViewHolder(ViewGroup parent, int viewType) {
  41. return new VH(ScoreBinding.inflate(inflater, parent, false));
  42. }
  43.  
  44. @Override
  45. public void onBindViewHolder(VH holder, int position) {
  46. holder.bindData(list[position]);
  47. }
  48.  
  49. @Override
  50. public int getItemCount() {
  51. return list.length;
  52. }
  53.  
  54. public class VH extends RecyclerView.ViewHolder {
  55. private final ScoreBinding binding;
  56.  
  57. public VH(ScoreBinding binding_) {
  58. super(binding_.getRoot());
  59. binding = binding_;
  60. }
  61.  
  62. public void bindData(ScoreModel model) {
  63. binding.setModel(model);
  64. binding.executePendingBindings();
  65. }
  66. }
  67. }
  68.  
  69. // score.xml
  70.  
  71. <?xml version="1.0" encoding="utf-8"?>
  72. <layout xmlns:android="http://schemas.android.com/apk/res/android">
  73. <data>
  74. <variable name="model" type="your.package.ScoreModel"/>
  75. </data>
  76.  
  77. <TextView android:layout_width="match_parent"
  78. android:layout_height="wrap_content"
  79. android:textAppearance="?android:attr/textAppearanceLarge"
  80. android:textSize="48dp"
  81. android:text="@{model.score}"
  82. />
  83. </layout>
  84.  
  85. // model
  86.  
  87. public class ScoreModel extends BaseObservable {
  88. private static final String TAG = "ScoreModel";
  89. String score;
  90.  
  91. public ScoreModel(String s) {
  92. score = s;
  93. }
  94.  
  95. @Bindable
  96. public String getScore() {
  97. return score;
  98. }
  99.  
  100. public void setScore(String s) {
  101. score = s;
  102. Log.d(TAG, "setScore " + score);
  103. notifyPropertyChanged(BR.score);
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment