使用步骤:
1、在build.gradle添加如下:
android {
dataBinding {
enabled = true
}
...........
2、我为Activity和Fragment分别写了一个Base
BaseActivity.java
//注意BaseActivity的泛型
public abstract class BaseActivity<SV extends ViewDataBinding> extends FragmentActivity implements View.OnClickListener {
protected SV bindingView;
//相当于findViewById();
protected <T extends View> T getView(int id) {
return (T) findViewById(id);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindView();
initViews();
}
//实例化BindingView
protected void bindView() {
bindingView = DataBindingUtil.setContentView(this, getLayoutResId());
}
//得到布局
protected abstract int getLayoutResId();
//实例化
public abstract void initViews();
public abstract void processClick(View v);
public void onClick(View v) {
processClick(v);
}
}
BaseFragment.java
public abstract class BaseFragment<SV extends ViewDataBinding> extends Fragment implements View.OnClickListener {
protected SV bindingView;
private View view;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = getActivity().getLayoutInflater().inflate(setContent(), container, false);
bindingView = DataBindingUtil.bind(view);
initView();
setData();
return view;
}
protected <T extends View> T getView(int id) {
return (T) view.findViewById(id);
}
/**
* 布局
*/
public abstract int setContent();
/**
* 初始化布局
*/
public abstract void initView();
public abstract void setData();
public abstract void processClick(View v);
@Override
public void onClick(View v) {
processClick(v);
}
}
4、具体代码 ,请求接口展示数据(Activity为例)
1>.根据请求数据写实体类,我的如下:
接口有点操蛋,实体类将就着看
public class WatingModel {
private String id;
private String doctor_name;
private String patient;
private String department_name;
private String department_area;
private String wait;
private String nowid;
private String nowtime;
private String go_time;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
private String status;
public String getNowtime() {
return nowtime;
}
public void setNowtime(String nowtime) {
this.nowtime = nowtime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDoctor_name() {
return doctor_name;
}
public void setDoctor_name(String doctor_name) {
this.doctor_name = doctor_name;
}
public String getPatient() {
return patient;
}
public void setPatient(String patient) {
this.patient = patient;
}
public String getDepartment_name() {
return department_name;
}
public void setDepartment_name(String department_name) {
this.department_name = department_name;
}
public String getDepartment_area() {
return department_area;
}
public void setDepartment_area(String department_area) {
this.department_area = department_area;
}
public String getWait() {
return wait;
}
public void setWait(String wait) {
this.wait = wait;
}
public String getNowid() {
return nowid;
}
public void setNowid(String nowid) {
this.nowid = nowid;
}
public String getGo_time() {
return go_time;
}
public void setGo_time(String go_time) {
this.go_time = go_time;
}
}
2、布局
几点注意的地方:
1>最外层的布局用
2>数据源
3>里面有来个属性,name是你在代码中实体类的对象 type是你的实体类,包名加类名
4>赋值:在控件的text属性中直接赋值:eg. android:text=”@{model.department_name}” 书写时会有提示
<layout
xmlns:android="http://schemas.android.com/apk/res/android" >
<data>
<variable
name="model"
type="com.xinghongkeji.ydzh.wisdommedical.model.WatingModel"></variable> </data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_paren“
android:orientation="vertical">
<include layout="@layout/alltitle_simple"></include>
<ScrollView
android:id="@+id/sc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/list_bg"
android:scrollbars="none">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_20"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_16"
android:text="科室: "
android:textColor="@color/black"
android:textSize="@dimen/textSize_18" />
<TextView
android:id="@+id/department"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@{model.department_name}"
android:textColor="@color/content_color"
android:textSize="@dimen/textSize_18" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/item_gray"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_16"
android:text="就诊人: "
android:textColor="@color/black"
android:textSize="@dimen/textSize_18" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{model.patient}"
android:layout_gravity="center_vertical"
android:textColor="@color/content_color"
android:textSize="@dimen/textSize_18" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_16"
android:text="就诊医生:"
android:textColor="@color/black"
android:textSize="@dimen/textSize_18" />
<TextView
android:id="@+id/doctor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@{model.doctor_name}"
android:textColor="@color/content_color"
android:textSize="@dimen/textSize_18" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/item_gray"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_16"
android:text="就诊时间:"
android:textColor="@color/black"
android:textSize="@dimen/textSize_18" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@{model.go_time}"
android:textColor="@color/content_color"
android:textSize="@dimen/textSize_18" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_16"
android:text="就诊地点:"
android:textColor="@color/black"
android:textSize="@dimen/textSize_18" />
<TextView
android:id="@+id/area"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@{model.department_area}"
android:textColor="@color/content_color"
android:textSize="@dimen/textSize_18" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
3、代码中操作:
public class WaitingDetailsActivity extends BaseActivity {//binding名字是布局名字依照驼峰命名法书写+Binding决定
private TextView title;
private Dialog progressDialog;
private String card_id, id;
private WatingModel.Data model;//实体类
@Override
protected int getLayoutResId() {
return R.layout.activity_waiting;//布局
}
@Override
public void initViews() {
//加载圈
progressDialog = LabelUtils.createLoadingDialog(WaitingDetailsActivity.this, "加载中...", true, 0);
card_id = getIntent().getStringExtra("card_id");
id = getIntent().getStringExtra("id");
title = getView(R.id.title);//对于引入的布局bindingView.无法直接获取其子控件;
getView(R.id.back).setOnClickListener(this);
title.setText("标题");
//bingView.可直接获取控件对象,无需fandViewById();
bindingView.refresh.setOnClickListener(this);
getData();
}
@Override
public void processClick(View v) {
switch (v.getId()) {
case R.id.back:
finish();
break;
}
}
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
progressDialog.dismiss();
if (msg.what == 1) {
String result = msg.obj.toString();
try {
JSONObject json = new JSONObject(result);
String code = json.getString("code");
boolean status = json.getBoolean("status");
String info = json.getString("info");
if (status) {
JSONArray array = json.getJSONArray("data");
JSONObject jsonObject = array.getJSONObject(0);
Gson gson = new Gson();
model = gson.fromJson(jsonObject.toString(), WatingModel.Data.class);
bindingView.setModel(model);//别忘记这句话,请求到数据后写,很重要,只有在布局中写到实体类,在代码中才有这个方法,方法名是你布局中的name;
} else {
LabelUtils.toast(WaitingDetailsActivity.this, code + info);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else if (msg.what == 3) {
LabelUtils.toast(WaitingDetailsActivity.this, getResources().getString(R.string.quest_fail));
}
return false;
}
});
public void getData() {
progressDialog.show();
HashMap<String, String> map = new HashMap<>();
map.put("card_id", card_id + "");
map.put("member_id", LoginDataModel.getLoginData().getMember_id() + "");
OkhttpTool.getNetData("请求地址", map).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
if (response.isSuccessful()) {
Message msg = new Message();
msg.what = 1;
msg.obj = result;
handler.sendMessage(msg);
} else {
Message msg = new Message();
msg.what = 3;
handler.sendMessage(msg);
}
}
});
}
}