RecyclerView将数据加载到错误的位置,同时选择选项卡,而滑动加载正确的数据

时间:2018-08-13 20:23:41

标签: android-recyclerview android-viewpager android-tablayout

我创建了一个带有选项卡布局的viewpager,它使用相同的recyclerview布局从数据库中以不同的片段加载不同类型的数据。问题是,当我滑动屏幕时,recyclerview会根据位置正确加载数据。但是在选择选项卡时,recyclerview不会根据选项卡位置加载数据。下面是oncreate方法:   super.onCreate(savedInstanceState);         setContentView(R.layout.activity_tasks);

    Toolbar toolbar =  findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    tabLayout = findViewById(R.id.tabs);
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            tabLayout.getTabAt(position).select();

            String AlarmType;
            switch (position){
                case 0: AlarmType="Dosing";
                    break;
                case 1: AlarmType="Maintenance";
                    break;
                case 2: AlarmType="Dosing";
                    break;
                case 3: AlarmType="Water Change";
                    break;
                default:AlarmType="";
            }
            System.out.println("Alarm Type "+ AlarmType);

            PageSelected(AlarmType);


        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

回收站视图:

 recyclerView= rootView.findViewById(R.id.menuRecyclerView);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        taskItems=new ArrayList <>();


        Bundle bundle=getIntent().getExtras();

        final String aquaID= bundle.getString("AquariumID");




        mydbHelper mydbhelper=new mydbHelper(this,aquaID);


        SQLiteDatabase db=mydbhelper.getWritableDatabase();
        Cursor c=mydbhelper.getDataANCondition(db,"Category",AlarmType);


        if(c.moveToFirst()) {
            do {
                taskItems.add(c.getString(0));
            } while (c.moveToNext());

        }
        c.close();

        for(int i=0;i<taskItems.size();i++)
            System.out.println(taskItems.get(i));

        adapter=new RecyclerAdapter(taskItems,aquaID);

        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

0 个答案:

没有答案