MainActivity.java
public class MainActivity extends Activity {
private Fragment mLandingAilsesFrag;
private Fragment mDetailsFragment;
private boolean mIsDetailsFragmentAdded;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_fragment);
getActionBar().setHomeButtonEnabled(true);
mLandingAilsesFrag = new CardFragement();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, mLandingAilsesFrag).commit();
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
if(mIsDetailsFragmentAdded){
removeDetailsFragement();
} else {
addDetailsFragment();
}
break;
}
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
if(mIsDetailsFragmentAdded){
removeDetailsFragement();
return true;
}else {
return super.onKeyUp(keyCode, event);
}
}
return super.onKeyUp(keyCode, event);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void addDetailsFragment() {
mIsDetailsFragmentAdded = true;
mDetailsFragment = new DetailsCardViewFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(R.id.content_frame, mDetailsFragment);
transaction.commit();
}
public void removeDetailsFragement() {
if (mIsDetailsFragmentAdded) {
mIsDetailsFragmentAdded = false;
FragmentManager fragmentManager = getFragmentManager();
//fm.beginTransaction().remove(mDetailsFragment).commit();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.remove(mDetailsFragment);
transaction.commit();
}
mIsDetailsFragmentAdded = false;
FragmentManager fragmentManager = getFragmentManager();
//fm.beginTransaction().remove(mDetailsFragment).commit();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.remove(mDetailsFragment);
transaction.commit();
}
}
}
activity_main_fragment.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_farment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.googlenowcard.CustomFrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
android:id="@+id/main_farment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.googlenowcard.CustomFrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
CardFragement.java
public class CardFragement extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, null);
//inflate the layout and return the View.
return view;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, null);
//inflate the layout and return the View.
return view;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_bottom_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text= "fragmentOne">
</TextView>
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_bottom_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text= "fragmentOne">
</TextView>
</RelativeLayout>
DetailsCardViewFragment:
public class DetailsCardViewFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_details, null);
//inflate the layout and return the View.
return view;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_details, null);
//inflate the layout and return the View.
return view;
}
}
activity_details.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_bottom_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text= "fragmentOne">
</TextView>
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_bottom_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text= "fragmentOne">
</TextView>
</RelativeLayout>
slide_in_left.xml:
create one xml file in res/animator folder.
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="1280"
android:valueTo="0"
android:duration="500"/>
</set>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="1280"
android:valueTo="0"
android:duration="500"/>
</set>
slide_out_right.xml:
create one xml file in res/animator folder.
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="1280"
android:duration="500"/>
</set>
Note: for th alternative clicks details fragment added and removed and fragment comes fromt the right side.
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="1280"
android:duration="500"/>
</set>
Note: for th alternative clicks details fragment added and removed and fragment comes fromt the right side.