Android的databinding(二)

    技术2022-07-11  82

    标题这节讲一下recyclerview的数据绑定

    首先要准备recyclerview的item渲染layout xml <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" > <data> <import type="com.freestyle.myapplication.common.BindingConverter"/> <variable name="data" type="com.freestyle.myapplication.pojo.User" /> <variable name="event" type="com.freestyle.myapplication.activity.RecyclerViewActivity.EventListener" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/user_name1" android:onClick="@{()->{event.editUser(data);}}"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@{data.userName}" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="Phone: "/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@={data.phone}" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="Age: "/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@={BindingConverter.int2Str(data.age)}" /> <TextView android:layout_width="0dp" android:layout_weight="1" android:visibility="invisible" android:layout_height="wrap_content" /> <Button android:layout_width="34dp" android:layout_height="wrap_content" android:onClick="@{()->{event.deleteUser(data);}}" android:text="X" /> </LinearLayout> </layout> 再准备AdapterHolder, 很简单 public class BindingViewAdapterHolder<T extends ViewDataBinding> extends RecyclerView.ViewHolder { private T binding; public T getBinding(){ return binding; } public BindingViewAdapterHolder(@NonNull T binding) { super(binding.getRoot()); this.binding=binding; } } 准备recyclerview的适配器RecyclerViewAdapter * com.freestyle.myapplication.adapter Created by rocklee , 2020/6/29 ***/ public class RecyclerViewAdapter extends RecyclerView.Adapter<BindingViewAdapterHolder> { private List<User> list; private Context context; private LayoutInflater layoutInflater; private RecyclerViewActivity.EventListener eventListener; public RecyclerViewAdapter(List<User> list, Context context,RecyclerViewActivity .EventListener eventListener) { this.list = list; this.context = context; this.layoutInflater = ((Activity)context).getLayoutInflater(); this.eventListener=eventListener; } @NonNull @Override public BindingViewAdapterHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { ViewDataBinding dataBinding= DataBindingUtil.inflate(layoutInflater, R.layout .item_layout,viewGroup,false); dataBinding.setVariable(BR.event,eventListener); return new BindingViewAdapterHolder(dataBinding); } @Override public void onBindViewHolder(@NonNull BindingViewAdapterHolder bindingViewAdapterHolder, int i) { User it=list.get(i); ViewDataBinding binding=bindingViewAdapterHolder.getBinding(); //binding.setVariable(BR.,it); binding.setVariable(BR.data,it); binding.executePendingBindings(); //刷新list } @Override public int getItemCount() { return list.size(); } public RecyclerViewActivity.EventListener getEventListener() { return eventListener; } } 准备包含recyclerview的activity xml <data> <variable name="event" type="com.freestyle.myapplication.activity.RecyclerViewActivity.EventListener" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Add user" android:onClick="@{event::addItem}"/> </LinearLayout> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:id="@+id/userList" android:layout_height="0dp" android:layout_weight="1"> </android.support.v7.widget.RecyclerView> </LinearLayout> 编写recyclerviewActivity的oncreate代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); users=new ArrayList<>(); for (int i=0;i<100;i++){ User user=new User("user "+String.valueOf(i),"086-0"+String.valueOf(i+1)); user.setCates(Config.cate); user.setSex(Math.random()*10>5); user.setIconIndex((int)(Math.random()*2)); user.setCateIndex((int)(Math.random()*4)); user.setAge((int)(Math.random()*30)); users.add(user); } recyclerViewAdapter=new RecyclerViewAdapter(users,this,new EventListener()); binding= DataBindingUtil.setContentView(this, R.layout.activity_recycler_view); binding.userList.setLayoutManager( new LinearLayoutManager( this )); binding.userList.setAdapter(recyclerViewAdapter); binding.setVariable(BR.event,recyclerViewAdapter.getEventListener()); }

    跑起来

    原创不容易, 请点赞, 最后提供 代码下载

    Processed: 0.011, SQL: 9