Muhammad Jobayer
Muhammad Jobayer

Reputation: 157

Android Fragment initialization

I am meeting with Fragment for the first time. So it is a bit complicated to me. I am reading a tutorial at android-hive. But I am unable to understand a point. There are something I don't understand. There are oneFragment(), twoFragment()... But I can't initiate them. So, please complete any of those... oneFragmemt() or twoFragment() from this link. I will be so pleased. Help me...

Upvotes: 2

Views: 2321

Answers (1)

Alvaro
Alvaro

Reputation: 1448

Muhamar, you always need to initiate the fragment inside an activity. If your fragment 1 has this code:

public class OneFragment extends Fragment{

    public OneFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_one, container, false);
    }

}

then in the main activity you can initiate like in the tutorial says:

public class MainActivity extends AppCompatActivity {

  //blabla

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

    //blablabla
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    //blabla
    viewPager.setAdapter(adapter);
}

In this case the main activity is using an adapter to display more than one fragment, so you should have in your code more the file of the adapter, which you can find also in the tutorial.

UPDATE:

if you don't want to initalize the fragment with the adapter, you have to do two things to initialize the fragment:

1) Put fragment in layout: in the layout of your main activity you have to include a fragment and identify it (in this example android:id="@+id/headlines_fragment"):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment android:name="com.example.android.fragments.HeadlinesFragment"
              android:id="@+id/headlines_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>

2) Insert the fragment in your main activity: with the following code you can add the freagment in your activity:

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);



            OneFragment firstFragment = new OneFragment();

            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.headlines_fragment, firstFragment).commit();

    }
}

UPDATE 2: to initialize from the adapter you just have to add it on the main activity, as the tutorial says. add this class on main activity:

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 addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

Hope it helps ;)

Upvotes: 1

Related Questions