Ich habe eine Basis recyclerview
erstellt und meine andere Klasse erweitert die Basisklasse. Ich habe das butterknife implementiert. Jedoch, wenn die andere Klasse die Basisklasse erweitert, bekomme ich die Ansicht als null.Butterknife funktioniert nicht mit der Adapterklasse
Unten ist mein Basisklasse
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();
}
Unter meinen Subclass
public class SubRecyclerAdapter extends BaseAdapter {
private Context context;
private List<LocationList> getLocationLists;
@Nullable
@BindView(R.id.branchimage)
ImageView branchimage;
public SubRecyclerAdapter(List<LocationList> getLocationLists,Context context) {
super(getLocationLists,context);
this.context=context;
this.getLocationLists=getLocationLists;
}
@Override
protected int getActivityLayout() {
return R.layout.locationlist;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Picasso.with(context) .load(getLocationLists.get(position).getBranch_image()) .placeholder(R.drawable.dummy_image)
.error(R.drawable.dummy_image) .into(branchimage);
//branchimage is null here
}
butter Version ist: -
compile 'com.jakewharton:butterknife:8.2.1'
apt 'com.jakewharton:butterknife-compiler:8.2.1'
Ich möchte ButterKnife in meine BaseAdapterClass injizieren.Ist es eine Möglichkeit, es zu tun.Vielen Dank für Ihren Kommentar – Soham
Warum können Sie dies in Ihrem Sichthalter hinzufügen: ButterKnife.bind (this, parent); Wie in der Antwort gesagt, und wenn Sie sie in der konkreten Klasse verwenden möchten, erstellen Sie einfach Ihre eigene abstrakte Methode und innerhalb der OnBindViewHolder() und innerhalb der OnBindViewHolder die Kesselbleche Operation, die Sie tun möchten, und rufen Sie die abstrakte Methode, so dass die konkrete Klasse wer immer die Methode implementiert, wird sich um seine eigenen Operationen kümmern. – Manikanta
Haben Sie meinen Code überprüft. Ich habe versucht, es zu tun, wie Sie im Kommentar gesagt haben. Sie können meinen Code ändern und meinen Fehler bitte korrigieren. – Soham