安卓design的CoordinatorLayout+TabLayout隐藏Toolbar之踩过的坑

首先XML布局


    


    
    

        
        

        

    

    


Activity代码

public class SecondActivity extends AppCompatActivity {
    private TabLayout mTabLayout;
    private ViewPager viewPager;
    String[] mTitleList = new String[]{"日报","主题","专栏","热门"};
    List fragments = new ArrayList();
    ZhihuMainAdapter mAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ActionBar ab = getSupportActionBar();
//        ab.setHomeAsUpIndicator(R.mipmap.ic_brightness_medium_white_36dp);
        ab.setDisplayHomeAsUpEnabled(true);
        viewPager = (ViewPager) this.findViewById(R.id.viewpager);
        mTabLayout= (TabLayout) findViewById(R.id.tabs);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);//设置tab模式,当前为系统默认模式

        fragments.add(new MyFragmrnt());
        fragments.add(new MyFragmrnt());
        fragments.add(new MyFragmrnt());
        fragments.add(new MyFragmrnt());
        mAdapter = new ZhihuMainAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(mAdapter);
        
        //此处遇过一个坑,就是用你AS版本对应的 compile 'com.android.support:design:25.0.1'依赖,我用的AS版本为2.2.2,否则报错
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList[0]));//添加tab选项卡
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList[1]));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList[2]));
        mTabLayout.addTab(mTabLayout.newTab().setText(mTitleList[3]));
        mTabLayout.setupWithViewPager(viewPager);
        mTabLayout.getTabAt(0).setText(mTitleList[0]);
        mTabLayout.getTabAt(1).setText(mTitleList[1]);
        mTabLayout.getTabAt(2).setText(mTitleList[2]);
        mTabLayout.getTabAt(3).setText(mTitleList[3]);
        
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_tab_layout, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        switch (id){
            case android.R.id.home:
                this.finish();
                return true;
            case R.id.action_settings:

                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

Fragment代码

public class MyFragmrnt extends Fragment {
    private ListView listView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment,null);
        RecyclerView mRecyclerView= (RecyclerView) view.findViewById(R.id.LV);//请用RecyclerView用ListView实现不了隐藏 
        mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(new RecyclerViewAdapter(getActivity()));
        return view;
    }
}


你可能感兴趣的:(安卓)