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

时间:2017-09-07 12:03:44

标签: android react-native android-recyclerview react-native-android

我正在使用React Native创建一个移动应用程序,并且包含列表组件的性能不够高,所以我开始使用Android的RecyclerView作为列表组件。但是有一个问题。在我滚动或更改RecyclerView的大小之前,RecyclerView不会更新其内容视图。什么可能导致这个问题,以及我如何解决它?我尝试过notifyDatasetChanged,notifyItemChanged,forceLayout,invalidate,postInvalidate以及每种变体的许多不同变体。

1 个答案:

答案 0 :(得分:1)

enter image description here试试这个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();
            }
        }
    }

}