Android TabLayout带有两个选项卡,每个回收站视图在切换时滚动到顶部

时间:2016-08-17 07:59:50

标签: android android-fragments android-recyclerview

我有一个带TabLayout的Activity和一个包含两个标签的Viewpager。这些选项卡是一个重用的片段,其中包含Recyclerview。所以每个片段/标签都包含一个带有一堆项目的回收者视图。

在这些选项卡之间切换时,当我返回上一个选项卡时,不会保留滚动状态。

我错过了什么?

的活动:

public class EventsActivity extends AbstractTracActivity implements OnRowClickListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.tabs)
    TabLayout tabLayout;

    @BindView(R.id.viewpager)
    ViewPager viewPager;

    private final String TAG = EventsActivity.class.getSimpleName();

    private static Context context;
    private SearchView searchView;
    private static EventListFragment eventsFragment, clubsFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.TracTracAppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.events);
        ButterKnife.bind(this);

        context = this;

        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setLogo(R.drawable.tractrac_logo);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }

        eventsFragment = EventListFragment.newInstance(EventListFragment.ListType.EVENTS);
        clubsFragment = EventListFragment.newInstance(EventListFragment.ListType.CLUBS);

        TabLayout.Tab events = tabLayout.newTab();
        TabLayout.Tab clubs = tabLayout.newTab();

        tabLayout.addTab(events, 0);
        tabLayout.addTab(clubs, 1);

        tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.accent));
        tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.accent));

        ViewPagerAdapter adapter = new ViewPagerAdapter (getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);

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

            @Override
            public void onPageSelected(int position) { }

            @Override
            public void onPageScrollStateChanged(int state) {
                searchView.setQuery("", true);
                searchView.clearFocus();
                searchView.setIconified(true);
                eventsFragment.filterList(null);
                clubsFragment.filterList(null);
            }
        });
    }

    ...

    public static class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 1:
                    return clubsFragment;
                default:
                    return eventsFragment;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 1:
                    return context.getResources().getString(R.string.section_clubs);
                default:
                    return context.getResources().getString(R.string.section_events);
            }
        }
    }
}

片段:

public class EventListFragment extends Fragment implements EventsFragmentPresenter.ViewInterface {

    enum ListType { EVENTS, CLUBS }

    private EventsFragmentPresenter presenter;
    private ListType listType;
    private List<Event> events;
    private RecyclerView recyclerView;

    public static EventListFragment newInstance(ListType listType) {
        EventListFragment f = new EventListFragment();
        Bundle args = new Bundle();
        args.putSerializable("listType", listType);
        f.setArguments(args);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(com.tractrac.androidlib.R.layout.base_list_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        recyclerView = (RecyclerView) view.findViewById(com.tractrac.androidlib.R.id.recycler_view);

        switch (getListType()) {
            case EVENTS:
                getPresenter().getEvents();
                break;
            case CLUBS:
                getPresenter().getClubEvents();
                break;
        }
    }

    private EventsFragmentPresenter getPresenter() {
        if (presenter == null) {
            presenter = new EventsFragmentPresenter();
            presenter.initialize(this);
        }
        return presenter;
    }

    private ListType getListType() {
        if (listType == null) {
            listType = (ListType) getArguments().getSerializable("listType");
        }
        return listType;
    }

    @Override
    public void listReady(List<Event> events) {
        EventsRecyclerAdapter adapter = new EventsRecyclerAdapter(events, (EventsActivity) getActivity());
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);

        if (getListType() == ListType.EVENTS) {
            mLayoutManager.scrollToPosition(getPresenter().getCurrentDayEventIndex(events));
        }
        this.events = events;
        ((EventsActivity) getActivity()).hideSpinner();
    }

    public void filterList(String filterBy) {
        if (TextUtils.isEmpty(filterBy)) {
            EventsRecyclerAdapter adapter = new EventsRecyclerAdapter(events, (EventsActivity) getActivity());
            recyclerView.setAdapter(adapter);
            return;
        }

        List<Event> filteredEvents = new ArrayList<>();
        for (Event event : events) {
            if (event.getName().toLowerCase().contains(filterBy) || event.getLocation().toLowerCase().contains(filterBy))
                filteredEvents.add(event);
        }
        EventsRecyclerAdapter adapter = new EventsRecyclerAdapter(filteredEvents, (EventsActivity) getActivity());
        recyclerView.setAdapter(adapter);
    }
}

1 个答案:

答案 0 :(得分:0)

onPageScrollStateChanged中,您正在调用filterList每次创建新EventsRecyclerAdapter的地方,这是重置滚动状态的原因。

您应该将适配器设置为RecyclerView,然后再调用notify方法。