I have one activity with 4 fragments. Every fragment got its separate class and layout. This is the parent activity class:
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new MainFragment();
case 1:
return new CatalogFragment();
case 2:
return new FolderFragment();
case 3:
return new SettingsFragment();
default:
return null;
}
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "PLAYER";
case 1:
return "CATALOG";
case 2:
return "FOLDERS";
case 3:
return "SETTINGS";
}
return null;
}
}
Fragment classes are only with:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main,container,false);
return view;
}
and a default constructor.
In FragmentMain i have 2 TextViews. I want to change them dynamically depending on info within MainActivity. I am using ViewPager.
This is my MainActivity XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
if i am doing this all wrong please correct me!
I can findViewById() but i cannot set the text(NullPointer).
From the fragment i cannot even findViewById() even thou they are in the MainFragment XML. I've searched a lot and i cannot find easy to understand solution or explanation how this works. Thanks in advance!
Related
I am following example from Android doc example here: https://developer.android.com/training/animation/screen-slide.html
I was wondering if I want several different fragments on each page then what would be the best approach.
This is my MainActivity, as you can see I open new fragment class for each page position:
public class MainActivity extends AppCompatActivity {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 4;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new MainActivity.ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
/**
* A simple pager adapter that represents 4 HeartsPageFragment objects, in
* sequence.
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new HeartsPageFragment();
case 1:
return new DiamondsPageFragment();
case 2:
return new ClubsPageFragment();
case 3:
return new SpadesPageFragment();
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
This is one of the fragment classes, all four are almost same:
public class ClubsPageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.view_clubs, container, false);
return rootView;
}
}
I was wondering if it's best approach to have four different fragment classes for each page? I feel like this is repetition and bad, but I am not sure how I could fix this. It is lagging on my phone to when I switch between those. Any tips would be welcome.
Edit:
Here is the code for the layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/content_suit"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/first_row_hearts">
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView1"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_2"/>
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView2"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_3"/>
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView3"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_4"/>
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView4"
android:src="#drawable/spades_5"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/second_row_hearts"
android:layout_below="#id/first_row_hearts"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView5"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_6" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView6"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_7" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView7"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_8" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView8"
android:src="#drawable/spades_9" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/third_row_hearts"
android:layout_below="#id/second_row_hearts"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView9"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_10" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView10"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_jack" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView11"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_queen" />
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView12"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_king"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fourth_row_hearts"
android:layout_below="#+id/third_row_hearts"
android:layout_marginTop="10dp">
<ImageView
android:layout_width="80dp"
android:layout_height="112dp"
android:id="#+id/imageView13"
android:layout_marginEnd="10dp"
android:src="#drawable/spades_ace"/>
</LinearLayout>
</RelativeLayout>
If you donot want to make different fragment classes study this auto generated code of tabbed activity android.
public class MainActivity extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
LeftDrawerLayout mLeftDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
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();
}
});
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 7 total pages.
return 7;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
case 3:
return "SECTION 4";
case 4:
return "SECTION 5";
case 5:
return "SECTION 6";
case 6:
return "SECTION 7";
}
return null;
}
}
}
I am making use of PagerTabStrip in my android app like the ones used in the new Google play store app. I went through a few tutorials and was succesfull in creating three tabs. They are Information, Electronic Configuration and Facts
Information
Electronic Configuration
Facts
Here is the xml layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:textSize="30dp"
android:background="#000000"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#ffffff" />
</android.support.v4.view.ViewPager>
Here is the Java file
public class Tabs extends FragmentActivity
{
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.act2aluminium);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
public SectionsPagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount()
{
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return "Information";
case 1:
return "Electronic Configuration";
case 2:
return "Facts";
}
return null;
}
}
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return textView;
}
}
}
Now, my question is How do I attach layouts or pages to the tabs instead of the tiny 1, 2, 3 textViews?
I've searched a lot but coudn't find a good explanation of how it's done. Please help me out with the code. Thanks in advance!
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:textSize="30dp"
android:background="#000000"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#ffffff" />
</android.support.v4.view.ViewPager>
page1.xml
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click here" />
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/button1"
android:text="I am Page one" />
page2.xml
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click here" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/button2"
android:text="I am Page two" />
page3.xml
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Click here" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/button3"
android:text="I am Page three" />
MainActivity.java
public class MainActivity extends FragmentActivity
{
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter
{
public SectionsPagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
switch (position) {
case 0:
// Top Rated fragment activity
return new Information();
case 1:
// Games fragment activity
return new ElectonicConfiguration();
case 2:
// Movies fragment activity
return new Fact();
}
return null;
}
#Override
public int getCount()
{
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position)
{
case 0:
return "Information";
case 1:
return "Electronic Configuration";
case 2:
return "Facts";
}
return null;
}
}
//Page 1 Fragment
public static class Information extends Fragment {
public Information()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page1, null);
TextView textView = (TextView)view.findViewById(R.id.text1);
Button button=(Button)view.findViewById(R.id.button1);
return view ;
}
}
//Page 2 Fragment
public static class ElectonicConfiguration extends Fragment {
public ElectonicConfiguration()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page2, null);
TextView textView = (TextView)view.findViewById(R.id.text2);
Button button=(Button)view.findViewById(R.id.button2);
return view ;
}
}
//Page 3 Fragment
public static class Fact extends Fragment {
public Fact()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.page3, null);
TextView textView = (TextView)view.findViewById(R.id.text3);
Button button=(Button)view.findViewById(R.id.button3);
return view ;
}
}
}
page1.xml, page2.xml,page3.xml are the layout file for the first,second and third page respectively.And There are 3 different fragments declared in the MainActivity.java for the 3 different pages. getItem() of SectionsPagerAdapter class manages all the 3 fragment pages. Make changes in the xml file as per your wish.I think the code is pretty self explanatory.If you have any doubt don't hesitate to ask.
Hope it helps. Cheers!
I have created a PagerTabStrip for my ViewPager and it is all working fine. Using a switch and case, I have the titles set up for each page. However I can't find how to set the background colour or or text colour within the java. I can change the colour in the xml, but I want the colour to change with my switch and case.
Here is my code:
EDIT: Added more code
public class RemViewPagerActivity extends FragmentActivity {
private static final int NUM_PAGES = 3;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_pager_activity);
mPager = (ViewPager) findViewById(R.id.viewpager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter); /
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 1) {
super.onBackPressed();
} else if (mPager.getCurrentItem() == 0){
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
else if (mPager.getCurrentItem() == 2){
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { // Sets the content of the adapter
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) { // Sets the fragment for each position
switch (position) {
case 0: return new Fragment1();
case 1: return new Fragment2();
case 2: return new Fragment3();
}
return null;
}
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "1";
case 1: return "2";
case 2: return "3";
}
return null;
}
#Override
public int getCount() { // Sets the number of pages
return NUM_PAGES;
}
}
}
Code of view_pager_activity.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.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
Thank you
I do not understand how can I transfer data from fragmentactivity to fragment, i have:
MainScreen.class
public class MainScreen extends FragmentActivity {
CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;
public static String currentCityId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_collection);
mCollectionPagerAdapter = new CollectionPagerAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setTitle(getString(R.string.app_name));
actionBar.setIcon(R.drawable.device_access_brightness_high);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Log.w("Refresh", "refresh");
break;
case R.id.action_options:
break;
case R.id.submenu_about:
break;
case R.id.submenu_help:
break;
case R.id.submenu_buy:
break;
case R.id.submenu_settings:
break;
case R.id.submenu_share:
break;
default:
break;
}
return true;
}
public class CollectionPagerAdapter extends FragmentStatePagerAdapter {
FragmentManager fm;
public CollectionPagerAdapter(FragmentManager fm) {
super(fm);
this.fm = fm;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentDay();
case 1:
return new FragmentHour();
case 2:
return new FragmentDaily();
default:
return new FragmentDay();
}
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.tab_day);
case 1:
return getString(R.string.tab_hour);
case 2:
return getString(R.string.tab_daily);
default:
return getString(R.string.tab_day);
}
}
}
FragmentDay.class
public class FragmentDay extends Fragment {
TextView currentTemp;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.hour_screen, container, false);
Bundle args = getArguments();
currentTemp = (TextView)rootView.findViewById(R.id.day_current_temp);
return rootView;
}
}
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#drawable/bk_new">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp"/>
</android.support.v4.view.ViewPager>
and fragment.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dayScreen"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bk_new">
<TextView
android:id="#+id/day_current_temp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:text="22 C"
android:textColor="#FFFFFF"
android:textSize="38dp"
android:layout_marginTop="270dp"/>
Read these articles (ViewPager Activity to notify a Fragment of a specific event, Updating fragments from Activity in a ViewPager, ViewPager - Update fragment from activity) and do not understand what to do!
I will be very grateful for the help!
When you say "transfer data" I'm assuming you mean variables so you can tweak your fragment in whichever way.
You can access the activity object at any time within the fragment using getActivity() method and cast it to your Activity implementation...
// Code in the fragment
((MainScreen) getActivity()).getSomeDataFromActivity()
There is an article here also by Google which describes patterns of communication between Fragment and Activity.
I'm trying to put com.viewpagerindicator.TabPageIndicator in the SherlockFragment. While the scrolling of content in ViewPager works very well, the styling of TabPageIndicator is not working properly. This pager hold five fragments and their titles would not fit the width of the screen. I have this working fine in Activity but when I try using this in Fragment, all five titles got cut off to fit the width of the screen.
I tried using both LayoutInflater provided in Fragment#onCreateView and getSystemService both result in the same result.
Not sure if related but I also have com.actionbarsherlock.widget.SearchView in this layout and all the attributes weren't applied.
my_search_layout.xml
<com.viewpagerindicator.TabPageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/indicator"
/>
</RelativeLayout>
MySearchFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutInflater themedInflater = (LayoutInflater) getSherlockActivity()
.getSupportActionBar()
.getThemedContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View root = themedInflater.inflate(R.layout.my_search_layout, container, false);
FragmentManager fm = ((Fragment)this).getChildFragmentManager();
mSectionsPagerAdapter = new SectionsPagerAdapter(fm);
mViewPager = (ViewPager) root.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabPageIndicator indicator = (TabPageIndicator) root.findViewById(R.id.indicator);
indicator.setViewPager(mViewPager);
return root;
}
SectionsPagerAdapter
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = null;
Log.d(TAG, "SectionsPagerAdapter: " + i);
fragment = new HelloFragment();
return fragment;
}
#Override
public int getCount() {
return 5;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.section_title_page_1);
case 1:
return getString(R.string.section_title_page_2);
case 2:
return getString(R.string.section_title_page_3);
case 3:
return getString(R.string.section_title_page_4);
case 4:
return getString(R.string.section_title_page_5);
}
return null;
}
}