Recyclerview fast scroll items disappear

前提是你 提交于 2019-12-10 18:47:03

问题


I am using GridLayoutManager with recyclerview, when i fast scroll down one item out of four visible grid items (bottom right ) is moved further down,

I am using this tutorial to implement fast scroll and fast scroll indicator here


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/33785845/recyclerview-fast-scroll-items-disappear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!