Хочу реализовать табы с фрагментами. Использую viewpager, в логах ошибок нет, но фрагменты не показываются, хотя если просто добавить один фрагмент в активнвность - все нормально.
ViewPager adapterpublic class FeedPagerAdapter extends FragmentPagerAdapter {
Context ctx;
final String[] tabTitles = new String[]{"Новые", "Все"};
final int count = 2;
public FeedPagerAdapter(FragmentManager fm, Context context) {
super(fm);
ctx = context;
}
@Override
public int getCount() {
return count;
}
@Override
public FeedFragment getItem(int position) {
Toast.makeText(ctx, position + "", Toast.LENGTH_SHORT).show();
FeedFragment fragment = null;
switch (position) {
case 0:
default:
fragment = FeedFragment.newInstance("new");
break;
case 1:
fragment = FeedFragment.newInstance("all");
break;
}
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
Fragmentpublic class FeedFragment extends android.support.v4.app.Fragment {
SharedPreferences userData;
String category;
RecyclerView mRvQs;
SwipeRefreshLayout mSwipeRefreshLayout;
ProgressBar mProgressQuestions;
public static FeedFragment newInstance(final String category) {
final FeedFragment fragment = new FeedFragment();
final Bundle arguments = new Bundle();
arguments.putString("cat", category);
fragment.setArguments(arguments);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
category = getArguments().getString("cat");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
userData = getActivity().getSharedPreferences("userData", getActivity().MODE_PRIVATE);
View view = inflater.inflate(R.layout.feed_fragment, container, false);
mRvQs = (RecyclerView) view.findViewById(R.id.questions_list);
mRvQs.setNestedScrollingEnabled(false);
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refreshQs);
mSwipeRefreshLayout.setRefreshing(false);
mSwipeRefreshLayout.setOnRefreshListener(() -> getQuestionFeed());
getQuestionFeed();
mProgressQuestions = (ProgressBar) view.findViewById(R.id.progressQuestion);
return view;
}
void getQuestionFeed() {
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
mRvQs.setLayoutManager(llm);
Observable<ArrayList<UsualQ>> qs = Observable.create(
(Observable.OnSubscribe<ArrayList<UsualQ>>) subscriber -> {
Collection c;
ArrayList<UsualQ> cq;
if(category.equals("new")) {
c = new Collection();
// check SharedPref, if not => load from net
Integer id = userData.getInt("id", -1);
if (id == -1)
id = Profile.getIdWithCookies(userData.getString("cookies", ""));
cq = c.collectQs(id);
}else {
c = new Collection("all");
cq = c.collectQs();
}
subscriber.onNext(cq);
});
qs
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(e -> e.printStackTrace())
.subscribe(cq -> {
mRvQs.setAdapter(new QuestionAdapter(cq, getActivity()));
mSwipeRefreshLayout.setRefreshing(false);
mProgressQuestions.setVisibility(View.GONE);
}
);
}
}
Инициализация в onCreate activitypager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
pager.setAdapter(new FeedPagerAdapter(getSupportFragmentManager(), MainActivity.this));
tabLayout.setupWithViewPager(pager);
XML, который инклюдится в layout активити
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root_main"
android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white">
</android.support.v4.view.ViewPager>
</RelativeLayout>