Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // code:
- RecyclerView rv = new RecyclerView(this);
- setContentView(rv);
- rv.setLayoutManager(new LinearLayoutManager(this));
- final ScoreModel[] list = {
- new ScoreModel("one"), new ScoreModel("two"), new ScoreModel("three"),
- new ScoreModel("4"), new ScoreModel("5"), new ScoreModel("6"),
- new ScoreModel("7"), new ScoreModel("8"), new ScoreModel("9"),
- new ScoreModel("10"), new ScoreModel("11"), new ScoreModel("12"),
- };
- ScoreAdapter adapter = new ScoreAdapter(this, list);
- rv.setAdapter(adapter);
- final Handler h = new Handler();
- Runnable r = new Runnable() {
- int cnt = 0;
- @Override
- public void run() {
- if (cnt++ < 10) {
- list[2].setScore("three #" + cnt);
- h.postDelayed(this, 3000);
- }
- }
- };
- h.postDelayed(r, 3000);
- // adapter
- class ScoreAdapter extends RecyclerView.Adapter<ScoreAdapter.VH> {
- private static final String TAG = "Adapter";
- private final LayoutInflater inflater;
- private final ScoreModel[] list;
- public ScoreAdapter(Context ctx, ScoreModel[] list) {
- inflater = LayoutInflater.from(ctx);
- this.list = list;
- }
- @Override
- public VH onCreateViewHolder(ViewGroup parent, int viewType) {
- return new VH(ScoreBinding.inflate(inflater, parent, false));
- }
- @Override
- public void onBindViewHolder(VH holder, int position) {
- holder.bindData(list[position]);
- }
- @Override
- public int getItemCount() {
- return list.length;
- }
- public class VH extends RecyclerView.ViewHolder {
- private final ScoreBinding binding;
- public VH(ScoreBinding binding_) {
- super(binding_.getRoot());
- binding = binding_;
- }
- public void bindData(ScoreModel model) {
- binding.setModel(model);
- binding.executePendingBindings();
- }
- }
- }
- // score.xml
- <?xml version="1.0" encoding="utf-8"?>
- <layout xmlns:android="http://schemas.android.com/apk/res/android">
- <data>
- <variable name="model" type="your.package.ScoreModel"/>
- </data>
- <TextView android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:textSize="48dp"
- android:text="@{model.score}"
- />
- </layout>
- // model
- public class ScoreModel extends BaseObservable {
- private static final String TAG = "ScoreModel";
- String score;
- public ScoreModel(String s) {
- score = s;
- }
- @Bindable
- public String getScore() {
- return score;
- }
- public void setScore(String s) {
- score = s;
- Log.d(TAG, "setScore " + score);
- notifyPropertyChanged(BR.score);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment