I am using fragments to get the Tabs style on my app, like seen here:
Tabs
Already deducted that each tab is a fragment and each one has a layout, but i cant display on each one display what i want.
Before having the fragments i had a simple activity to display data from a database on a listview, but now i cant get it to work on one of the fragments, with the following code that i used:
dal = new DAL(TabListas.this);
dal.connect(DBAccessMode.READ);
Cursor cursor = dal.selectALLFromRegSintomas();
// Find ListView to populate
ListView lvItems = (ListView) findViewById(R.id.listv);
// Setup cursor adapter using cursor from last step
RegistosCursorAdapter todoAdapter = new RegistosCursorAdapter(TabListas.this, cursor, 0);
// Attach cursor adapter to the ListView
lvItems.setAdapter(todoAdapter);
How can i make a use the fragments to display data from db in a listview in one fragment ? What should i change ?
VerRegsSintomas.java (like mainactivity.java):
package com.example.bugdroid.menuexe.Activities;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import com.example.bugdroid.menuexe.CursorAdapter.RegistosCursorAdapter;
import com.example.bugdroid.menuexe.R;
import com.example.bugdroid.menuexe.TabFragments.PageAdapter;
import com.example.bugdroid.menuexe.database.DAL;
import com.example.bugdroid.menuexe.database.DBAccessMode;
public class VerRegsSintomas extends AppCompatActivity {
private ViewPager mViewPager;
private DAL dal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ver_regs_sintomas);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1a212c")));
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Sintomas"));
tabLayout.addTab(tabLayout.newTab().setText("Listas"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PageAdapter adapter = new PageAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
PageAdapter.java:
package com.example.bugdroid.menuexe.TabFragments;
/**
* Created by BugDroid on 07/06/2016.
*/
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PageAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PageAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
TabSintomas tab1 = new TabSintomas();
return tab1;
case 1:
TabListas tab2 = new TabListas();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
TabSintomas.java:
package com.example.bugdroid.menuexe.TabFragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.bugdroid.menuexe.R;
public class TabSintomas extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab_sintomas, container, false);
}
}
fragment_Tab_sintomas.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Activities.VerRegistos">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listv"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Try this Example ,here you can find a TabLayout with the help of Viewpager and custom toolbar and along with this all you can find a navigation drawer over there.
Related
So, in the current state, I can see the fragment inside, showing when I boot up the virtual phone; however, as soon as I switch to different fragment, from my drawer, and switch back again into the fragment, with the tableLayout, its shows nothing! Plus, switching between the tabs does seem to fix it.
You can see the image here
Here is the code:
package com.example.teamapp;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.material.tabs.TabItem;
import com.google.android.material.tabs.TabLayout;
public class HomeFragmentNav extends Fragment {
FragmentManager fragmentManagerHome;
ViewPager viewPager;
TabLayout tableLayout;
PagerAdapter pagerAdapter;
TabItem TeamsTab;
TabItem ChatTab;
View v;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_home_nav, container, false);
tableLayout = v.findViewById(R.id.TabBar);
TeamsTab = v.findViewById(R.id.TeamsTab);
ChatTab = v.findViewById(R.id.ChatTab);
viewPager = v.findViewById(R.id.ViewPager);
tableLayout.getTabAt(1).select();
tableLayout.getTabAt(0).select();
fragmentManagerHome = getFragmentManager();
pagerAdapter = new PagerAdapter(fragmentManagerHome,
tableLayout.getTabCount(), getContext());
viewPager.setAdapter(pagerAdapter);
tableLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return v;
}
#Override
public void onResume() {
super.onResume();
tableLayout = v.findViewById(R.id.TabBar);
TeamsTab = v.findViewById(R.id.TeamsTab);
ChatTab = v.findViewById(R.id.ChatTab);
viewPager = v.findViewById(R.id.ViewPager);
tableLayout.getTabAt(1).select();
tableLayout.getTabAt(0).select();
fragmentManagerHome = getFragmentManager();
pagerAdapter = new PagerAdapter(fragmentManagerHome,
tableLayout.getTabCount(), getContext());
viewPager.setAdapter(pagerAdapter);
}
}
Thank you for your help.
Prevent the page from refreshing.
view_pager.setOffscreenPageLimit(2);
Found the problem, I used getFragmentManager() instead of getChildFragmentManager()
I have a tablayout with 5 tabs. Each tab has an icon and title. I want to change color of icon and title for tab that is selected and tab that is un-selected. For this i try using custom tabs but it didn't work. Here is my code for custom_tab.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:id="#+id/tabIcon"/>
<com.prj.shopt.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#C1C1C1"
android:gravity="center"
android:id="#+id/tabTitle"/>
</LinearLayout>
and my code in Home2Activity:
package com.prj.shopt;
import android.graphics.Typeface;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Home2Activity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
private TabLayout tabs;
private ViewPager viewPager;
private HomeTabsAdapter tabsAdapter;
private int[] activeIcons={R.drawable.homeactive,R.drawable.category_highlighted,R.drawable.heart_highlighted,
R.drawable.basketactive,R.drawable.avatar_highlighted};
private int[] deactiveIcons={R.drawable.homedeactive,R.drawable.category_default,R.drawable.heart_default,
R.drawable.basketdeactive,R.drawable.avatar_default};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home2);
tabs=findViewById(R.id.homeTabs);
viewPager=findViewById(R.id.tabsViewPager);
setupViewPager();
tabs.setupWithViewPager(viewPager);
setupTabIcons();
tabs.addOnTabSelectedListener(this);
}
private void setupTabIcons(){
if(tabs.getTabCount()<5)
return;
int len=activeIcons.length;
for(int j=0;j<len;j++) {
//tabs.getTabAt(j).setIcon(deactiveIcons[j]);
tabs.getTabAt(j).setCustomView(HomeTabsAdapter.getTabView(deactiveIcons[j], R.color.tab_inactive, "test", Home2Activity.this,tabs));
}
tabs.getTabAt(0).setCustomView(HomeTabsAdapter.getTabView(activeIcons[0],R.color.tab_active,"test",Home2Activity.this,tabs));
}
private void setupViewPager(){
tabsAdapter=new HomeTabsAdapter(getSupportFragmentManager());
viewPager.setAdapter(tabsAdapter);
setupFragments();
}
private void setupFragments() {
for(int i=0;i<5;i++)
tabsAdapter.addFragment(new FavFragment(),getResources().getString(R.string.favTab));
}
#Override
public void onTabSelected(TabLayout.Tab tab) {
//tab.setIcon(activeIcons[tab.getPosition()]);
tab.setCustomView(HomeTabsAdapter.getTabView(activeIcons[tab.getPosition()],R.color.tab_active,"test",Home2Activity.this,tabs));
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
//tab.setIcon(deactiveIcons[tab.getPosition()]);
tab.setCustomView(HomeTabsAdapter.getTabView(deactiveIcons[tab.getPosition()],R.color.tab_inactive,"test",Home2Activity.this,tabs));
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
//tab.setIcon(activeIcons[tab.getPosition()]);
tab.setCustomView(HomeTabsAdapter.getTabView(activeIcons[tab.getPosition()],R.color.tab_active,"test",Home2Activity.this,tabs));
}
}
and Here is my adapter(HomeTabsAdapter.java):
import android.content.Context;
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.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class HomeTabsAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;
List<String> tabTitles;
public HomeTabsAdapter(FragmentManager fm) {
super(fm);
fragments=new ArrayList<>();
tabTitles=new ArrayList<>();
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
public void addFragment(Fragment fragment,String title){
fragments.add(fragment);
tabTitles.add(title);
notifyDataSetChanged();
}
public static View getTabView(int imageId, int color, String title, Context context, TabLayout tabLayout){
View v= LayoutInflater.from(context).inflate(R.layout.custom_tab,tabLayout,false);
TextView tabTitle=v.findViewById(R.id.tabTitle);
tabTitle.setText(title);
tabTitle.setTextColor(color);
tabTitle.setTypeface(Utility.getFontTypeFace(context));
ImageView icon=(v.findViewById(R.id.tabIcon));
icon.setImageResource(0);
icon.setImageResource(imageId);
return v;
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}
}
And the result is icons with fix color and also titles with fixed color. Nothing change with select and unselect the tabs.
Thanks for any help
Use this method:-
private void setCustomTab(TabLayout tabLayout, int index, String title) {
TextView tab_home = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.custome_tab, null);
tab_home.setText(title);
tabLayout.getTabAt(index).setCustomView(tab_home);
}
Use this above method like below:-
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
setCustomTab(tabLayout, 0, "Upcoming");
setCustomTab(tabLayout, 1, "Completed");
I have researched a lot on SharedElement Transitions , however I could only find Transitions for onclick event.
I want the animation between Fragments where the speed of animation is controlled by the user's scrolling speed. If the user stops in between while scrolling the objects should be still.
Example :
In this image , the viewpager is in the middle of screen1 and screen2 and the animating objects are in stopped state.
These objects move from one screen to another and also change their positions.
The translation animations part I have figured out but how to achieve this shared element transition with viewpager between fragments. And moreover it should scroll/animate with the speed of finger swipe.
MainActivity.java
package com.karan.onboardanimation;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private MyFragmentPagerAdapter pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: ");
ViewPager pager = (ViewPager) findViewById(R.id.pager);
FragmentManager fm = getSupportFragmentManager();
pagerAdapter = new MyFragmentPagerAdapter(fm);
pager.setAdapter(pagerAdapter);
pager.setCurrentItem(0);
}
}
FirstFragment.java- This fragment has an imageview which has to shared with second fragment when user is swiping on the viewpager.
package com.karan.onboardanimation;
import android.app.ActivityOptions;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class FirstFragment extends Fragment {
private static final String TAG = "FirstFragment";
ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= 21){
}
Log.d(TAG, "onCreate: ");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: ");
View view = inflater.inflate(R.layout.first_fragment, container, false);
imageView = (ImageView) view.findViewById(R.id.img1);
ViewPagerTransformer viewPagerTransformer = new ViewPagerTransformer();
viewPagerTransformer.transformPage(imageView, 1);
return view;
}
}
MyFragmentPagerAdapter.java
package com.karan.onboardanimation;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private static final String TAG = "MyFragmentPagerAdapter";
final int PAGE_COUNT = 2;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
Log.d(TAG, "MyFragmentPagerAdapter: ");
}
#Override
public Fragment getItem(int position) {
Log.d(TAG, "getItem: ");
switch (position) {
case 0:
return new FirstFragment();
case 1:
return new SecondFragment();
default:
return null;
}
}
#Override
public int getCount() {
return PAGE_COUNT;
}
}
second_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d02030"
android:orientation="vertical">
<ImageView
android:id="#+id/img2"
android:layout_width="100dp"
android:layout_centerInParent="true"
android:src="#drawable/orange_centre"
android:transitionName="selectedIcon"
android:layout_height="100dp" />
</RelativeLayout>
first_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/img1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="#drawable/orange_centre" />
</LinearLayout>
Edit1-
This question is not a duplicate of link mentioned in comment because that question is a sub part of my question. The difference is that the animation in that question happens on a click with a static speed. However in my question, the speed of animation must be equal to the speed of ViewPager swipe speed.
You can try PageTransformer. It is not shared transition but more of view animation that is dependent on page position offset generated by swipe events.
I have a problem with android a ViewPager setup with FragmentPagerAdapter and TabLayout
TabLayout works fine. But, the code in all tabs run simultaneously. I want the code in the currently selected tab to run
This is my activity
package francesco.prisco.siamostudenti.ui;
import android.os.Bundle;
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.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import francesco.prisco.siamostudenti.R;
import francesco.prisco.siamostudenti.ui.fragments.CalendarFragment;
import francesco.prisco.siamostudenti.ui.fragments.ChatFragment;
import francesco.prisco.siamostudenti.ui.fragments.EmailFragment;
import francesco.prisco.siamostudenti.ui.fragments.HomeFragment;
import francesco.prisco.siamostudenti.ui.fragments.ProfiloFragment;
public class HomeTabActivity extends AppCompatActivity {
// private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_tab);
// toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons() {
int[] tabIcons = {
R.drawable.ic_home,
R.drawable.ic_calendar,
R.drawable.ic_chat,
R.drawable.ic_email,
R.drawable.ic_profile
};
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
tabLayout.getTabAt(4).setIcon(tabIcons[4]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new HomeFragment(), "HOME");
adapter.addFrag(new CalendarFragment(), "CALENDAR");
adapter.addFrag(new ChatFragment(), "CHAT");
adapter.addFrag(new EmailFragment(), "EMAIL");
adapter.addFrag(new ProfiloFragment(), "PROFILO");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
// return null to display only the icon
return null;
}
}
}
And this is code of a fragment
package francesco.prisco.siamostudenti.ui.fragments;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import francesco.prisco.siamostudenti.R;
import francesco.prisco.siamostudenti.ui.HomeTabActivity;
public class ProfiloFragment extends Fragment {
public ProfiloFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profilo, container, false);
return rootView;
}
}
Help me please
Like #Mrugesh Thaker says, all of the Fragment lifecycle functions - onCreateView(), etc. are executed every time the View of a Fragment is inflated.
Here, the View of each Fragment is inflated every time it becomes the adjacent Fragment of the current Fragment page.
So, you should move the 'code each Fragment needs to execute' when it becomes the currently selected Fragment page into an OnPageChangeListener
In your HomeTabActivity, when you set up the ViewPager, add an
OnPageChangeListener to yourViewPager
private void setupViewPager(ViewPager viewPager) {
.
.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
public void onPageScrollStateChanged (int state){
}
public void onPageSelected (int position){
/*
* Define the action to be performed for each page.
* Variable 'position' is the index of the page in the ViewPager
*/
}
public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels){
}
});
}
Learning Resource
Actually, view pager works like that. The main aim of view pager is to enable a user to have smooth swipe , so what it does is , it will create all the fragments that you gave at a time to have smooth swiping flow. This is actually the limitation of it because all the methods of fragment lifecycle are called at a time.
The question is not as complex as it seems.
I made this design based on some tutorials on the internet.
I have a tabbed browsing with swipe using SupportLibrary for devices prior to Honeycomb can visualize.
Now I want to add a ActionBar in the application. Also by using the SuportLibrary appcompat.v7.
I've used the appcompat.v7 to create an application with ActionBar compatible with older versions of Android.
What I'm not getting now and join these two navigational structures.
I really want to add to this mu ActionBar project with swipe tabs below:
MainActivity:
package br.com.wgbn.poemadodiav2;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
private ActionBar actionBar;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewPager = (ViewPager) findViewById(R.id.viewpager);
TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());
mViewPager.setAdapter(titleAdapter);
mViewPager.setCurrentItem(0);
}
#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;
}
}
TitleAdapter:
package br.com.wgbn.poemadodiav2;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
public class TitleAdapter extends FragmentPagerAdapter{
private String titles[] = new String[]{"O Poema do Dia","Comentários"};
private Fragment frags[] = new Fragment[titles.length];
public TitleAdapter(FragmentManager fm) {
super(fm);
frags[0] = new FragmentView1();
frags[1] = new FragmentView2();
}
#Override
public CharSequence getPageTitle (int position){
Log.v("TitleAdapter - getPageTitle=", titles[position]);
return titles[position];
}
#Override
public Fragment getItem(int position) {
Log.v("TitleAdapter - getItem=", String.valueOf(position));
return frags[position];
}
#Override
public int getCount() {
return frags.length;
}
}
activity_main.xml:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v4.view.PagerTabStrip
android:id="#+id/pagerTabStrip"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#cccccc"/>
</android.support.v4.view.ViewPager>
</LinearLayout>
This is the image of the app working so far.
I hope you can help me.
EDIT
Following the guidance of NikolaDespotoski, decided the issue only changing the extension class for ActionBarActivity!
Actually I was thinking it had to implement the two things together, without realizing that ActionBarActivity already encompassed both.
My code looked like this, and it worked:
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
public class MainActivity extends ActionBarActivity {
// code here
}
Thanks!