@Override
public void onItemRangeInserted(ObservableArrayList newItems, int i, int i1) {
BaseBindingAdapter.this.onItemRangeInserted(newItems, i, i1);
}
@Override
public void onItemRangeMoved(ObservableArrayList newItems, int i, int i1, int i2) {
BaseBindingAdapter.this.onItemRangeMoved(newItems);
}
@Override
public void onItemRangeRemoved(ObservableArrayList sender, int positionStart, int itemCount) {
BaseBindingAdapter.this.onItemRangeRemoved(sender, positionStart, itemCount);
}
}
}
- BaseBindingViewHolder
package tsou.cn.databinding.viewholder;
import android.support.v7.widget.RecyclerView;
import android.view.View;
/**
- Created by Administrator on 2018/5/31 0031.
*/
public class BaseBindingViewHolder extends RecyclerView.ViewHolder {
public BaseBindingViewHolder(View itemView) {
super(itemView);
}
}
- 创建StudentAdapter
package tsou.cn.databinding.adapter;
import android.content.Context;
import tsou.cn.databinding.R;
import tsou.cn.databinding.bean.Student;
import tsou.cn.databinding.databinding.ItemStudentBinding;
/**
- Created by Administrator on 2018/5/31 0031.
*/
public class StudentAdapter extends BaseBindingAdapter<Student,ItemStudentBinding> {
public StudentAdapter(Context context) {
super(context);
}
@Override
protected int getLayoutResId(int viewType) {
return R.layout.item_student;
}
@Override
protected void onBindItem(ItemStudentBinding binding, Student item) {
binding.setModel(item);
binding.executePendingBindings();
}
}
- 列表布局item_student
<variable
name=“model”
type=“Student” />
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“60dp”
android:layout_marginBottom=“10dp”
android:background=“@android:color/darker_gray”
android:gravity=“center_vertical”
android:orientation=“vertical”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@{model.name}” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@{String.valueOf(model.age)}” />
- 创建主布局activity_recycler_view
<layout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”>
<variable
name=“viewModel”
type=“tsou.cn.databinding.viewmodel.RecyclerViewModel” />
<RelativeLayout
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“tsou.cn.databinding.RecyclerViewActivity”>
<android.support.v7.widget.RecyclerView
android:id=“@+id/recyclerView”
android:layout_width=“match_parent”
android:layout_height=“match_parent” />
<Button
android:id=“@+id/add_data”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentBottom=“true”
android:layout_alignParentLeft=“true”
android:onClick=“@{viewModel::addData}”
android:text=“添加” />
<Button
android:id=“@+id/delete_data”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_alignParentBottom=“true”
android:layout_alignParentRight=“true”
android:onClick=“@{viewModel::deleteData}”
android:text=“删除” />
- Activity调用
package tsou.cn.databinding;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import java.util.ArrayList;
import tsou.cn.databinding.adapter.StudentAdapter;
import tsou.cn.databinding.bean.Student;
import tsou.cn.databinding.databinding.ActivityRecyclerViewBinding;
import tsou.cn.databinding.viewmodel.RecyclerViewModel;
public class RecyclerViewActivity extends AppCompatActivity {
private ActivityRecyclerViewBinding binding;
private StudentAdapter adapter;
private RecyclerViewModel viewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_recycler_view);
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new StudentAdapter(this);
binding.recyclerView.setAdapter(adapter);
initData();
// adapter.getItems().add(new Student(“张三”, 18));
// adapter.getItems().add(new Student(“李四”, 28));
// adapter.getItems().add(new Student(“王五”, 38));
viewModel = new RecyclerViewModel(this, binding, adapter);
//使用对象
binding.setViewModel(viewModel);
}
private void initData() {
ArrayList students = new ArrayList<>();
students.add(new Student(“张三”, 18));
students.add(new Student(“李四”, 28));
students.add(new Student(“王五”, 38));
adapter.getItems().addAll(students);
}
}
- ViewModel处理逻辑
package tsou.cn.databinding.viewmodel;
import android.content.Context;
import android.view.View;
import java.util.ArrayList;
import tsou.cn.databinding.adapter.StudentAdapter;
import tsou.cn.databinding.bean.Student;
import tsou.cn.databinding.databinding.ActivityRecyclerViewBinding;
/**
- Created by Administrator on 2018/5/31 0031.
*/
public class RecyclerViewModel {
private Context mContext;
private ActivityRecyclerViewBinding mBinding;
private StudentAdapter mAdapter;
public RecyclerViewModel(Context context,
ActivityRecyclerViewBinding binding,
StudentAdapter adapter) {
this.mContext = context;
this.mBinding = binding;
this.mAdapter = adapter;
}
public void addData(View view) {
文末
那么对于想坚持程序员这行的真的就一点希望都没有吗?
其实不然,在互联网的大浪淘沙之下,留下的永远是最优秀的,我们考虑的不是哪个行业差哪个行业难,就逃避掉这些,无论哪个行业,都会有他的问题,但是无论哪个行业都会有站在最顶端的那群人。我们要做的就是努力提升自己,让自己站在最顶端,学历不够那就去读,知识不够那就去学。人之所以为人,不就是有解决问题的能力吗?挡住自己的由于只有自己。
Android希望=技能+面试
- 技能
- 面试技巧+面试题
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
,我们考虑的不是哪个行业差哪个行业难,就逃避掉这些,无论哪个行业,都会有他的问题,但是无论哪个行业都会有站在最顶端的那群人。我们要做的就是努力提升自己,让自己站在最顶端,学历不够那就去读,知识不够那就去学。人之所以为人,不就是有解决问题的能力吗?挡住自己的由于只有自己。
Android希望=技能+面试
- 技能
[外链图片转存中…(img-DL0ClMXj-1714226142983)] - 面试技巧+面试题
[外链图片转存中…(img-YL7pOqlC-1714226142983)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!