I am developing an application in Android to test Fragment Transitions..
As a starting point, I have created an Activity with one Fragment, "Fragment1". I would like to load this fragment at the start of the Activity..
Problem arises in the MainActivity.. I get the message "cannot resolve method"..
fragmentTransaction.add(R.id.container,fragment1);
First argument of the method is the ViewPager id in the MainActivity.xml. Second Argument an instance of the Fragment1 class.
WHAT SHOULD I DO?
The following code:
For the MainActivity.java
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private android.support.v4.app.FragmentManager fragmentManager;
private android.support.v4.app.FragmentTransaction fragmentTransaction;
private boolean status_zwart, status_geel, status_rood;
ViewPager viewPager;
//FragmentPagerAdapter saves the Fragments in memory, so they can be accessed more rapidly
//FragmentStatePagerAdapter is better for situations in which you have a lot of Fragments >10
//ViewPager is a LayoutManager that is used to animate Transitions between entire screens, serves as a Fragment Container
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Activity Started");
viewPager = (ViewPager)findViewById(R.id.container);
Fragment1 fragment1 = new Fragment1();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container,fragment1);
fragmentTransaction.commit();
}
For the MainActivity.xml I use a ViewPager as a Container View for the Fragments..
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gebruiker.fragment_transitions.MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
For the Fragment.java
public class Fragment1 extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1_layout,container,true);
return rootView;
}
}