Java tutorial
/** * Copyright 2015-2016 Deepansh Jain * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.dj.hacktor.nshiddenlayout; import android.content.res.Configuration; import android.os.Bundle; import android.support.design.widget.CollapsingToolbarLayout; import android.support.design.widget.NavigationView; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import com.dj.hacktor.nshiddenlayout.views.WrapContentHeightViewPager; /** * @author DeepanshJain * This is simple base calss of actvity which use to display posts and photos * MainActivity#NavigationView use for dispalying navigation drawer menu */ public class MainActivity extends AppCompatActivity { private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle toggle; private TabLayout tabLayout; private SampleAdapter sampleAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar ab = getSupportActionBar(); ab.setDisplayHomeAsUpEnabled(true); CollapsingToolbarLayout toolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbarCollapse); toolbarLayout.setTitle(getString(R.string.app_name)); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setBackgroundColor(getResources().getColor(android.R.color.white)); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer); mDrawerLayout.setDrawerListener(toggle); setupDrawerContent(navigationView); //By default add post fragment tabLayout = (TabLayout) findViewById(R.id.tabLayout); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); sampleAdapter = new SampleAdapter(getSupportFragmentManager()); WrapContentHeightViewPager mPager = (WrapContentHeightViewPager) findViewById(R.id.viewPager); mPager.setAdapter(sampleAdapter); tabLayout.setupWithViewPager(mPager); } private void setupDrawerContent(NavigationView navigationView) { navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { mDrawerLayout.closeDrawers(); menuItem.setChecked(true); getSupportActionBar().setTitle(menuItem.getTitle()); return true; } }); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); toggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); toggle.onConfigurationChanged(newConfig); } public static class SampleFragment extends android.support.v4.app.Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment, container, false); } } /** * FragmentPagerAdapter for WrapContentHeightViewPager */ public class SampleAdapter extends FragmentPagerAdapter { public SampleAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return new SampleFragment(); } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { return "Page " + (position + 1); } } }