![]() |
| Agregar leyenda |
![]() |
| Agregar leyenda |
![]() |
| Agregar leyenda |
![]() |
| Agregar leyenda |
MainActivity.java
package com.example.manueltrejos.swipeviews;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity implements ActionBar.TabListener,ViewPager.OnPageChangeListener {
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//---------------VIEW PAGER---------------
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(adapter);
mViewPager.setOnPageChangeListener(this);
//-------------END VIEW PAGER-------------
//---------------ACTION BAR---------------
ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00639F"))); //Definir Color del Action Bar
actionBar.setTitle("SwipeViews");
actionBar.setSubtitle("SwipeViews");
actionBar.setDisplayShowHomeEnabled(true);
//actionBar.setIcon(R.drawable.ic_launcher);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#00639F"))); // Definir color de los tabs del Action Bar
ActionBar.Tab tab = actionBar.newTab().setText("Fragment1").setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("Fragment2").setTabListener(this);
actionBar.addTab(tab);
tab = actionBar.newTab().setText("Fragment3").setTabListener(this);
actionBar.addTab(tab);
//--------------END ACTION BAR-------------
}
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
default:
return null;
}
}
public int getCount() {
return 3;
}
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//Metodo implementado por ActionBar.Tablistener
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//Metodo implementado por ActionBar.Tablistener
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
//Metodo implementado por ActionBar.Tablistener
}
@Override
public void onPageScrolled(int i, float v, int i1) {
//Método implementado por ViewPager.OnPageChangeListener
}
@Override
public void onPageSelected(int i) {
//Método implementado por ViewPager.OnPageChangeListener
getSupportActionBar().setSelectedNavigationItem(i);
}
@Override
public void onPageScrollStateChanged(int i) {
//Método implementado por ViewPager.OnPageChangeListener
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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);
}
}
Fragment1.java
package com.example.manueltrejos.swipeviews;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment1, container, false);
return rootView;
}
}
Fragment2.java
package com.example.manueltrejos.swipeviews;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends Fragment {
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment2, container, false);
return rootView;
}
}
Fragment3.java
package com.example.manueltrejos.swipeviews;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment3 extends Fragment {
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment3, container, false);
return rootView;
}
}
main_activity.xml
<android.support.v4.view.ViewPager
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"
android:id="@+id/pager"
tools:context=".MainActivity">
</android.support.v4.view.ViewPager>
fragment1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment1"
android:textStyle="bold"
android:textSize="32dp"
android:layout_gravity="center"/>
</LinearLayout>
fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#04B404">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment2"
android:textStyle="bold"
android:textSize="32dp"
android:layout_gravity="center"/>
</LinearLayout>
fragment3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#08088A">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fragment3"
android:textStyle="bold"
android:textSize="32dp"
android:layout_gravity="center"/>
</LinearLayout>




No hay comentarios.:
Publicar un comentario