简体   繁体   English

让Android RecyclerView更新React Native组件中的视图

[英]Getting Android RecyclerView to update view inside React Native component

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. 我正在使用React Native创建一个移动应用程序,并且包含列表组件的性能不够高,所以我开始使用Android的RecyclerView作为列表组件。 There is a problem though with it. 但是有一个问题。 The RecyclerView doesn't update its contents views until I scroll or change RecyclerView's size. 在我滚动或更改RecyclerView的大小之前,RecyclerView不会更新其内容视图。 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. 我尝试过notifyDatasetChanged,notifyItemChanged,forceLayout,invalidate,postInvalidate以及各自的许多不同变体。

在此输入图像描述 Try this one this.setIsRecyclable(true); 试试这个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();
            }
        }
    }

}

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

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