gpt4 book ai didi

android - 回收站 View 未调用 getItemCount

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:19 32 4
gpt4 key购买 nike

我刚刚制作了一个回收器 View ,但它没有工作,所以我在 getItemCount 上设置了一个断点,但该方法没有被调用。我从未见过 SO 上的任何其他人有这个特定问题,我相信这是非常明显的事情。这是我的代码。

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private ArrayList<Object> displayList;

public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View v) {
super(v);
}
}

public class ErrorSearchItem extends ViewHolder {
//this is here if there is no other viewholder

public ErrorSearchItem(View view) {
super(view);

}
}

public class HeaderViewHolder extends ViewHolder {


public HeaderViewHolder(View view) {
super(view);

}
}



@Override
public int getItemViewType(int position) {

return position;
}

public SearchAdapter(ArrayList<Object> displayList) {
this.displayList = displayList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (displayList.get(viewType) instanceof String){
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_search_title, parent, false);
return new HeaderViewHolder(itemView);
}
else{ //this is for if there is an error and no other xml files match
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_error_search_item, parent, false);
return new ErrorSearchItem(itemView);
}
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

}


@Override
public int getItemCount() {

return displayList.size();
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);

}

}

编辑: fragment 代码:

private RecyclerView recyclerView;
private SearchAdapter searchAdapter;

recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);

searchAdapter = new SearchAdapter(categorizedArray);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(searchAdapter);

最佳答案

在应用适配器之前添加recycler.setLayoutManager(new LinearLayoutManager(this));

关于android - 回收站 View 未调用 getItemCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41522567/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com