跑马灯;RxJava和DataBinding;

1.跑马灯效果的Textview


重点是true这行改成1的话就不行了。

2.RxJava与DataBinding共用的一个问题

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

    mUser = new User("Test", "User");
    binding.setUser(mUser);

    mUser.setFirstName("修改后1");

    mService = getRetrofit().create(PullService.class);
    MyParams params = new MyParams();
    params.put("roomnumber", "123");
    mService.getRoomInfo(params.get())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    new Action1() {
                        @Override
                        public void call(Bean s) {
                            mUser.setFirstName("修改后2");
                        }
                    });
}

这样写的话,mUser这个变量数据虽然已经写进去了,但是绑定的view不会更新的。暂时找不到很好的办法,只能

new Action1() {
     @Override
     public void call(Bean s) {
         mUser.setFirstName("修改后2");
         binding.textView.setText(s.getMsg());
     }
});

这样手动去设置一下了。
Edit:知道原因了,是需要ViewModel继承BaseObservable的,如下:

public class User extends BaseObservable{
    private String firstName;
    private String lastName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;
        notifyPropertyChanged(BR.firstName);
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
        notifyPropertyChanged(BR.lastName);
    }

    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Bindable
    public String getFirstName() {
        return this.firstName;
    }

    @Bindable
    public String getLastName() {
        return this.lastName;
    }

    @Override
    public String toString() {
        return "User{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }
}

分三步:1.继承BaseObservable 2.为getter方法注解@Bindable 3.setter方法内写notifyPropertyChanged(BR.xxx);之前没这么写是因为发现不用这样写也能出发UI变化,所以以为是新的sdk把操作简化了,原来还是要按官网的操作来的。

3.还是DrawerLayout的抽屉全屏问题

找到了一个更好的方案,不在xml里设置-64dp,而是在代码去动态读取当前window的信息,然后设置抽屉部分的宽度为窗口的宽度就行了。如下:

/** * 由于官方的drawer定死了拉出时与另一边的距离,在这个方法里使drawer布满整个屏幕 */
private void drawerMaximize() {
    DisplayMetrics metric = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metric);
    int windowsWight = metric.widthPixels;
    View leftMenu = findViewById(R.id.nav_frag_container);
    ViewGroup.LayoutParams leftParams = leftMenu.getLayoutParams();
    leftParams.width = windowsWight;
    leftMenu.setLayoutParams(leftParams);
}

你可能感兴趣的:(跑马灯;RxJava和DataBinding;)