sdf
This commit is contained in:
150
app/src/main/java/adapter/BaseRecycleViewAdapter.java
Normal file
150
app/src/main/java/adapter/BaseRecycleViewAdapter.java
Normal file
@@ -0,0 +1,150 @@
|
||||
package adapter;
|
||||
|
||||
/**
|
||||
* Created by 90432 on 2018/1/17.
|
||||
*/
|
||||
|
||||
import android.content.Context;
|
||||
import android.databinding.DataBindingUtil;
|
||||
import android.databinding.ViewDataBinding;
|
||||
import android.support.annotation.LayoutRes;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
||||
public class BaseRecycleViewAdapter<T1, T2 extends ViewDataBinding> extends RecyclerView.Adapter<BaseRecycleViewAdapter.BaseViewHolder> {
|
||||
private ArrayList<T1> list;
|
||||
private LayoutInflater inflater;
|
||||
private Context context;
|
||||
private AdapterView.OnItemClickListener itemClickListener;
|
||||
@LayoutRes
|
||||
private int layout;
|
||||
private BindView<T2> bindView;
|
||||
private static OnItemClickListener listener;
|
||||
private static OnLongItemClickListener longItemClickListener;
|
||||
|
||||
|
||||
|
||||
public BaseRecycleViewAdapter(Context context, @LayoutRes int layout) {
|
||||
inflater = LayoutInflater.from(context);
|
||||
this.context = context;
|
||||
this.layout = layout;
|
||||
list = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
||||
public void setData(ArrayList<T1> list) {
|
||||
this.list.clear();
|
||||
this.list=new ArrayList<>();
|
||||
this.list.addAll(list);
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
public void addData(ArrayList<T1> list) {
|
||||
this.list = list;
|
||||
this.list.addAll(list);
|
||||
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public void setOnBindViewHolder(BindView bindView) {
|
||||
this.bindView = bindView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = inflater.inflate(layout, parent, false);
|
||||
|
||||
return new BaseViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(BaseViewHolder holder, int position) {
|
||||
bindView.onBindViewHolder((T2) holder.getBinding(), position);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return list.size();
|
||||
}
|
||||
|
||||
public interface BindView<T2> {
|
||||
void onBindViewHolder(T2 b, int position);
|
||||
}
|
||||
public interface OnItemClickListener {
|
||||
void onItemClick(View itemView, int position);
|
||||
}
|
||||
public interface OnLongItemClickListener {
|
||||
void onItemLongClick(View itemView, int position);
|
||||
}
|
||||
public void setOnItemLongClickListener(OnLongItemClickListener longClickListener) {
|
||||
this.longItemClickListener = longClickListener;
|
||||
}
|
||||
public void setOnItemClickListener(OnItemClickListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public static class BaseViewHolder extends RecyclerView.ViewHolder {
|
||||
private ViewDataBinding b;
|
||||
|
||||
public BaseViewHolder(final View itemView) {
|
||||
super(itemView);
|
||||
b = DataBindingUtil.bind(itemView);
|
||||
itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (listener != null)
|
||||
listener.onItemClick(itemView, getLayoutPosition());
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
itemView.setOnLongClickListener(new View.OnLongClickListener(){
|
||||
@Override
|
||||
public boolean onLongClick(View v) {
|
||||
|
||||
if (longItemClickListener != null)
|
||||
longItemClickListener.onItemLongClick(itemView,getLayoutPosition());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
);
|
||||
}
|
||||
public ViewDataBinding getBinding() {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* @BindingAdapter({"app:imageUrl","app:placeholderDraw"})
|
||||
public static void setNetImg(ImageView ivNet, String imgUrl, Drawable placeHodler){
|
||||
Glide.with(ivNet.getContext()).load(imgUrl).placeholder(placeHodler).into(ivNet);
|
||||
}*/
|
||||
|
||||
|
||||
/* 作者:蚊子T
|
||||
链接:https://www.jianshu.com/p/eb14bcfdde32
|
||||
來源:简书
|
||||
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/
|
||||
|
||||
|
||||
|
||||
|
||||
/* 作者:陈SunMoon
|
||||
链接:https://www.jianshu.com/p/56428bdc1c69
|
||||
來源:简书
|
||||
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*/
|
||||
|
||||
Reference in New Issue
Block a user