Created
August 16, 2015 17:47
-
-
Save bdunovska/b7237374806db7ed23aa to your computer and use it in GitHub Desktop.
DataBindingExample
This file contains hidden or 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
<layout> | |
<data> | |
<variable | |
name="user" | |
type="com.example.belladunovska.databinding.User" /> | |
</data> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<TextView | |
android:id="@+id/first_name_list_item_text_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@{user.firstName}" /> | |
<Button | |
android:id="@+id/change_name_button" | |
android:layout_width="match_parent" | |
android:layout_height="30dp" /> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/recycler_view" | |
android:layout_width="match_parent" | |
android:layout_height="300dp" /> | |
</LinearLayout> | |
</layout> |
This file contains hidden or 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
<layout> | |
<data> | |
<variable | |
name="user" | |
type="com.example.belladunovska.databinding.User" /> | |
</data> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
android:orientation="horizontal"> | |
<TextView | |
android:id="@+id/first_name_list_item_text_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@{user.firstName}" /> | |
<TextView | |
android:id="@+id/last_name_list_item_text_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@{user.lastName}" /> | |
</LinearLayout> | |
</layout> |
This file contains hidden or 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
public class MainActivity extends AppCompatActivity { | |
private User user; | |
private LinearLayoutManager layoutManager; | |
private RecyclerView recyclerView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); | |
user = new User("Bella", "Dunovska", false); | |
binding.setUser(user); | |
layoutManager = new LinearLayoutManager(this); | |
recyclerView = binding.recyclerView; | |
recyclerView.setHasFixedSize(false); | |
recyclerView.setLayoutManager(layoutManager); | |
recyclerView.setAdapter(new RecyclerAdapter(this, generateDummyData())); | |
binding.changeNameButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
user.setFirstName("Rocks"); | |
} | |
}); | |
} | |
public ArrayList<User> generateDummyData(){ | |
ArrayList<User> rockstars = new ArrayList<>(); | |
User user = new User("Rob","Halford", true); | |
User user1 = new User("Bruce","Dickinson", true); | |
User user3 = new User("Till","Lindemann", true); | |
rockstars.add(user); | |
rockstars.add(user1); | |
rockstars.add(user3); | |
return rockstars; | |
} | |
} |
This file contains hidden or 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
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> { | |
private Context context; | |
private ArrayList<User> users; | |
public RecyclerAdapter(Context context, ArrayList<User> users) { | |
this.users = users; | |
this.context = context; | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
LayoutInflater layoutInflater = (LayoutInflater) context | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
ItemListBinding listItemUserBinding = DataBindingUtil.inflate(layoutInflater, R.layout.item_list, parent, false); | |
return new ViewHolder(listItemUserBinding); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, int position) { | |
holder.listItemUserBinding.setUser(users.get(position)); | |
} | |
@Override | |
public int getItemCount() { | |
return users.size(); | |
} | |
class ViewHolder extends RecyclerView.ViewHolder { | |
private ItemListBinding listItemUserBinding; | |
public ViewHolder(ItemListBinding itemListBinding) { | |
super(itemListBinding.getRoot()); | |
this.listItemUserBinding = itemListBinding; | |
} | |
} | |
} |
This file contains hidden or 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
public class User extends BaseObservable { | |
public String firstName; | |
public String lastName; | |
public boolean isRockStar; | |
public User(String firstName, String lastName, boolean isRockStar) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.isRockStar = isRockStar; | |
} | |
@Bindable | |
public String getFirstName() { | |
return firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public boolean getRockStar() { | |
return isRockStar; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
notifyPropertyChanged(com.example.belladunovska.databinding.BR.firstName); | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment