Last active
July 6, 2017 14:52
-
-
Save vogella/6ee271996337fc25d95376cae62b4de5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout xmlns:android="http://schemas.android.com/apk/res/android"> | |
<data> | |
<variable | |
name="temp" | |
type="com.vogella.android.databinding.TemperatureData" /> | |
<variable | |
name="presenter" | |
type="com.vogella.android.databinding.MainActivityPresenter"/> | |
</data> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@={temp.location}" | |
/> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@={temp.celsius}" | |
/> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="8dp" | |
android:text="@={temp.celsius}" /> | |
<Button | |
android:text="Show data model" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:onClick="@{() -> presenter.onShowData(temp)}" | |
android:id="@+id/button" /> | |
<Button | |
android:text="Start second activity" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:onClick="@{() -> presenter.showList()}" | |
/> | |
</LinearLayout> | |
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vogella.android.databinding; | |
import android.databinding.DataBindingUtil; | |
import android.databinding.ViewDataBinding; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.ViewGroup; | |
public abstract class MyBaseAdapter extends RecyclerView.Adapter<MyBaseAdapter.MyViewHolder> { | |
// Provide a reference to the views for each data item | |
// Complex data items may need more than one view per item, and | |
// you provide access to all the views for a data item in a view holder | |
public class MyViewHolder extends RecyclerView.ViewHolder { | |
// each data item is just a string in this case | |
private final ViewDataBinding binding; | |
public MyViewHolder(ViewDataBinding binding) { | |
super(binding.getRoot()); | |
this.binding = binding; | |
} | |
public void bind(Object obj) { | |
binding.setVariable(BR.obj,obj); | |
binding.executePendingBindings(); | |
} | |
} | |
@Override | |
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
// create a new view | |
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); | |
ViewDataBinding binding = DataBindingUtil.inflate(layoutInflater, getLayoutIdForType(viewType), parent, false); | |
// set the view's size, margins, paddings and layout parameters | |
return new MyViewHolder(binding); | |
} | |
// Replace the contents of a view (invoked by the layout manager) | |
@Override | |
public void onBindViewHolder(MyViewHolder holder, int position) { | |
holder.bind(getDataAtPosition(position)); | |
} | |
public abstract Object getDataAtPosition(int position); | |
public abstract int getLayoutIdForType(int viewType); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment