我的RecyclerView不更新(有时)

时间:2019-07-27 20:13:37

标签: java android android-recyclerview

我有一个选项卡式活动。每个片段中都有一个RecyclerView。 如果滚动到其他片段然后再返回,我的RecyclerView将不会更新。如果我只是启动该应用程序,它将起作用。为了更新我的RecyclerView,我更改了例如Driffrent活动中的名称,并在一个不同的类中调用了一个方法来更新我的RecyclerView,这有时仅起作用。

当您退出活动以更改数据并返回到片段时,我还尝试更新整个片段(在onResume()中)。

我发现问题只有在再次调用该片段的onCreateView时才会出现。

这是我的Fragment类:

public static class DayFragment extends Fragment {

        private static final String SECTION_NUMBER = "section_number";

        public DayFragment() {

        }

        public static DayFragment newInstance(int sectionNumber) {
            DayFragment fragment = new DayFragment();
            Bundle args = new Bundle();
            args.putInt(SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            Day day = new Day(getArguments().getInt(SECTION_NUMBER, 0) - 1, rootView, getContext(), getContext().getFilesDir());
            MainActivity.DAYS.add(day);
            day.createList();

            System.out.println("CREATED" + (getArguments().getInt(SECTION_NUMBER, 0) - 1));

            return rootView;
        }

    }

设置列表:

public void createList() {
        dayList = day.findViewById(R.id.dayList); // day is the rootView of the fragment passed on to this object of a class
        lessonsAdapter = new LessonListAdapter();
        dayList.setAdapter(lessonsAdapter);
        dayList.setLayoutManager(new LinearLayoutManager(context) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        dayList.addItemDecoration(new MarginItemDecoration(context));
    }

更新RecyclerView(在普通的Java类中):

public void update() {
        data.clear();
        //if(lessonsAdapter != null) {
        //    lessonsAdapter.notifyDataSetChanged();
        //}
        data.addAll(newData);
        lessonsAdapter.notifyDataSetChanged();
}

所以请告诉我我需要做什么,以便我的RecyclerView每次都更新。

4 个答案:

答案 0 :(得分:0)

实施ViewPager.OnPageChangeListener,然后更新

中的片段
public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    ... 
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        ...
        viewPager.addOnPageChangeListener(this);
        ...
        ...
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    @Override
    public void onPageSelected(int position) {
        Fragment fragment = adapter.getFragment(position);
        if (fragment != null) {
            // Update fragment here
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {}
}

答案 1 :(得分:0)

每次更改片段时,都必须重新设置适配器,您可以在onResume上进行操作。尝试这样的事情:

    private void initAdapter(){
    lessonsAdapter = new LessonListAdapter();
    dayList.setAdapter(lessonsAdapter);
    dayList.setLayoutManager(new LinearLayoutManager(context) {
        @Override
        public boolean canScrollVertically() {
            return false;
        }
    });
    dayList.addItemDecoration(new MarginItemDecoration(context));
    }

    @Override
    public void onResume() { initAdapter(); }

答案 2 :(得分:0)

使用侯赛因答案,并使用此代码将片段放入onPageSelected方法中

Fragment fragment = (Fragment) adaper.instantiateItem(mViewPager, position);

答案 3 :(得分:0)

S,我找到了解决方案,当然我的问题是在其他地方。 非常抱歉浪费您的时间,并感谢您的努力!

问题在于,在调用onCreateView时,我将新的Day对象添加到列表中而没有删除旧的对象,因此根本不会显示新的更新数据。

相关问题