简体   繁体   English

Recycler View 在滚动时不使用提供的布尔值一致地更新视图

[英]Recycler View not updating views consistently using provided booleans when scrolling

I have a dataset I use to create a recycler view and the elements in it have a "solved" Boolean value.我有一个用于创建回收器视图的数据集,其中的元素具有“已解决”布尔值。 When the recyclerview is initialized everything displays correctly.当 recyclerview 被初始化时,一切都会正确显示。 When I scroll through slowly for the most part the corresponding image is displayed correctly but a few of the images blink in and out of view.当我在大部分情况下缓慢滚动时,相应的图像显示正确,但有一些图像在视图中闪烁。 If I scroll very fast then everything essentially gets messed up.如果我滚动得非常快,那么一切基本上都会变得一团糟。

private class RegularCrimeHolder extends CrimeHolder implements View.OnClickListener{
    private Crime mCrime;
    private TextView mTitleTextView;
    private TextView mDateTextView;
    public RegularCrimeHolder(LayoutInflater inflater, ViewGroup parent) {

        super(inflater.inflate(R.layout.list_item_crime, parent, false));
        mTitleTextView = this.itemView.findViewById(R.id.crime_title);
        mDateTextView = this.itemView.findViewById(R.id.crime_date);
        mSolved = this.itemView.findViewById(R.id.solved);

        itemView.setOnClickListener(this);
    }


    //Optional
    public void bind(Crime crime){
        mCrime = crime;
        mTitleTextView.setText(mCrime.getTitle());
        mDateTextView.setText(mCrime.getDate().toString());
        mSolved.setVisibility(crime.isSolved()? View.VISIBLE : View.GONE);
    }


    @Override
    public void onClick(View view) {
        //Toast toast = Toast.makeText(getActivity(), mCrime.getTitle() + " clicked!", Toast.LENGTH_SHORT);
        //toast.show();

        Intent intent = CrimeViewPagerActivity.newIntent(mCrime.getID(), getContext(), CrimeLab.GetIndex(mCrime));
        startActivityForResult(intent, REQUEST_CRIME);
    }
}

private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder>{

    private List<Crime> mCrimes;

    public CrimeAdapter(List<Crime> crimes){
        mCrimes = crimes;
        this.setHasStableIds(true);
    }

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

    @Override
    public CrimeHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        if(viewType == 0)
            return new RegularCrimeHolder(layoutInflater, viewGroup);
        else
            return new BadCrimeHolder(layoutInflater, viewGroup);
    }

    @Override
    public int getItemViewType(int position) {
        if(mCrimes.get(position).RequiresPolice())
            return 1;
        else
            return 0;
    }

    @Override
    public void onBindViewHolder(@NonNull CrimeHolder crimeHolder, int i) {
        Crime crime = mCrimes.get(i);
        crimeHolder.bind(crime);
    }

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

}

Don't worry!别担心! Cool!凉爽的! In your adapter, just do the below things在您的适配器中,只需执行以下操作

Kotlin科特林

 override fun getItemId(position: Int): Long {
    return position.toLong()
  }
override fun getItemViewType(position: Int): Int {
    return position
}

Java爪哇

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

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

Your sample code lacks detail.您的示例代码缺乏细节。 Post the full adapter viewholder and item layout code.发布完整的适配器视图和项目布局代码。

But if I had to guess here, your layout is messed up and your onViewRecycled isn't cleaning up correctly.但是,如果我不得不在这里猜测,您的布局就一团糟,并且您的 onViewRecycled 没有正确清理。

For example, say a viewholder was just used and the visibility was set to visible.例如,假设刚刚使用了一个查看器,并且可见性设置为可见。 Now when this viewholder is recycled and re bound, what you'll see is a flicker.现在,当这个 viewholder 被回收和重新绑定时,你会看到一个闪烁。 Because the view in question is visible from that last bind and it needs to hide itself in this bind.因为有问题的视图在最后一个绑定中是可见的,它需要在这个绑定中隐藏自己。

What you want to do is inside your onViewRecycled, you want to set the visibility set to gone so that when viewholders are reused, there are no flickers.您想要做的是在您的 onViewRecycled 中,您想要将可见性设置为消失,以便在重用视图时不会出现闪烁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM