2017-06-16 62 views
1

我有一個基於RealmRecyclerViewAdapter的recyclerviewAdapter。RecyclerView在滾動後選擇錯誤的項目onClick

public class PlaceAdapter extends RealmRecyclerViewAdapter<Place, PlaceAdapter.PlaceViewHolder> implements Filterable { 

private static final String TAG = PlaceAdapter.class.getSimpleName(); 

static class PlaceViewHolder extends RecyclerView.ViewHolder { 

    TextView name; 

    PlaceViewHolder(View itemView) { 
     super(itemView); 
     name = (TextView) itemView.findViewById(R.id.name); 
    } 
} 

private Realm realm; 
private static RecyclerViewClickListener itemListener; 
private TextView lastTextView; 

public PlaceAdapter(@NonNull Context context, @Nullable OrderedRealmCollection data, boolean autoUpdate, RecyclerViewClickListener listener, 
        Realm realm) { 
    super(context, data, autoUpdate); 
    this.realm = realm; 
    itemListener = listener; 
} 

@Override 
public PlaceViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    View itemView = LayoutInflater.from(viewGroup.getContext()). 
      inflate(R.layout.place_item_layout, viewGroup, false); 
    return new PlaceViewHolder(itemView); 
} 

@Override 
public void onBindViewHolder(final PlaceViewHolder holder, final int position) { 
    final Place place = getData().get(position); 
    holder.name.setText(place.getName()); 

    holder.itemView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      itemListener.recyclerViewListClicked(place.getId()); 
      holder.name.setTypeface(Typeface.create(holder.name.getTypeface(), Typeface.BOLD), Typeface.BOLD); 

      if (lastTextView != null && lastTextView != holder.name) { 
       lastTextView.setTypeface(Typeface.create(lastTextView.getTypeface(), Typeface.NORMAL), Typeface.NORMAL); 
      } 
      notifyItemRangeChanged(0, getItemCount()); 

      lastTextView = holder.name; 
     } 
    }); 
} 

@Override 
public int getItemCount() { 
    return getData().size(); 
} 

@Override 
public Filter getFilter() { 
    final Filter filter = new Filter() { 

     @SuppressWarnings("unchecked") 
     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      String text = constraint.toString(); 
      if (text == null || "".equals(text)) { 
       updateData(realm.where(Place.class).findAll()); 
      } else { 
       updateData(realm.where(Place.class) 
         .contains("name", text, Case.INSENSITIVE) // TODO: change field 
         .findAll()); 
      } 
     } 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 

      FilterResults results = new FilterResults(); 
      return results; 
     } 
    }; 

    return filter; 
} 

public interface RecyclerViewClickListener { 

    void recyclerViewListClicked(int placeId); 
} 

}

的問題是,當我滾動回收視圖,使頂部的人是不可見的,並且我點擊視圖,它上面的觀點得到加粗字體。只有當我第二次點擊它時,我的正確視圖纔會變成粗體字體。

+2

我想你忘了在適配器中覆蓋這些方法: public long getItemId(int position){ return position; } public int getItemViewType(int position){ return position; } –

+0

工作就像一個魅力,謝謝。我不知道爲什麼這些方法必須被覆蓋 – Traabefi

+0

前段時間我面臨同樣的問題,我浪費了2小時來找到 –

回答

1

我想你忘了適配器來覆蓋這些方法:

@Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     return position; 
    } 

我不知道爲什麼發生這種情況,但它是Recyclerview搞亂問題,當滾動正確的解決方案。

+0

這使得RecyclerView可以創建與項目一樣多的視圖。 – EpicPandaForce

+0

是的。多視圖類型和使用getItemViewType創建的分頁 –

相關問題