Android 진급---android databinding 사용
android {
....
dataBinding {
enabled = true
}
}
2.기초 사용
public class SwordMan{
private String name;
private String level;
public SwordMan(String name, String level) {
this.name = name;
this.level = level;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="swordsman"
type="com.light.testdatabinding.SwordMan" />
data>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={swordsman.name}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{swordsman.leval}" />
LinearLayout>
layout>
ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
SwordMan swordMan=new SwordMan(" "," ");
binding.setSwordsman(swordMan);
3.지식 설명,API 문서 읽 기 자세히 소개
<data>
<import type="java.util.Date"/>
<variable
name="swordsman"
type="com.light.testdatabinding.SwordMan" />
<variable
name="time"
type="Date" />
</data>
ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
binding.test.setText(" ");
binding.btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
swordMan.setName(" ");
startActivity(new Intent(MainActivity.this,Main2Activity.class));
}
});
4.recyclerview 데이터 바 인 딩 사용
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
android.support.v7.widget.RecyclerView>
LinearLayout>
layout>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="com.light.testdatabinding.OBSwordMan" />
<variable
name="swordman"
type="OBSwordMan" />
data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{swordman.name}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{swordman.level}" />
LinearLayout>
layout>
public class OBSwordMan extends BaseObservable {
private String name;
private String level;
public OBSwordMan(String name, String level) {
this.name = name;
this.level = level;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
public class SwordmanAdapter extends RecyclerView.Adapter<SwordmanAdapter.SwordmamViewHoler> {
private List manList;
public SwordmanAdapter(List manList) {
this.manList = manList;
}
@Override
public SwordmamViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
RvItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.rv_item, parent, false);
return new SwordmamViewHoler(binding);
}
@Override
public void onBindViewHolder(SwordmamViewHoler holder, int position) {
OBSwordMan swordMan = manList.get(position);
holder.getBinding().setSwordman(swordMan);
}
@Override
public int getItemCount() {
return manList.size();
}
public class SwordmamViewHoler extends RecyclerView.ViewHolder {
RvItemBinding binding;
public SwordmamViewHoler(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = (RvItemBinding) binding;
}
public RvItemBinding getBinding() {
return binding;
}
}
}
public class Main2Activity extends AppCompatActivity {
private ActivityMain2Binding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main2);
initRecycler();
}
private void initRecycler() {
LinearLayoutManager manager = new LinearLayoutManager(this);
binding.recycler.setLayoutManager(manager);
SwordmanAdapter adapter = new SwordmanAdapter(getList());
binding.recycler.setAdapter(adapter);
}
public List getList() {
List list = new ArrayList<>();
OBSwordMan obSwordMan = new OBSwordMan(" ", " ");
OBSwordMan obSwordMan2 = new OBSwordMan(" 1", " ");
OBSwordMan obSwordMan3 = new OBSwordMan(" 2", " ");
OBSwordMan obSwordMan4 = new OBSwordMan(" 3", " ");
OBSwordMan obSwordMan5 = new OBSwordMan(" 4", " ");
list.add(obSwordMan);
list.add(obSwordMan2);
list.add(obSwordMan3);
list.add(obSwordMan4);
list.add(obSwordMan5);
return list;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
UWP에서 DataTemplate에 바인딩하는 방법무엇보다도 x:Bind 를 사용할 때 DataTemplate의 외부 컨텍스트에 바인딩할 수 없으므로 모든 경우에 x:Bind를 사용하는 것이 좋습니다. 어떤 경우에는 Binding가 작동한다고 주장할 수 있으며 이는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.