Android官方DataBinding(十一):对双向绑定之反向绑定的改进和简化

时间:2021-03-27 09:28:03
Android官方DataBinding(十一):对于双向绑定之反向绑定的改进和简化


本文接附录十文章,对附录十文章的代码和逻辑进行改进和简化,使得代码更易于阅读和理解,进一步清晰化文章十的下拉刷新实现代码和逻辑。


建立model:
package zhangphil.test;

import android.databinding.BaseObservable;
import android.databinding.ObservableBoolean;

/**
 * Created by Phil on 2017/8/25.
 */

public class ViewModel extends BaseObservable {
    public final ObservableBoolean isRefreshing = new ObservableBoolean();
}



写布局activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <import type="android.view.View" />

        <variable
            name="model"
            type="zhangphil.test.ViewModel" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ProgressBar
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center_horizontal"
            android:visibility="@{model.isRefreshing ? View.VISIBLE:View.GONE}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@android:color/holo_red_light"
            android:gravity="center"
            android:text="加载中..."
            android:visibility="@{model.isRefreshing ? View.VISIBLE:View.GONE}" />

        <zhangphil.test.PhilView
            android:id="@+id/philview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light"
            app:refreshing="@={model.isRefreshing}">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:gravity="center"
                    android:text="z" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:gravity="center"
                    android:text="h" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:gravity="center"
                    android:text="a" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:gravity="center"
                    android:text="n" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:gravity="center"
                    android:text="g" />
            </LinearLayout>

        </zhangphil.test.PhilView>
    </LinearLayout>
</layout>



自定义的双向绑定PhilView.java:

package zhangphil.test;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.InverseBindingAdapter;
import android.databinding.InverseBindingListener;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.util.Log;

/**
 * Created by Phil on 2017/8/25.
 */

public class PhilView extends NestedScrollView {
    private static final String TAG = "调试";
    private static boolean isRefreshing = false;

    public PhilView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @BindingAdapter(value = "refreshing", requireAll = false)
    public static void setRefreshing(PhilView view, boolean refreshing) {
        if (isRefreshing == refreshing) {
            //防止死循环
            Log.d(TAG, "重复设置");
            return;
        } else {
            Log.d(TAG, "setRefreshing " + refreshing);
            isRefreshing = refreshing;
        }
    }

    @InverseBindingAdapter(attribute = "refreshing", event = "refreshingAttrChanged")
    public static boolean getRefreshing(PhilView view) {
        return isRefreshing;
    }

    @BindingAdapter(value = {"refreshingAttrChanged"}, requireAll = false)
    public static void setRefreshingAttrChanged(PhilView view, final InverseBindingListener inverseBindingListener) {
        if (inverseBindingListener == null) {
            Log.e(TAG, "InverseBindingListener为空!");
        } else {
            Log.d(TAG, "setRefreshingAttrChanged");
            inverseBindingListener.onChange();
        }
    }
}



测试的MainActivity.java:
package zhangphil.test;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import zhangphil.test.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {
    private final String TAG = "MainActivity调试";
    private ViewModel mViewModel;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final GestureDetector mGestureDetector = createGestureDetector();

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        mViewModel = new ViewModel();
        binding.setModel(mViewModel);

        binding.philview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                mGestureDetector.onTouchEvent(motionEvent);
                return false;
            }
        });
    }

    private GestureDetector createGestureDetector() {
        // mGestureDetector用于监测用户在手机屏幕上的上滑和下滑事件。
        GestureDetector mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                //处于顶部且用户再下拉
                if ((distanceY < 0) && (binding.philview.getScrollY() == 0)) {
                    if (mViewModel.isRefreshing.get()) {
                        Log.d(TAG, "加载中,请勿重复加载");
                    } else {
                        Log.d(TAG, "开始下拉刷新...");

                        //执行下拉刷新/加载更多事务
                        loadMore();
                    }
                }

                return super.onScroll(e1, e2, distanceX, distanceY);
            }
        });

        return mGestureDetector;
    }

    private void loadMore() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //通过设置布尔值改变View
                mViewModel.isRefreshing.set(true);

                try {
                    //假设这里做了一个长时间的耗时操作
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //通过设置布尔值改变View
                mViewModel.isRefreshing.set(false);
            }
        }).start();
    }
}


代码运行结果,下拉刷新:

Android官方DataBinding(十一):对双向绑定之反向绑定的改进和简化



附录:
1,《Android官方DataBinding简例(一)》链接:http://blog.csdn.net/zhangphil/article/details/77322530    
2,《Android官方DataBinding(二):动态数据更新notifyPropertyChanged》链接:http://blog.csdn.net/zhangphil/article/details/77328688   
3,《Android官方DataBinding(三):RecyclerView 使用ViewDataBinding更新数据》链接:http://blog.csdn.net/zhangphil/article/details/77367432  
4,《Android官方DataBinding(四):BindingAdapter》链接:http://blog.csdn.net/zhangphil/article/details/77374211 
5,《Android官方DataBinding(五):ObservableMap,ObservableArrayMap》链接:http://blog.csdn.net/zhangphil/article/details/77448933
6,《Android官方DataBinding(六): @= 操作符进行双向绑定》链接:http://blog.csdn.net/zhangphil/article/details/77454045 
7,《Android官方DataBinding(七):BindingMethods与BindingMethod》链接:http://blog.csdn.net/zhangphil/article/details/77479843 
8,《Android官方DataBinding(八):Lambda长表达式事件处理》链接:http://blog.csdn.net/zhangphil/article/details/77503013 
9,《Android官方DataBinding(九):反向绑定,View变化结果回写进数据模型中》链接:http://blog.csdn.net/zhangphil/article/details/77649256  
10,《Android官方DataBinding(十):双向绑定之基于InverseBindingAdapter的反向绑定》链接:http://blog.csdn.net/zhangphil/article/details/77767513