11

I am making a mobile application using React Native and included list components didn't have high enough performance for it so I started using Android's RecyclerView as the list component. There is a problem though with it. The RecyclerView doesn't update its contents views until I scroll or change RecyclerView's size. What could cause this problem and how I can fix it? I have tried notifyDatasetChanged, notifyItemChanged, forceLayout, invalidate, postInvalidate and many different variations with each.

Vaintti
  • 166
  • 8
  • Isn't flatlist satisfying your requirements, and have you seen this page https://facebook.github.io/react-native/docs/virtualizedlist.html#virtualizedlist ? Flatlist inherits from virtualizedlist, try making your `data` items PureComponent – eden Sep 17 '17 at 19:36
  • I have tried to use flatlist but its performance isn't good enough for my purpose. It gets quite slow when used with data of thousands of items. – Vaintti Sep 18 '17 at 06:25
  • Have you tried making your list items Pure Component? – eden Sep 18 '17 at 07:07
  • Yes I have. It didn't help enough. – Vaintti Sep 21 '17 at 07:57
  • 1
    Did you figure this out my friend? I'm in the exact same situation! – SudoPlz Mar 19 '18 at 21:07
  • @SudoPlz I have the same problem, RecyclerView somewhere in a SimpleViewManager. It doesn't update until I scroll. Did you figure it out? – Florian Cargoet Apr 04 '18 at 16:07
  • 1
    @SudoPlz I found your solution: https://stackoverflow.com/questions/49371866/recyclerview-wont-update-child-until-i-scroll/49372697#49372697 – Florian Cargoet Apr 04 '18 at 16:46

1 Answers1

1

enter image description hereTry this one this.setIsRecyclable(true);

It will referesh your views

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private ArrayList<String> mSingleItemLists = new ArrayList<>();
    private SingleListItemAdapter mSingleListItemAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_single_item);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        setDummyData();
    }

    private void setDummyData() {
        for (int i = 0; i <= 30; i++)
            mSingleItemLists.add("item" + i);
    }


    @Override
    protected void onResume() {
        super.onResume();
        mSingleListItemAdapter = new SingleListItemAdapter(mSingleItemLists);
        mRecyclerView.setAdapter(mSingleListItemAdapter);
    }

    class SingleListItemAdapter extends RecyclerView.Adapter<SingleListItemAdapter.SingleListItemHolder> {
        private ArrayList<String> mSingleItemLists;

        private SingleListItemAdapter(ArrayList<String> singleItemLists) {
            mSingleItemLists = singleItemLists;
            //You can do notifydatasetchange if u r having any saved value 
        }

        @Override
        public SingleListItemAdapter.SingleListItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflatedView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.row_recyclerview, parent, false);
            return new SingleListItemHolder(inflatedView);
        }

        @Override
        public void onBindViewHolder(SingleListItemAdapter.SingleListItemHolder holder, int position) {
            holder.mItemDate.setText(mSingleItemLists.get(position));
        }

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

        class SingleListItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            private TextView mItemDate;

            SingleListItemHolder(View v) {
                super(v);
                mItemDate = (TextView) v.findViewById(R.id.textview_recycler_list_item);
                v.setOnClickListener(this);
                this.setIsRecyclable(true); // This will help u 
            }

            @Override
            public void onClick(View v) {
                //do your stuff
                notifyDataSetChanged();
            }
        }
    }

}
Amjad Khan
  • 1,309
  • 15
  • 32
  • I tried to add this.setIsRecycleable(true) to all of my ViewHolders but unfortunately that didn't make my views to refresh. – Vaintti Sep 21 '17 at 07:55
  • mSingleListItemAdapter = new SingleListItemAdapter(mSingleItemLists); mRecyclerView.setAdapter(mSingleListItemAdapter); Try set this again where u want to referesh – Amjad Khan Sep 21 '17 at 09:02
  • That doesn't work first time the view updates and causes jumping to beginning of the list and such. – Vaintti Sep 22 '17 at 09:10
  • I have added Image also u can check – Amjad Khan Sep 23 '17 at 09:01