Last active
July 13, 2020 12:07
-
-
Save yunusemredilber/50c3ccbb7c080847ca3f25d9d40da257 to your computer and use it in GitHub Desktop.
Multiple turbolinks view with bottom navigation bar [Android]
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"?> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
tools:context=".MultipleTurbolinksViewActivity"> | |
<androidx.viewpager2.widget.ViewPager2 | |
android:id="@+id/view_pager" | |
android:layout_width="match_parent" | |
android:layout_height="0dp" | |
app:layout_constrainedHeight="true" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintBottom_toTopOf="@id/bottom_navigation" /> | |
<com.google.android.material.bottomnavigation.BottomNavigationView | |
app:layout_constraintBottom_toBottomOf="parent" | |
android:id="@+id/bottom_navigation" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:labelVisibilityMode="unlabeled" | |
app:menu="@menu/bottom_navigation_menu" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
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
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<com.basecamp.turbolinks.TurbolinksView | |
android:id="@+id/view_turbolinks" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</FrameLayout> |
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
/* | |
implementation 'androidx.viewpager2:viewpager2:1.1.0-alpha01' | |
implementation 'com.google.android.material:material:1.1.0-alpha08' | |
*/ | |
public class MultipleTurbolinksViewActivity extends BaseActivity { | |
private ActivityMultipleTurbolinksViewBinding binding; | |
private String[] paths = new String[]{"/", "/profile"}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
binding = ActivityMultipleTurbolinksViewBinding.inflate(getLayoutInflater()); | |
setContentView(binding.getRoot()); | |
MultipleTurbolinksViewAdapter multipleTurbolinksViewAdapter = | |
new MultipleTurbolinksViewAdapter( | |
getSupportFragmentManager(), | |
getLifecycle(), paths); | |
binding.viewPager.setAdapter(multipleTurbolinksViewAdapter); | |
binding.viewPager.setUserInputEnabled(false); // To disable swipe | |
binding.bottomNavigation.setOnNavigationItemSelectedListener(item -> { | |
switch (item.getItemId()) { | |
case R.id.home: | |
binding.viewPager.setCurrentItem(0, true); | |
return true; | |
case R.id.profile: | |
binding.viewPager.setCurrentItem(1, true); | |
return true; | |
default: | |
return false; | |
} | |
}); | |
} | |
@Override | |
protected void onRestart() { | |
super.onRestart(); | |
refreshCurrentFragment(); | |
} | |
@Override | |
protected void refresh() { | |
// Cold boot and refresh all fragments. | |
for (int i = 0; i < paths.length; i++) { | |
Fragment fragment = getSupportFragmentManager().findFragmentByTag("f" + i); | |
if(!(fragment instanceof TurbolinksViewFragment)) continue; | |
((TurbolinksViewFragment) fragment).resetToColdBoot(); | |
((TurbolinksViewFragment) fragment).visitCurrentLocation(true); | |
} | |
} | |
private void refreshCurrentFragment() { | |
Fragment currentFragment = getSupportFragmentManager().findFragmentByTag("f" + binding.viewPager.getCurrentItem()); | |
if(!(currentFragment instanceof TurbolinksViewFragment)) return; | |
((TurbolinksViewFragment) currentFragment).visitCurrentLocation(true); | |
} | |
} |
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
public class MultipleTurbolinksViewAdapter extends FragmentStateAdapter { | |
private String[] paths; | |
public MultipleTurbolinksViewAdapter(@NonNull FragmentManager fragmentManager, | |
@NonNull Lifecycle lifecycle, | |
String[] paths) { | |
super(fragmentManager, lifecycle); | |
this.paths = paths; | |
} | |
@NonNull | |
@Override | |
public Fragment createFragment(int position) { | |
return TurbolinksViewFragment.newInstance(paths[position]); | |
} | |
@Override | |
public int getItemCount() { | |
return paths.length; | |
} | |
} |
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
public class TurbolinksViewFragment extends Fragment { | |
private static final String PATH = "path"; | |
private String path; | |
private FragmentTurbolinksViewBinding binding; | |
private TurbolinksSession customTurbolinksSession; | |
public TurbolinksViewFragment() { | |
// Required empty public constructor | |
} | |
/** | |
* Use this factory method to create a new instance of | |
* this fragment using the provided parameters. | |
* | |
* @param path Path to visit. | |
* @return A new instance of fragment TurbolinksViewFragment. | |
*/ | |
public static TurbolinksViewFragment newInstance(String path) { | |
TurbolinksViewFragment fragment = new TurbolinksViewFragment(); | |
Bundle args = new Bundle(); | |
args.putString(PATH, path); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (getArguments() != null) { | |
path = getArguments().getString(PATH); | |
} | |
} | |
@Override | |
public View onCreateView(@NonNull LayoutInflater inflater, | |
ViewGroup container, | |
Bundle savedInstanceState) { | |
binding = FragmentTurbolinksViewBinding.inflate(inflater, container, false); | |
Activity activity = getActivity(); | |
if(activity == null) { | |
Log.d("NULL", "TurbolinksViewFragment - onCreateView - getActivity is null"); | |
return null; | |
} | |
// Setup a new turbolinks session | |
customTurbolinksSession = TurbolinksSession.getNew(activity); | |
setupTurbolinksSession(customTurbolinksSession); | |
visitCurrentLocation(false); | |
return binding.getRoot(); | |
} | |
public void resetToColdBoot() { | |
customTurbolinksSession.resetToColdBoot(); | |
} | |
public void visitCurrentLocation(boolean restoreWithCachedSnapshot) { | |
Helper.turbolinksVisit(getActivity(), customTurbolinksSession, | |
binding.viewTurbolinks, Constants.BASE_URL + path, restoreWithCachedSnapshot); | |
} | |
/* Not required */ | |
private void setupTurbolinksSession(TurbolinksSession turbolinksSession) { | |
turbolinksSession.setPullToRefreshEnabled(false); | |
turbolinksSession.getWebView().addJavascriptInterface(JsBridge.getInstance(), "AndroidBridge"); | |
turbolinksSession.getWebView().setWebChromeClient(CustomChromeClient.getInstance()); | |
turbolinksSession.getWebView().getSettings() | |
.setUserAgentString(Helper.getUserAgentString(getActivity())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment