Monday 4 July 2016

Android FragmentManager and FragmentTransaction.

FragmentManager and FragmentTransaction example in which we will replace Fragment with another Fragment using Button OnClickListener.o achieve it, first we get the instance of FragmentTransaction using FragmentManager, then call replace method which will accept view container id and new fragment instance.

Create the activity.xml in project.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/linear" />

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#371931"
        android:orientation="horizontal"
        android:padding="12dp">

        <LinearLayout
            android:id="@+id/newsLinear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="ID"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/news" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="NEWS"
                android:textColor="#ffffff"
                android:textSize="12dp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/bookLinear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="ID"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/book" />


            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="BOOKING"
                android:textColor="#ffffff"
                android:textSize="12dp" />
        </LinearLayout>

    </LinearLayout>

</RelativeLayout>


Create fragment_new.xml file in project.  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#33cc88"
    android:orientation="vertical">
</LinearLayout>


Create fragment_book.xml file in project.
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">
</LinearLayout>



Create MainActivity class in project.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import refresh.swipe.com.designdemo.fragment.FragmentBook;
import refresh.swipe.com.designdemo.fragment.FragmentNews;

/**
 * Created by Laptop on 04-07-2016.
 */
public class MainActivity extends AppCompatActivity {
    FragmentManager fragmentManager;
    LinearLayout newLinearLayout,bookLinearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_pro);
        Init();
        Listener();
    }

    private void Listener() {
        fragmentManager = getSupportFragmentManager();
        newLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentNews fragmentNews = new FragmentNews();
                InflateFragment(fragmentNews);
            }
        });
        bookLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentBook fragmentBook = new FragmentBook();
                InflateFragment(fragmentBook);
            }
        });

    }
    /*********************************
     * inflate fragments
     *****************************/
    public void InflateFragment(Fragment frg) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container, frg);
        //fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
    private void Init() {
        newLinearLayout=(LinearLayout)findViewById(R.id.newsLinear);
        bookLinearLayout=(LinearLayout)findViewById(R.id.bookLinear);
    }
}

Create FragmantNews class in project.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import refresh.swipe.com.designdemo.R;

/**
 * Created by Laptop on 04-07-2016.
 */
public class FragmentNews extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, viewGroup, false);
        return view;
    }
}

Create FragmantBook class in project.


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import refresh.swipe.com.designdemo.R;

/**
 * Created by Laptop on 04-07-2016.
 */
public class FragmentBook extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup viewGroup, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book, viewGroup, false);
        return view;
    }
}




May this code help you. Thanks!!!!!!

No comments:

Post a Comment