Search in sources :

Example 1 with TabLayout

use of com.commonsware.cwac.crossport.design.widget.TabLayout in project cw-omnibus by commonsguy.

the class MainActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final RecyclerView pager = (RecyclerView) findViewById(R.id.pager);
    final LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    pager.setLayoutManager(layoutManager);
    snapperCarr.attachToRecyclerView(pager);
    adapter = new PageAdapter(pager, getLayoutInflater());
    pager.setAdapter(adapter);
    final TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
    for (int i = 0; i < adapter.getItemCount(); i++) {
        tabs.addTab(tabs.newTab().setText(adapter.getTabText(this, i)));
    }
    tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            pager.smoothScrollToPosition(tab.getPosition());
        }

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

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        // unused
        }
    });
    pager.setOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            int tab = layoutManager.findFirstCompletelyVisibleItemPosition();
            if (tab >= 0 && tab < tabs.getTabCount()) {
                tabs.getTabAt(tab).select();
            }
        }
    });
}
Also used : TabLayout(com.commonsware.cwac.crossport.design.widget.TabLayout) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager)

Aggregations

LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 TabLayout (com.commonsware.cwac.crossport.design.widget.TabLayout)1