Friday, September 11, 2015

Record-Review-Follow Apps Project Part 2



.

Fragments, View Pager and Tab Layout
Record-Review-Follow Apps Project Part 2
MOTIVE
This is the second part of Record-Review-Follow Apps Project.
This part adds the Fragments, View Pager and Tab Layout component to the project.
1) Kickstart
2) Check dependencies
Ensure that you have the following dependencies
File Name: build.gradle (Module:app)
dependencies {
   compile fileTree(dir'libs'include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile 'com.android.support:appcompat-v7:23.1.0'
   compile 'com.android.support:design:23.1.0'
}
3) Add View Pager and Tab Layout component to the layout file.
Delete the padding definition.
Add the components.
File Name: res/layout/content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:showIn="@layout/app_bar_main"
   tools:context=".MainActivity">
   <android.support.design.widget.TabLayout
       android:id="@+id/sliding_tabs"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="?attr/colorPrimary"
       android:elevation="6dp"
       android:minHeight="?attr/actionBarSize"
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

   <android.support.v4.view.ViewPager
       android:id="@+id/viewpager"
       android:layout_width="match_parent"
       android:layout_height="fill_parent"
       android:layout_below="@id/sliding_tabs"/>

</RelativeLayout>
3) Create Page Controllers
We will create three fragment pages for REPORT, REVIEW and FOLLOW
Create New/Fragment/Fragment(Blank)
Repeat the same steps to create Fragment2 and Fragment3.
You should get three Java (controller) files and their corresponding XML (layout) files.
Android automatically generated import for Fragment Library.
However, we need to manually change it to Support.V4 in order to construct ViewPager.
Change the library for Page1.java, Page2.java and Page3.java
Example:
File Name: Page1.java
package com.notarazi.mynavigationdrawer1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class Page1 extends Fragment {
   public Page1() {
       // Required empty public constructor
   }
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
       // Inflate the layout for this fragment
       return inflater.inflate(R.layout.fragment_page1, container, false);
   }
}
Example:
File Name: res/layout/fragment_page1.xml
(Type RECORD, REVIEW and FOLLOW at the respective TextViews to identify them)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"          
android:layout_width="match_parent"
   android:layout_height="match_parent"tools:context="com.notarazi.mynavigationdrawer1.Page1">
   <!-- TODO: Update blank fragment layout -->
   <TextView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:text="RECORD/>
</FrameLayout>
5) Create another Fragment to act as a View Pager Controller
Do no tick the options for layout, factory and interface callbacks
Again, make sure that you are using android.support.v4.app.* for your Fragment library
File Name: ViewPagerController.java
package com.notarazi.mynavigationdrawer1;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* A simple {@link Fragment} subclass.
*/
public class ViewPagerController extends FragmentPagerAdapter {
   final int PAGE_COUNT 3;
   private String tabTitles[] = new String[] { "RECORD""REVIEW""FOLLOW" };
   private Context context;
   public ViewPagerController(FragmentManager fm, Context context) {
       super(fm);
       this.context = context;
   }
   @Override
   public int getCount() {
       return PAGE_COUNT;
   }
   @Override
   public android.support.v4.app.Fragment getItem(int arg0) {
       Bundle data = new Bundle();
       switch(arg0){
           /** tab1 is selected */
           case 0:
               Page1 page1 = new Page1();
               return page1;
           /** tab2 is selected */
           case 1:
               Page2 page2 = new Page2();
               return page2;
           /** tab3 is selected */
           case 2:
               Page3 page3 = new Page3();
               return page3;
       }
       return null;
   }
   @Override
   public CharSequence getPageTitle(int position) {
       // Generate title based on item position
       return tabTitles[position];
   }
}
6) Update MainActivity.java to include calls to View Pager controller.
package com.notarazi.recordreviewfollow;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity
       implements NavigationView.OnNavigationItemSelectedListener {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);
       // Get the ViewPager and set it's PagerAdapter so that it can display items
       ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
       viewPager.setAdapter(new ViewPagerController(getSupportFragmentManager(),
               MainActivity.this));
       // Give the TabLayout the ViewPager
       TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
       tabLayout.setupWithViewPager(viewPager);
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
       fab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action"null).show();
           }
       });
       DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
       ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
               this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
       drawer.setDrawerListener(toggle);
       toggle.syncState();
       NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
       navigationView.setNavigationItemSelectedListener(this);
   }
   @Override
   public void onBackPressed() {
       DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
       if (drawer.isDrawerOpen(GravityCompat.START)) {
           drawer.closeDrawer(GravityCompat.START);
       } else {
           super.onBackPressed();
       }
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
   }
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       int id = item.getItemId();
       //noinspection SimplifiableIfStatement
       if (id == R.id.action_settings) {
           return true;
       }
       return super.onOptionsItemSelected(item);
   }
   @SuppressWarnings("StatementWithEmptyBody")
   @Override
   public boolean onNavigationItemSelected(MenuItem item) {
       // Handle navigation view item clicks here.
       int id = item.getItemId();
       if (id == R.id.nav_camara) {
           // Handle the camera action
       else if (id == R.id.nav_gallery) {
       } else if (id == R.id.nav_slideshow) {
       } else if (id == R.id.nav_manage) {
       } else if (id == R.id.nav_share) {
       } else if (id == R.id.nav_send) {
       }
       DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
       drawer.closeDrawer(GravityCompat.START);
       return true;
   }
}
TEST RUN.
DOWNLOAD


.

No comments:

Post a Comment