user2756111
user2756111

Reputation: 79

Confusion about fragments and their activities

I have a TabLayout and ViewPager to hold and navigate between my fragments (3 of them) and that is working fine however I'm getting confused as to how I interact with each fragment for example button clicks or edit texts. Here is my main activity xml.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.TabLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabLayout"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:background="@color/colorPrimary"
    app:tabMode="fixed"
    app:tabGravity="fill" />

<android.support.v4.view.ViewPager
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/viewPager"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/tabLayout" />

</RelativeLayout>

This is the activity for it.

public class ActivityMain extends AppCompatActivity {

private String tabTitles[] = new String[] { "contacts", "home", "search" };

TabLayout tabLayout;
ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPager.setAdapter(new CustomAdapater(getSupportFragmentManager(),getApplicationContext()));

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager);

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }
    });

}

private class CustomAdapater extends FragmentPagerAdapter {

    private String fragments [] = {"contacts", "home", "search"};

    public CustomAdapater(FragmentManager supportFragmentManager, Context applicationContext) {
        super(supportFragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new FragmentContacts();
            case 1:
                return new FragmentMain();
            case 2:
                return new FragmentSearch();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return fragments.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}
}

And here is a fragment activity.

public class FragmentSearch extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.fragment_search,container,false);
}
}

Now say in my R.layout.fragment_search I had a button just for examples sake, how do I set an onclicklistener for it? Is it within the FragmentSearch or ActivityMain? I've searched around but I'm just getting more confused.

Upvotes: 1

Views: 30

Answers (2)

Benjam&#237;n Molina
Benjam&#237;n Molina

Reputation: 211

Now, if you want to communicate between Fragments, you will need to implement more things. The basic way is to use interfaces to be implemented by your activity, so each fragment can call those methods within the activities.

There are also messaging libraries such as Otto and EventBus, they are great by sending stuff between not just fragments, but services also.

Upvotes: 1

Vucko
Vucko

Reputation: 7479

You'd set your onClickListener in the fragment's onCreateView method by doing something like:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.fragment1, container, false);
   myButton = (Button) rootView.findViewById(R.id.buttonId);
   myButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
         }
     });
   return rootView;
  } 

Everything is the same regarding the onClickListener, as you can see. The only difference is that you have to inflate to view and use that view to find the appropriate components (e.g. buttons, textviews..)

Upvotes: 1

Related Questions