gpt4 book ai didi

android - RecyclerView 在滚动后选择错误的项目 onClick

转载 作者:太空狗 更新时间:2023-10-29 16:28:10 25 4
gpt4 key购买 nike

我有一个基于 RealmRecyclerViewAdapter 的 recyclerviewAdapter。

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);
}

问题是,当我滚动回收器 View 时,顶部的那些不可见,我单击该 View ,它上方的 View 显示为粗体。只有当我第二次点击它时,我的正确 View 才会变成粗体。

最佳答案

我想你忘了在适配器中覆盖这些方法:

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

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

我不确定为什么会发生这种情况,但它是滚动时 Recyclerview 困惑问题的正确解决方案。

关于android - RecyclerView 在滚动后选择错误的项目 onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44589989/

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