mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 03:39:05 +01:00
Blog Fragment in Navigation Drawer with Tabs
This replaces the custom layouts in the navigation drawer with a `NavigationView` and adds a menu entry for Blogs. A Main Blogs fragment is added that holds a `TabLayout` and a `ViewPager`. Five tabs are already added, but they just have a single placeholder fragment that is to be replaced by the actual fragments. Closes #409
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package org.briarproject.android.blogs;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.design.widget.TabLayout;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v4.app.FragmentStatePagerAdapter;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.fragment.BaseFragment;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class BlogsFragment extends BaseFragment {
|
||||
|
||||
public final static String TAG = BlogsFragment.class.getName();
|
||||
|
||||
// TODO add your first fragment here
|
||||
//@Inject
|
||||
//Lazy<MyBlogsFragment> myBlogsFragment;
|
||||
|
||||
private static final String SELECTED_TAB = "selectedTab";
|
||||
private TabLayout tabLayout;
|
||||
|
||||
@Inject
|
||||
public BlogsFragment() {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
View v = inflater.inflate(R.layout.fragment_blogs, container,
|
||||
false);
|
||||
|
||||
tabLayout = (TabLayout) v.findViewById(R.id.tabLayout);
|
||||
ViewPager viewPager = (ViewPager) v.findViewById(R.id.pager);
|
||||
|
||||
String[] titles = {
|
||||
getString(R.string.blogs_feed),
|
||||
getString(R.string.blogs_my_blogs),
|
||||
getString(R.string.blogs_blog_list),
|
||||
getString(R.string.blogs_available_blogs),
|
||||
getString(R.string.blogs_drafts)
|
||||
};
|
||||
TabAdapter tabAdapter =
|
||||
new TabAdapter(getChildFragmentManager(), titles);
|
||||
viewPager.setAdapter(tabAdapter);
|
||||
tabLayout.setupWithViewPager(viewPager);
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
int position = savedInstanceState.getInt(SELECTED_TAB, 0);
|
||||
viewPager.setCurrentItem(position);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putInt(SELECTED_TAB, tabLayout.getSelectedTabPosition());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
|
||||
private static class TabAdapter extends FragmentStatePagerAdapter {
|
||||
private String[] titles;
|
||||
|
||||
TabAdapter(FragmentManager fm, String[] titles) {
|
||||
super(fm);
|
||||
this.titles = titles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return titles.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Fragment getItem(int position) {
|
||||
switch (position) {
|
||||
// TODO add your fragments here
|
||||
default:
|
||||
return MyBlogsFragment.newInstance(position);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getPageTitle(int position) {
|
||||
return titles[position];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package org.briarproject.android.blogs;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.briarproject.R;
|
||||
import org.briarproject.android.fragment.BaseFragment;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class MyBlogsFragment extends BaseFragment {
|
||||
|
||||
public final static String TAG = MyBlogsFragment.class.getName();
|
||||
|
||||
@Inject
|
||||
public MyBlogsFragment() {
|
||||
}
|
||||
|
||||
static MyBlogsFragment newInstance(int num) {
|
||||
MyBlogsFragment f = new MyBlogsFragment();
|
||||
|
||||
Bundle args = new Bundle();
|
||||
args.putInt("num", num);
|
||||
f.setArguments(args);
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
||||
View v = inflater.inflate(R.layout.fragment_blogs_my, container,
|
||||
false);
|
||||
|
||||
TextView numView = (TextView) v.findViewById(R.id.num);
|
||||
String num = String.valueOf(getArguments().getInt("num"));
|
||||
numView.setText(num);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUniqueTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user