简体   繁体   中英

Recyclerview fast scroll items disappear

I am using GridLayoutManager with recyclerview, when i fast 在此处输入图像描述<\/a> scroll down one item out of four visible grid items (bottom right ) is moved further down,

Guess i've found the solution.

Looks like when the Recycled Views are attached to the Window again, their state is changed to Invisible (IDK why). All we have to do is make it visible again.

Fortunately, RecyclerView.Adapter class has a method to handle when a view is attached or reattached to the RecyclerView.

    @Override
    public void onViewAttachedToWindow(ViewHolder holder){
        super.onViewAttachedToWindow(holder);
        if(!holder.tv_1.getText().toString().contentEquals("#EMPTYVIEW"))
            holder.root.setVisibility(View.VISIBLE);

        Log.i("ATTACHED_VISIBILITY",holder.tv_1.getText().toString()+"\t"+holder.root.getVisibility());
    }

My ViewHolder class

class ViewHolder extends RecyclerView.ViewHolder{
    TextView tv_1;
    TextView tv_2;
    ImageView iv;
    ImageButton bt;

    View root;

    ViewHolder(View v){
        super(v);

        //setIsRecyclable(false);     //to prevent views from getting deleted. :-(

        tv_1=(TextView)v.findViewById(R.id.aral_tv_title);
        tv_2=(TextView)v.findViewById(R.id.aral_tv_stitle);
        iv=(ImageView)v.findViewById(R.id.aral_iv);
        bt=(ImageButton)v.findViewById(R.id.aral_bt_more);

        tv_1.setTypeface(WorkActivity.mainTF);
        tv_2.setTypeface(WorkActivity.mainTF);

        root=v;
    }
}

Hope this helps you out.

For every if there should be else .

For example: If you have a view which is visible in XML file by default but you are changing its visibility in code, you must supply the else otherwise RecyclerView will re-use the views and will show re-used views with different data.

if(somethingIsTrue) {
 view.setVisibility(View.GONE);
} else {
 view.setVisibility(View.VISIBLE);
}

检查回收站项目的布局

android:layout_height="wrap_content"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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