Add ExploreTabFragment and SavedArticlesTabFragment to the WikivoyageExploreDialogFragment

This commit is contained in:
Alex Sytnyk 2018-04-05 20:37:23 +03:00
parent 2c4aa1afba
commit 82c57f9b49
3 changed files with 72 additions and 2 deletions

View file

@ -0,0 +1,7 @@
package net.osmand.plus.wikivoyage;
import net.osmand.plus.base.BaseOsmAndFragment;
public class ExploreTabFragment extends BaseOsmAndFragment {
}

View file

@ -0,0 +1,7 @@
package net.osmand.plus.wikivoyage;
import net.osmand.plus.base.BaseOsmAndFragment;
public class SavedArticlesTabFragment extends BaseOsmAndFragment {
}

View file

@ -5,21 +5,33 @@ import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView; import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.BottomNavigationView.OnNavigationItemSelectedListener;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.plus.LockableViewPager;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.wikivoyage.search.WikivoyageSearchDialogFragment; import net.osmand.plus.wikivoyage.search.WikivoyageSearchDialogFragment;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragment { public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragment {
public static final String TAG = "WikivoyageExploreDialogFragment"; public static final String TAG = "WikivoyageExploreDialogFragment";
private static final int EXPLORE_POSITION = 0;
private static final int SAVED_ARTICLES_POSITION = 1;
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -37,10 +49,34 @@ public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragmen
} }
}); });
ColorStateList navColorStateList = createBottomNavColorStateList(); final LockableViewPager viewPager = (LockableViewPager) mainView.findViewById(R.id.view_pager);
BottomNavigationView bottomNav = (BottomNavigationView) mainView.findViewById(R.id.bottom_navigation); viewPager.setOffscreenPageLimit(2);
viewPager.setSwipeLocked(true);
viewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
final ColorStateList navColorStateList = createBottomNavColorStateList();
final BottomNavigationView bottomNav = (BottomNavigationView) mainView.findViewById(R.id.bottom_navigation);
bottomNav.setItemIconTintList(navColorStateList); bottomNav.setItemIconTintList(navColorStateList);
bottomNav.setItemTextColor(navColorStateList); bottomNav.setItemTextColor(navColorStateList);
bottomNav.setOnNavigationItemSelectedListener(new OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int position = -1;
switch (item.getItemId()) {
case R.id.action_explore:
position = EXPLORE_POSITION;
break;
case R.id.action_saved_articles:
position = SAVED_ARTICLES_POSITION;
break;
}
if (position != -1 && position != viewPager.getCurrentItem()) {
viewPager.setCurrentItem(position);
return true;
}
return false;
}
});
return mainView; return mainView;
} }
@ -60,4 +96,24 @@ public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragmen
return false; return false;
} }
} }
private static class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragments = new ArrayList<>();
ViewPagerAdapter(FragmentManager fm) {
super(fm);
fragments.addAll(Arrays.asList(new ExploreTabFragment(), new SavedArticlesTabFragment()));
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
}
} }