0

So basically,

I'm using the slidingTabLayout and slidingTabStrip

I have a ViewPager:

<android.support.v4.view.ViewPager
     android:id="@+id/pager"
     android:layout_height="match_parent"
     android:layout_width="match_parent"
     android:layout_weight="1"
     ></android.support.v4.view.ViewPager>

Which gets filled with a adapter on my MainActivity.java:

private ViewPager pager;
private TabsViewPagerAdapter adapter;
private SlidingTabLayout tabs;

protected void onCreate(Bundle savedInstanceState) {
    adapter =  new TabsViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        tabs = (SlidingTabLayout) findViewById(R.id.tabs);
        tabs.setDistributeEvenly(true);
        tabs.setViewPager(pager);
}

The TabsViewPagerAdapter returns a Fragment, depending on the tab position:

public Fragment getItem(int position) {

    if(position == 0)
    {
        PersonalTab tab1 = new PersonalTab();
        return tab1;
    }
    else
    {
        GroupTab tab2 = new GroupTab();
        return tab2;
    }
}

finally, each fragment contains this simple layout:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/personal_tab">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

The tabs are rendering pretty well and working. Now I have two questions since I'm newbie:

  1. How can I access this fragment from the main activity so I can change the inner textView text for example?

  2. In case it is possible to access the fragment, is it better to replace the fragment for data changes or dynamically change the data inside?

user1214120
  • 173
  • 1
  • 2
  • 15

1 Answers1

0

you can change inner TextView text from fragment, no need to accessing from your Activity but in case that you want access to Fragment , you can use getFragmentByTag function.

here is a helpful link describing how to achieve this goal.

Community
  • 1
  • 1
Mohammad Rahchamani
  • 5,002
  • 1
  • 26
  • 36
  • you mean by calling a function inside my fragment? That's good but how do I get my fragment instance so I can be able to call this function - will check out your link! – user1214120 Jun 18 '15 at 02:49
  • no. you can call `findFragmentByTag` from your activity. you have to set a tag for each fragment and then, find fragment with that tag. It's completely explained with example in above link. – Mohammad Rahchamani Jun 18 '15 at 02:51
  • Hello! I did not actually use the getFragmentByTag, instead I followed this [answer](http://stackoverflow.com/a/15261142/1214120) which worked pretty well. If there are any down sides of using this approach it would be useful to know :) – user1214120 Jun 18 '15 at 20:39
  • that answer is somehow equal. In this approach you should create your own list (or array or something else) containing `Fragment`s and add/remove them manually but with `getFragmentByTag` you don't have to do these works. but if that link is working for you, use it. – Mohammad Rahchamani Jun 18 '15 at 22:24