让 Android RecyclerView 更新 React Native 组件内的视图 [英] Getting Android RecyclerView to update view inside React Native component

查看:16
本文介绍了让 Android RecyclerView 更新 React Native 组件内的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 React Native 制作一个移动应用程序,但包含的列表组件的性能不够高,所以我开始使用 Android 的 RecyclerView 作为列表组件.虽然它有问题.在我滚动或更改 RecyclerView 的大小之前,RecyclerView 不会更新其内容视图.什么可能导致这个问题,我该如何解决?我已经尝试过 notifyDatasetChanged、notifyItemChanged、forceLayout、invalidate、postInvalidate 和许多不同的变化.

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.

推荐答案

试试这个 this.setIsRecyclable(true);

它会参考你的观点

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

}

这篇关于让 Android RecyclerView 更新 React Native 组件内的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆