1.使layout 绑定databinding
image.png
2.创建ViewModel (这里使用的使rxjava+retrofit)
public class Bang_Meau_Vm extends ViewModel {
public MutableLiveData<BaseRespon<Bang_Music_list>> 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.<BaseRespon<Bang_Music_list>>io_main())
.subscribe(new ApiResponse<BaseRespon<Bang_Music_list>>(context) {
@Override
public void success(BaseRespon<Bang_Music_list> 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 <T extends ViewDataBinding> 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<BindingViewHolder> {
private Context context;
private List<Bang_list.MusicListBean> musicListBeans;
private BangMeauItemaptBinding bangMeauItemaptBinding;
private Bang_Meau_ItemApt.OnItemClick onItemClick;
public Bang_Meau_ItemApt(Context context, List<Bang_list.MusicListBean> 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_Album.AlbumListBean> artist_albums;
public Gv_artist_albums_apt(Context context, List<Artist_Album.AlbumListBean> 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();
}
}
网友评论