Java Butterknife不使用适配器类
我已经创建了一个baseJava Butterknife不使用适配器类,java,android,android-recyclerview,butterknife,Java,Android,Android Recyclerview,Butterknife,我已经创建了一个baserecyclerview,我的另一个类正在扩展这个base类。我已经实现了。但是,当另一个类扩展基类时,我得到的视图为null 下面是我的基础课 public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseAdapter.MyViewHolder> { private List<T> mObjects; private Co
recyclerview
,我的另一个类正在扩展这个base类。我已经实现了。但是,当另一个类扩展基类时,我得到的视图为null
下面是我的基础课
public abstract class BaseAdapter<T>
extends RecyclerView.Adapter<BaseAdapter.MyViewHolder> {
private List<T> mObjects;
private Context context;
public BaseAdapter(final List<T> objects, Context context) {
this.mObjects = objects;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(getActivityLayout(), parent, false);
ButterKnife.bind(this,parent);
return new MyViewHolder(itemView);
}
/**
* Adds the specified object at the end of the array.
*
* @param object The object to add at the end of the array.
*/
public void add(final T object) {
mObjects.add(object);
notifyItemInserted(getItemCount() - 1);
}
/**
* Remove all elements from the list.
*/
public void clear() {
final int size = getItemCount();
mObjects.clear();
notifyItemRangeRemoved(0, size);
}
@Override
public int getItemCount() {
return mObjects.size();
}
public T getItem(final int position) {
return mObjects.get(position);
}
public long getItemId(final int position) {
return position;
}
/**
* Returns the position of the specified item in the array.
*
* @param item The item to retrieve the position of.
* @return The position of the specified item.
*/
public int getPosition(final T item) {
return mObjects.indexOf(item);
}
/**
* Inserts the specified object at the specified index in the array.
*
* @param object The object to insert into the array.
* @param index The index at which the object must be inserted.
*/
public void insert(final T object, int index) {
mObjects.add(index, object);
notifyItemInserted(index);
}
/**
* Removes the specified object from the array.
*
* @param object The object to remove.
*/
public void remove(T object) {
final int position = getPosition(object);
mObjects.remove(object);
notifyItemRemoved(position);
}
/**
* Sorts the content of this adapter using the specified comparator.
*
* @param comparator The comparator used to sort the objects contained in this adapter.
*/
public void sort(Comparator<? super T> comparator) {
Collections.sort(mObjects, comparator);
notifyItemRangeChanged(0, getItemCount());
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View view) {
super(view);
}
}
protected abstract int getActivityLayout();
}
如果要在适配器中使用ButterKnife,则需要在视图保持架和onCreateViewHolder中使用此方法:
//View Holder for performance optimizations of the recycler view display
static class AddProductsViewHolder extends RecyclerView.ViewHolder{
@InjectView(R.id.product_image)
ImageView productImage;
@InjectView(R.id.product_name)
TextView productName;
@InjectView(R.id.product_brand)
TextView productBrand;
public final View mView;
public AddProductsViewHolder(View itemView) {
super(itemView);
mView = itemView;
ButterKnife.inject(this, itemView);
}
}
@Override
public AddProductsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View productView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row_products, parent, false);
return new AddProductsViewHolder(productView);
}
用法:
@Override
public void onBindViewHolder(AddProductsViewHolder holder, int position) {
try{
AddProductInfo addProductInfo = addProductInfoList.get(position);
if(!addProductInfo.getProductImage().isEmpty()){
holder.productName.setText(addProductInfo.getProductName());
holder.productBrand.setText(addProductInfo.getProductBrand());
}
catch (Exception e){
}
}
如果您有任何疑问,请在下面进行评论
使用最新版本的butterknife,您只需将@InjectView
替换为@Bind
这是我的旧代码,但我认为它符合您的目的。我想在我的BaseAdapterClass中注入ButterKnife。有什么方法可以这样做吗。感谢您的评论为什么您不能将此添加到视图持有者:ButterKnife.bind(这个,父级);如回答中所述,如果您想在具体类中使用它们,只需在onBindViewHolder()中创建自己的抽象方法在onBindViewHolder中,执行您想要执行的锅炉板操作,并调用抽象方法,以便实现该方法的具体类将处理其自身的操作。您是否检查了我的代码。我正试图按照您在评论中所说的那样做。您能修改我的代码并更正我的错误吗?为什么需要baseAdapter及其自定义实现,您可以直接在SubRecyclerAdapter()本身中执行此操作,适配器(具有相同的布局和功能)是否在这么多地方使用?我知道,但我想在基本适配器类中执行许多实现。因此,如果您能以这种方式帮助我,那就更好了。
//View Holder for performance optimizations of the recycler view display
static class AddProductsViewHolder extends RecyclerView.ViewHolder{
@InjectView(R.id.product_image)
ImageView productImage;
@InjectView(R.id.product_name)
TextView productName;
@InjectView(R.id.product_brand)
TextView productBrand;
public final View mView;
public AddProductsViewHolder(View itemView) {
super(itemView);
mView = itemView;
ButterKnife.inject(this, itemView);
}
}
@Override
public AddProductsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View productView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row_products, parent, false);
return new AddProductsViewHolder(productView);
}
@Override
public void onBindViewHolder(AddProductsViewHolder holder, int position) {
try{
AddProductInfo addProductInfo = addProductInfoList.get(position);
if(!addProductInfo.getProductImage().isEmpty()){
holder.productName.setText(addProductInfo.getProductName());
holder.productBrand.setText(addProductInfo.getProductBrand());
}
catch (Exception e){
}
}