1.使layout 绑定databinding
2.创建ViewModel (这里使用的使rxjava+retrofit)
public class Bang_Meau_Vm extends ViewModel {
public MutableLiveData> Bang_Music_list=new MutableLiveData<>();
public void Get_Bang_Music_list(Context context , String bangId,String pn,String rn,String reqId){
Api.getInstance().iRetrofit.Bang_Music_list(bangId,pn,rn,reqId).compose(ApiSubscribe.>io_main())
.subscribe(new ApiResponse>(context) {
@Override
public void success(BaseRespon data) {
Bang_Music_list.setValue(data);
}
});
}
}
3.在activity或者frament中的使用
private Bang_Meau_Vm bang_meau_vm;
bang_meau_vm = ViewModelProviders.of(this).get(Bang_Meau_Vm.class);
4.databinding在activity 和frment以及自定义view中的使用
activity:
private BangMeauActivityBinding bangMeauActivityBinding;
bangMeauActivityBinding = DataBindingUtil.setContentView(this, R.layout.bang_meau_activity);
frament:
private Frament_Rec_VM framentRecVM;
private FramentRecBinding framentRecBinding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
framentRecBinding= DataBindingUtil.inflate(inflater,R.layout.frament_rec,container,false);
framentRecVM = ViewModelProviders.of(this).get(Frament_Rec_VM.class);
StatusBarUtil.setTranslucentForImageViewInFragment(getActivity(),0, null);
setVM();
initdata();
return framentRecBinding.getRoot();
}
自定义view中:
custombannerBinding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.custombanner,
this,true);
5.在recycleview 中的adapter中的使用
通用的viewholder
public class BindingViewHolder extends RecyclerView.ViewHolder {
private T mBinding;
public BindingViewHolder(@NonNull T binding) {
super(binding.getRoot());
mBinding = binding;
}
public T getBinding() {
return mBinding;
}
}
public class Bang_Meau_ItemApt extends RecyclerView.Adapter {
private Context context;
private List musicListBeans;
private BangMeauItemaptBinding bangMeauItemaptBinding;
private Bang_Meau_ItemApt.OnItemClick onItemClick;
public Bang_Meau_ItemApt(Context context, List musicListBeans){
this.context=context;
this.musicListBeans=musicListBeans;
}
@NonNull
@Override
public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
bangMeauItemaptBinding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.bang_meau_itemapt,parent,false);
return new BindingViewHolder(bangMeauItemaptBinding);
}
@Override
public void onBindViewHolder(@NonNull BindingViewHolder holder, final int position) {
ViewDataBinding binding = holder.getBinding();
binding.setVariable(BR.bangmeauitem,musicListBeans.get(position));
bangMeauItemaptBinding.tvBangMeauItemPos.setText(String.valueOf(position+1));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClick.OnItemClickListener(position);
}
});
}
@Override
public int getItemCount() {
return 5;
}
//ItemView 的点击接口
public interface OnItemClick{
void OnItemClickListener(int pos);
}
public void setOnItemClick(Bang_Meau_ItemApt.OnItemClick click){
this.onItemClick=click;
}
}
在grideview中使用
public class Gv_artist_albums_apt extends BaseAdapter {
private Context context;
private List artist_albums;
public Gv_artist_albums_apt(Context context, List artist_albums){
this.artist_albums=artist_albums;
this.context=context;
}
@Override
public int getCount() {
return artist_albums.size();
}
@Override
public Object getItem(int i) {
return artist_albums.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
GvArtistAlbumsAptBinding gvArtistAlbumsAptBinding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.gv_artist_albums_apt,viewGroup,false);
gvArtistAlbumsAptBinding.setVariable(BR.artistalbums, artist_albums.get(i));
return gvArtistAlbumsAptBinding.getRoot();
}
}