我有一个通用的 RecycleView
我有不同的行类,在这个行类中我想实现一个接口,我的目标是传递 OnQueryTextListener
到实现接口的活动或片段。
public class PlantDocHeader extends LinearLayout implements RecycleViewGenericAdapter.RecyclerViewRowHeader<PlantDocViewModel> {
private Button buttonQuestion;
private Button buttonPosts;
private TextView searchTxtField;
private ImageView imageViewExpandSearch;
private SearchView searchView;
private boolean expand = true;
SearchView.OnQueryTextListener onQueryTextListener;
private onTextChange onTextChange;
Resources res;
public PlantDocHeader(Context context) {
super(context);
}
public PlantDocHeader(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public PlantDocHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
searchTxtField = findViewById(R.id.textView_plant_doc_header_search);
imageViewExpandSearch = findViewById(R.id.imageView_plant_doc_header_expand_button);
searchView = findViewById(R.id.searchView_plant_doc_header);
buttonQuestion = findViewById(R.id.button_plant_doc_header_filter_my_posts);
buttonPosts = findViewById(R.id.button_plant_doc_header_filter_question);
}
@Override
public void showData(PlantDocViewModel item) {
res = getResources();
buttonQuestion.setText(res.getString(R.string.question));
buttonQuestion.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AskQuestionFragment askQuestionFragment = new AskQuestionFragment();
ChangeFragment(askQuestionFragment, (Activity) getContext(), false);
}
});
buttonPosts.setText(res.getString(R.string.my_posts));
buttonPosts.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
PlantDocMyPostsFragment myPostsFragment = new PlantDocMyPostsFragment();
ChangeFragment(myPostsFragment, (Activity) getContext(), false);
}
});
searchTxtField.setText(res.getString(R.string.search));
imageViewExpandSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(expand){
searchView.setVisibility(VISIBLE);
imageViewExpandSearch.setImageResource(R.drawable.collapse);
expand = false;
}else{
searchView.setVisibility(GONE);
imageViewExpandSearch.setImageResource(R.drawable.expand);
expand = true;
}
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(getContext(), ""+newText, Toast.LENGTH_SHORT).show();
onTextChange.textSearch(newText);
return true;
}
});
}
public interface onTextChange {
void textSearch(String searchText);
}
}
这就是我犯的错误
尝试对空对象引用调用接口方法“void com.gardify.android.ui.plantdoc.plantdocheader$ontextchange.textsearch(java.lang.string)”
再循环通用适配器
/**
* @param <T> is generic parameter type provided to List, OnRecyclerViewItemClickListener and RecyclerViewRow
* @param <V> generic type for header view Model
* @param <E> generic type for footer view Model
*/
public class RecycleViewGenericAdapter<T, V, E> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<T> mDataset;
private V mHeaderData;
private E mFooterData;
private static final int TYPE_HEADER = 000;
private static final int TYPE_ITEM = 111;
private static final int TYPE_FOOTER = 222;
private OnItemClickListener<T> onItemClickListener;
private OnItemClickListenerHeader<V> onItemClickListenerHeader;
private OnItemClickListenerFooter<E> onItemClickListenerFooter;
private int layoutId, layoutIdHeader, layoutIdFooter;
public RecycleViewGenericAdapter(List<T> mDataset, int layoutId, V mHeaderData, int layoutIdHeader,
E mFooterData, int layoutIdFooter, OnItemClickListener<T> onItemClickListener,
OnItemClickListenerHeader onItemClickListenerHeader, OnItemClickListenerFooter onItemClickListenerFooter) {
this.onItemClickListener = onItemClickListener;
this.onItemClickListenerHeader = onItemClickListenerHeader;
this.onItemClickListenerFooter = onItemClickListenerFooter;
this.mDataset = mDataset;
this.mHeaderData = mHeaderData;
this.mFooterData = mFooterData;
this.layoutId = layoutId;
this.layoutIdHeader = layoutIdHeader;
this.layoutIdFooter = layoutIdFooter;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder vh = null;
if (viewType == TYPE_ITEM) {
RecyclerViewRow<T> row = (RecyclerViewRow<T>) LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
vh = new ItemViewHolder(row);
}else if (viewType == TYPE_HEADER) {
RecyclerViewRowHeader<V> rowHeader = (RecyclerViewRowHeader<V>) LayoutInflater.from(parent.getContext()).inflate(layoutIdHeader, parent, false);
vh = new HeaderViewHolder(rowHeader);
} else if(viewType == TYPE_FOOTER) {
RecyclerViewRowFooter<E> footerRow = (RecyclerViewRowFooter<E>) LayoutInflater.from(parent.getContext()).inflate(layoutIdFooter, parent, false);
vh = new FooterViewHolder(footerRow);
}
return vh;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof RecycleViewGenericAdapter.ItemViewHolder) {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
itemViewHolder.onBind((mHeaderData!=null? position-1 : position), itemViewHolder);
} else if (holder instanceof RecycleViewGenericAdapter.HeaderViewHolder) {
HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
headerViewHolder.onBind(headerViewHolder);
} else if (holder instanceof RecycleViewGenericAdapter.FooterViewHolder) {
FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
footerViewHolder.onBind(footerViewHolder);
}
}
@Override
public int getItemCount() {
if (mHeaderData != null && mFooterData != null) {
return mDataset.size() + 2;
} else if (mHeaderData != null || mFooterData != null) {
return mDataset.size() + 1;
}
return mDataset.size();
}
private int getLastPosition() {
return getItemCount() - 1;
}
private boolean isLastPosition(int position) {
return position == getLastPosition();
}
@Override
public int getItemViewType(int position) {
if (position == 0 && mHeaderData!=null) {
return TYPE_HEADER;
} else if (isLastPosition(position) && mFooterData!=null) {
return TYPE_FOOTER;
} else {
return TYPE_ITEM;
}
}
/**
* Item ViewHolder
**/
public class ItemViewHolder extends RecyclerView.ViewHolder {
public RecyclerViewRow<T> mRow;
public ItemViewHolder(RecyclerViewRow<T> itemView) {
super((View) itemView);
mRow = itemView;
}
private void onBind(final int position, ItemViewHolder viewHolder) {
viewHolder.mRow.showData(mDataset.get(position));
((View) viewHolder.mRow).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemClickListener != null) {
onItemClickListener.onItemClick(mDataset.get(position));
}
}
});
}
}
public interface RecyclerViewRow<T> {
void showData(T item);
}
public interface OnItemClickListener<T> {
void onItemClick(T position);
}
public void updateList(List<T> _mDataset){
this.mDataset= _mDataset;
notifyDataSetChanged();
}
/**
* Header ViewHolder
**/
public class HeaderViewHolder extends RecyclerView.ViewHolder {
public RecyclerViewRowHeader<V> mRowHeader;
public HeaderViewHolder(RecyclerViewRowHeader<V> itemView) {
super((View) itemView);
mRowHeader = itemView;
}
private void onBind(HeaderViewHolder viewHolder) {
viewHolder.mRowHeader.showData(mHeaderData);
((View) viewHolder.mRowHeader).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemClickListenerHeader != null) {
onItemClickListenerHeader.onItemClickHeader(mHeaderData);
}
}
});
}
}
public interface RecyclerViewRowHeader<V> {
void showData(V item);
}
public interface OnItemClickListenerHeader<V> {
void onItemClickHeader(V position);
}
/**
* Footer ViewHolder
**/
public class FooterViewHolder extends RecyclerView.ViewHolder {
public RecyclerViewRowFooter<E> mRowFooter;
public FooterViewHolder(RecyclerViewRowFooter<E> itemView) {
super((View) itemView);
mRowFooter = itemView;
}
private void onBind(FooterViewHolder viewHolder) {
viewHolder.mRowFooter.showData(mFooterData);
((View) viewHolder.mRowFooter).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (onItemClickListenerFooter != null) {
onItemClickListenerFooter.onItemClickFooter(mFooterData);
}
}
});
}
}
public interface RecyclerViewRowFooter<E> {
void showData(E item);
}
public interface OnItemClickListenerFooter<E> {
void onItemClickFooter(E position);
}
}
片段中的用法
public class PlantDocFragment extends Fragment implements RecycleViewGenericAdapter.OnItemClickListener<PlantDocViewModel>, PlantDocHeader.OnTextChangeListener {
//...
RecycleViewGenericAdapter<PlantDocViewModel, PlantDocViewModel, Nullable> adapter = new RecycleViewGenericAdapter<>(plantDocList, R.layout.recycler_view_plant_doc_row_item, plantDocHeader, R.layout.recycler_view_plant_doc_header,
null, 0, this, null, null);
//...
@Override
public void textSearch(String searchText) {
Toast.makeText(getContext(), "searched text : "+searchText, Toast.LENGTH_SHORT).show();
}
我正在使用的通用回收视图可以在这里找到
4条答案
按热度按时间rks48beu1#
你还没告诉我打电话时该怎么办
textSearch()
方法。你必须实施
onTextChange
接口(在声明接口/类/变量时请遵循java命名约定)在某个类中,您将在其中重写textSearch()
方法,然后将引用(实现的类)传递给变量onTextChange
(您已经为接口和示例变量指定了相同的名称,所以为了避免混淆,您可以为变量指定另一个名称)。当你打电话的时候textSearch()
在这条线上onTextChange.textSearch(newText);
你将有一个实现。你的代码也能用。68bkxrlz2#
首先,即使不是强制性的,您也应该始终遵循java命名约定,例如您的名字
interface
应该是这样的OnTextChangeListener
.你的错误是因为你做了手术,
onTextChange.textSearch(newText)
不为引用指定示例,onTextChange
.你可以这样做
huwehgph3#
在适配器中创建公共方法:
在你的片段中,改变你的
implements
要实现接口:然后设置ContextChange:
hi3rlvi24#
基本上,您需要将片段引用传递给
HeaderView
.首先,你得有个二传手
PlantDocHeader
如下所示。我把界面也去掉了,好像没用。对适配器构造函数重新措辞一点,它需要大量的参数构建器可能会有所帮助,或者您可以为可选属性创建单独的设置器。
为所有侦听器创建单独的setter,并根据调用组件的需要进行设置。现在,当你完成设置你需要通过
onTextChangeListener
到页眉视图。现在应该可以了。另外,您可能需要考虑在侦听器之间传递位置,因为可以有多个头,如果没有,则始终为0。
如果你试图创建一个泛型适配器,你应该考虑现在就创建一个抽象适配器类,你的类看起来不是泛型的,因为你在构造函数中传递了太多的参数,而这些参数应该只在子类中传递,它一点也不灵活。您可能需要检查一些开放源码通用适配器来查看。