问题描述
我有一个 RecyclerView
,我使用。我已经使用 RecyclerView
过,但从来没有过这个问题。
当我向上和向下滚动一些物品消失,一些消失的项目在底部再次出现。
code:
ViewHolder
:
公共类ViewHolder扩展RecyclerView.ViewHolder {
公众的TextView TXT; 公共ViewHolder(查看视图){
超(视图);
TXT =(TextView中)view.findViewById(R.id.txt);
}
}
适配器
:
公共类MyAdapter扩展RecyclerView.Adapter< ViewHolder> {
私人最终活动活动;
私人最终的ArrayList<&HashMap的LT;字符串,字符串>> mItems; 公共MyAdapter(活动活动,ArrayList的<&HashMap的LT;字符串,字符串>> mItems){
this.activity =活动;
this.mItems = mItems;
} @覆盖
公共ViewHolder onCreateViewHolder(ViewGroup中ViewGroup中,int i)以{
返回新ViewHolder(LayoutInflater.from(活动).inflate(R.layout.items,ViewGroup中,FALSE));
} @覆盖
公共无效onBindViewHolder(ViewHolder viewHolder,INT位置){
HashMap的<字符串,字符串>项目= mItems.get(位置); 字符串信息= item.get(信息);
如果(信息!= NULL){
viewHolder.txt.setText(信息);
}其他{
viewHolder.txt.setVisibility(View.GONE);
}
} @覆盖
公众诠释getItemCount(){
返回(空= mItems mItems.size():?0);
}
}
onBindViewHolder
重用意见,让我们说第一次 onBindViewHolder()
被称为信息
是空
。这将导致该行有能见度 View.GONE
。
在 onBindViewHolder
再次调用绑定一个新行,该行的观点仍然 View.GONE
- 没有被绑定行之间重置
因此,你的if语句应该完全复位状态:
如果(信息!= NULL){
viewHolder.txt.setText(信息);
viewHolder.txt.setVisibility(View.VISIBLE);
}其他{
viewHolder.txt.setVisibility(View.GONE);
}
这将保证每一行的可见性设置是否正确。
I have a RecyclerView
that I am using. I have used RecyclerView
before but never had this problem.
When I scroll up and down some of the items disappear, and some of the items that disappear appear again in the bottom.
Code:
ViewHolder
:
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txt;
public ViewHolder(View view) {
super(view);
txt = (TextView) view.findViewById(R.id.txt);
}
}
Adapter
:
public class MyAdapter extends RecyclerView.Adapter<ViewHolder> {
private final Activity activity;
private final ArrayList<HashMap<String, String>> mItems;
public MyAdapter (Activity activity, ArrayList<HashMap<String, String>> mItems) {
this.activity = activity;
this.mItems= mItems;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(activity).inflate(R.layout.items, viewGroup, false));
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
HashMap<String, String> item = mItems.get(position);
String info = item.get("info ");
if (info!= null) {
viewHolder.txt.setText(info);
} else {
viewHolder.txt.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return (null != mItems? mItems.size() : 0);
}
}
onBindViewHolder
reuses Views so let's say the first time onBindViewHolder()
is called, info
is null
. This will cause that row to have visibility of View.GONE
.
When onBindViewHolder
is called again to bind a new row, the view for that row is still View.GONE
- nothing is reset between rows being bound.
Therefore your if statement should reset the state completely:
if (info!= null) {
viewHolder.txt.setText(info);
viewHolder.txt.setVisibility(View.VISIBLE);
} else {
viewHolder.txt.setVisibility(View.GONE);
}
This will ensure that each row's visibility is set correctly.
这篇关于Android的RecyclerView,回收工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!