Last active
July 9, 2018 03:01
-
-
Save colabug/38a8210d2c9a4a5d19d6 to your computer and use it in GitHub Desktop.
Static and Dynamic Views
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.app.Fragment; | |
import android.app.FragmentManager; | |
import android.app.FragmentTransaction; | |
import android.os.Bundle; | |
public class DynamicFragmentSwappingActivity extends Activity | |
{ | |
@Override | |
public void onCreate( Bundle savedInstanceState ) | |
{ | |
super.onCreate( savedInstanceState ); | |
setContentView( R.layout.fragment_dynamic_holder); | |
swapFragment( new YourFragmentHere(), false ); | |
} | |
private void swapFragment( Fragment fragment, boolean shouldAddToBackStack ) | |
{ | |
// Get a reference to the fragment manager | |
FragmentManager manager = getFragmentManager(); | |
FragmentTransaction transaction = manager.beginTransaction(); | |
// Start the transaction & specify the holder | |
String tag = fragment.getClass().getSimpleName(); | |
transaction.replace( R.id.fragment_holder, fragment, tag ); | |
// If desired, add to the backstack | |
if ( shouldAddToBackStack ) | |
{ | |
transaction.addToBackStack( tag ); | |
} | |
// Make sure to commit! | |
transaction.commit(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/fragment_holder" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<fragment | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/static_fragment" | |
android:name="com.example.myapp.YourFragmentHere" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<TextView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/welcome_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Welcome!"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.app.Fragment; | |
import android.os.Bundle; | |
public class ReferenceStaticFragmentActivity extends Activity | |
{ | |
@Override | |
public void onCreate (Bundle savedInstanceState) | |
{ | |
super.onCreate( savedInstanceState ); | |
setContentView( R.layout.fragment_statically_defined ); | |
Fragment fragment = getFragmentManager().findFragmentById( R.id.static_fragment ); | |
// Do something with the fragment here | |
} | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Fragment; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
public class YourFragmentHere extends Fragment | |
{ | |
@Override | |
public View onCreateView (LayoutInflater inflater, | |
ViewGroup container, | |
Bundle savedInstanceState) | |
{ | |
View layout = inflater.inflate( R.layout.fragment_yours, | |
container, | |
false ); | |
// Do something with the view here | |
return layout; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really nice one demo
Thanks