编程语言
首页 > 编程语言> > java-更新recyclerView适配器而不滚动到顶部

java-更新recyclerView适配器而不滚动到顶部

作者:互联网

我尝试在添加一些数据后更新适配器,但没有更新,当我切换其他活动并回来时,适配器这次已更新了我的代码:

    void updateAdapter(){
    adapter.setData(getData());
    adapter.notifyDataSetChanged();
    Log.d("TAG"," DATA SIZE AFTER UPDATE : "+getData().size() +" items : "+adapter.getItemCount());

}

我的适配器:

    private void createAdapter(final RecyclerView recyclerView , final List<Information> data) {
    List<Information> content = new ArrayList<>();

    Log.d("TAG","createAdapter Called");

    final int[] currentRowPosition = {0};
    content = data;
    final List<Information> finalContent = content;
     adapter = new ParallaxRecyclerAdapter<Information>(finalContent) {

/// my onBindViewHolder
        @Override
        public void onBindViewHolderImpl(final RecyclerView.ViewHolder viewHolder, final ParallaxRecyclerAdapter<Information> adapter, final int i) {


            currentRowPosition[0] = i;

            if ( i <= finalContent.size() -1 ){

                ((ViewHolder) viewHolder).linearLayout.setVisibility(View.GONE);

            final Information current = finalContent.get(i);

            ((ViewHolder) viewHolder).DESCRIPTION.setText(current.description);

        }else {

                 // this is my loadMore button which stays at the last row of recyclerView 
                ((ViewHolder) viewHolder).setIsRecyclable(false);
                ((ViewHolder) viewHolder).linearLayout.setVisibility(View.VISIBLE);
                ((ViewHolder) viewHolder).itemView.getLayoutParams().height = 100;


                ((ViewHolder) viewHolder).LOADMORE.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        fetchNext();
                    }
                });

            }

        }
//my on create View Holder
        @Override
        public RecyclerView.ViewHolder onCreateViewHolderImpl(ViewGroup viewGroup, final ParallaxRecyclerAdapter<Information> adapter, int i) {

            return  new ViewHolder(getActivity().getLayoutInflater().inflate(R.layout.custom_row, viewGroup, false));

        }

        @Override
        public int getItemCountImpl(ParallaxRecyclerAdapter<Information> adapter) {
            return finalContent.size()+1;
        }
    });
// adding header to recyclerView  
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    View header = getActivity().getLayoutInflater().inflate(R.layout.header, recyclerView, false);

    headerImageView =  (ImageView) header.findViewById(R.id.headerImageView);
    //setting CoverImage

    adapter.setParallaxHeader(header, recyclerView);

    adapter.setData(content);
    recyclerView.setAdapter(adapter);

}

任何想法我如何在RecyclerView上实时更新添加新项目?任何指导都会对我有帮助

解决方法:

我假设当您第一次运行Activity(具有RecyclerView)时,它将正确地从适配器加载所有内容.

但是,当您更改为其他活动时,会更改适配器的项目,然后返回到其中包含RecyclerView的Activity,因为适配器未更新,因此无法获取正确的更新信息.

这可能是因为您的活动在onResume()中恢复时没有刷新适配器,类似这样.您可能需要根据需要对onResume方法进行其他更改,

onResume(){
  if(adapter != null){
     adapter.notifyDataSetChanged();
   }
}

请检查活动的生命周期

enter image description here

例:
这不会直接解决您的问题,但是可以帮助您理解,当您重新开始活动时应该刷新适配器.

有一个NoteListActivity,其中带有ListView,您可以在NoteEditActivity中向其中的适配器添加或编辑项,一旦回来,就必须在具有列表视图的活动中刷新适配器

标签:java,android,android-adapter
来源: https://codeday.me/bug/20191026/1939978.html