RecylerView 不刷新 android studio 中的 onRefresh

问题描述

我正在尝试更新 recylerview 中的 onRefresh 项目,但它没有生成所需的列表。当我关闭应用程序并重新打开它时,它会提供更新的项目列表,但它不会通过 onRefresh 方法更新项目列表。所以基本上要更新列表,您必须关闭并打开应用程序,并且不能以其他方式更新它。

 swipeRefreshLayout.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
               
                myAdapter = new MyAdapter(getContext(),items);
                recyclerView.setAdapter(myAdapter);
                recyclerView.setLayoutManager(new linearlayoutmanager(getContext(),RecyclerView.VERTICAL,false));
                //myAdapter.notifyDataSetChanged();
                recyclerView.invalidate();
                swipeRefreshLayout.setRefreshing(false);

我已经尝试了 notifyDataSetChanged() 和 invalidate() 但它们都不起作用。 logcat中也没有错误。如何更改方法以在刷新时更新它?

我在 onCreateView 方法中有相同的适配器设置

 public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
  myAdapter = new MyAdapter(getContext(),videoFiles);
    recyclerView.setAdapter(myAdapter);
    recyclerView.setLayoutManager(new linearlayoutmanager(getContext(),false));
}

这是使用 recylerview 的完整活动

public class FilesFragment extends Fragment {
    RecyclerView recyclerView;
    View view;
    MyAdapter myAdapter;
     //     public static List<Files> items; from adapter class
    public FilesFragment() {
        // required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
     
        view = inflater.inflate(R.layout.fragment_files,container,false);
        recyclerView = view.findViewById(R.id.filesRV);
       
        myAdapter = new MyAdapter(getContext(),items);
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new linearlayoutmanager(getContext(),false));
       // recyclerView.invalidate();
        swipeRefreshLayout = view.findViewById(R.id.swipetoRefresh);
        swipeRefreshLayout.setonRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
           
                
                // recyclerView.invalidate();
                myAdapter.notifyDataSetChanged();
               // updateData(items);
                swipeRefreshLayout.setRefreshing(false);
            }

            public void updateData(List<Files> items) {
               // items.clear();
                myAdapter = new MyAdapter(getContext(),false));
                items.addAll(items);
                myAdapter.notifyDataSetChanged();
            }
        });

      
        if (items!= null && items.size() > 0) {
            myAdapter = new MyAdapter(getContext(),items);
            recyclerView.setAdapter(myAdapter);
            recyclerView.setLayoutManager(new linearlayoutmanager(getContext(),false));
        }
        return view;
    }

解决方法

无需每次都设置新的适配器。只需调用通知数据集更改如下:

recyclerView.getAdapter().notifyDataSetChanged();
    

在此之前,请确保您的商品已更新。我的猜测是您没有插入或更新项目。如果它是一个数组列表,请在调用 notifyDataSetChanged()

之前插入或更新一个项目 ,
myAdapter.notifyDataSetChanged();

添加此行以通知适配器数据源已更改 把它放在 onResume() 中以使其无缝

您也可以尝试以下操作

recyclerView.setAdapter(null);
recyclerView.setLayoutManager(null);
recyclerView.setAdapter(myAdapter);
recyclerView.setLayoutManager(myLayoutManager);
myAdapter.notifyDataSetChanged();

这将使用新的更新数据使回收站视图无效

尽管存在无效方法,但在某些情况下往往不起作用

   public class FilesFragment extends Fragment {
    RecyclerView recyclerView;
    View view;
    MyAdapter myAdapter;
     //     public static List<Files> items; from adapter class
    public FilesFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
     
        view = inflater.inflate(R.layout.fragment_files,container,false);
        recyclerView = view.findViewById(R.id.filesRV);
       
        myAdapter = new MyAdapter(getContext(),items);
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),RecyclerView.VERTICAL,false));
       // recyclerView.invalidate();
        swipeRefreshLayout = view.findViewById(R.id.swipeToRefresh);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
           
                
                // recyclerView.invalidate();
               // myAdapter.notifyDataSetChanged();
                updateData(items);
                swipeRefreshLayout.setRefreshing(false);
            }

            
        });

      public void updateData(List<Files> items) {
               // items.clear();
                myAdapter = new MyAdapter(getContext(),items);
                recyclerView.setAdapter(myAdapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),false));
                items.addAll(items);
                myAdapter.notifyDataSetChanged();
               recyclerview.getAdapter.notifydatasetchanged();
            }
       
        return view;
    }