【android学习】Jetpack:DataBinding在recycleview中的使用

简介

这文章是结合前面一篇文章写的后续使用,给定的demo包含了上一篇文章的代码,如果有不了解的,可以移步

Jetpack:DataBinding数据绑定

 

直接上代码

public class MyAdapter extends RecyclerView.Adapter {

    public Context context;
    public List data;

    public MyAdapter(Context context, List data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item, parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Item item = data.get(position);
        if (position%2==0){
            item.setButtonVisiable(false);
        }
        ((MyViewHolder) holder).getBinding().setVariable(BR.item, item);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        ViewDataBinding binding = null;

        public MyViewHolder(ViewDataBinding itemView) {
            super(itemView.getRoot());
            this.binding = itemView;
        }

        public ViewDataBinding getBinding() {
            return binding;
        }

    }

}

item的代码

public class Item extends BaseObservable {
    private String name;
    private String password;
    private String imageUrl;

    private boolean isButtonVisiable = true;

    public Item(String name, String password, String imageUrl) {
        this.name = name;
        this.password = password;
        this.imageUrl = imageUrl;
    }
    @Bindable
    public boolean isButtonVisiable() {
        return isButtonVisiable;
    }

    public void setButtonVisiable(boolean buttonVisiable) {
        isButtonVisiable = buttonVisiable;
        notifyPropertyChanged(BR.buttonVisiable);
    }

    @Bindable
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }

    @Bindable
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        notifyPropertyChanged(BR.password);
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    //自定义属性:提供一个静态方法来加载image
    @BindingAdapter("bind:imageSrc")
    public static void getImage(ImageView view, String url) {
        Picasso.with(view.getContext()).load(url).into(view);
    }

    public void click(View view) {
        Toast.makeText(view.getContext(), getName(), Toast.LENGTH_SHORT).show();

    }
}

布局文件




    

        
        
    

    


        

        


        


        

总结

这里使用了一个表达式

 android:visibility="@{item.buttonVisiable ? View.VISIBLE : View.GONE}"

前面需要引用

我们在myAdapter中,偶数位设置隐藏

    if (position%2==0){
            item.setButtonVisiable(false);
        }

补充在listview中使用

这里是贴出了主要的getView方法

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewDataBinding dataBinding;
        if(convertView==null){
            dataBinding= DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.item,parent,false);
        }else{
            //就重就之前的
            dataBinding=DataBindingUtil.getBinding(convertView);
        }
        dataBinding.setVariable(BR.item,data.get(position));
        return dataBinding.getRoot().getRootView();
    }

demo下载demo下载地址

你可能感兴趣的:(android学习,Jetpack,DataBinding,recycleview)