M
M
Michael Kim2016-08-22 22:38:18
Java
Michael Kim, 2016-08-22 22:38:18

Why is Fragment not showing when using ViewPager?

I want to implement tabs with fragments. I use viewpager, there are no errors in the logs, but fragments are not shown, although if you just add one fragment to the activity, everything is fine.
ViewPager adapter

public 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];
    }
}

Fragment
public 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);
                        }
                );

    }
}

Initialization in onCreate activity
pager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
pager.setAdapter(new FeedPagerAdapter(getSupportFragmentManager(), MainActivity.this));
tabLayout.setupWithViewPager(pager);

XML that is included in the activity's 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>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2016-08-23
@Marox

<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">

RelativeLayout knows nothing about weights. It just gives you a height of 0.

D
davidnum95, 2016-08-23
@davidnum95

Try writing this in onCreateView:

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
mRvQs.setLayoutManager(llm);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question