blipinsk / FlippableStackView

An Android library introducing a stack of Views with the first item being flippable.
Apache License 2.0
809 stars 150 forks source link

[SOLVED] FlippableStackView doesn't is showing in multiple fragments #9

Closed adrielcafe closed 8 years ago

adrielcafe commented 8 years ago

Hi @blipinsk! First of all, congrats for this great lib :)

So, I have this layout below. The first 3 tabs must show the same FlippableStackView layout, but with different content. Each tab has a different Fragment instance that handles an FlippableStackView separately.

Problem is: only first tab is showing FlippableStackView layout. It's like only one instance of FlippableStackView could exist.

More strange part is: if I put the second or third tab as first tab, FlippableStackView will init normally on that tab, but on the other tabs will not.

Layout and classes are below.

Thanks in advance!

2015-11-19 20 49 26

_activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            app:tabMode="fixed"
            app:tabIndicatorColor="@android:color/white"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/main_bg"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

_fragmentstack.xml

<com.bartoszlipinski.flippablestackview.FlippableStackView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/stack"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/main_bg" />

FeedFragment.java (that handles FlippableStackView)

public class FeedFragment extends Fragment {
    private List<Moment> moments = new ArrayList<>();
    private List<FeedItemFragment> momentsFrag = new ArrayList<>();

    @Bind(R.id.stack)
    FlippableStackView stackView;

    public FeedFragment() {

    }

    public static FeedFragment newInstance(List<Moment> moments) {
        Bundle args = new Bundle();
        args.putSerializable(Constant.EXTRA_MOMENTS, moments);
        FeedFragment frag = new FeedFragment();
        frag.setArguments(args);
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        moments = (List<Moment>) getArguments().getSerializable(Constant.EXTRA_MOMENTS);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_feed, container, false);
        ButterKnife.bind(this, rootView);
        updateMoments();
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void updateMoments(){
        for (Moment moment : moments) {
            momentsFrag.add(FeedItemFragment.newInstance(moment));
        }
        FeedAdapter feedAdapter = new FeedAdapter(getFragmentManager());
        stackView.setAdapter(feedAdapter);
        stackView.initStack(4);
    }

    private class FeedAdapter extends FragmentStatePagerAdapter {

        public FeedAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return momentsFrag.get(position);
        }

        @Override
        public int getCount() {
            return momentsFrag.size();
        }

    }
}
blipinsk commented 8 years ago

Hi @adrielcafe! Can you also show the way you're setting up your ViewPager inside your main Activity? As I can see it now, it's fragment-related issue. I might try to recreate your issue later today.

adrielcafe commented 8 years ago

Yes, below is my MainActivity. As you can see, I create 3 different instances of FeedFragment.

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.coordinator_layout)
    CoordinatorLayout coordinatorLayout;
    @Bind(R.id.viewpager)
    ViewPager viewPager;
    @Bind(R.id.tabs)
    TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();
    }

    private void init(){
        TabPagerAdapter tabPagerAdapter = new TabPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(tabPagerAdapter);
        viewPager.setOffscreenPageLimit(4);
        viewPager.setPageTransformer(true, new FadePageTransformer());
        tabLayout.setupWithViewPager(viewPager);
    }

    private class TabPagerAdapter extends FragmentPagerAdapter {

        public TabPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return FeedFragment.newInstance(DbUtil.friendsFeed);
                case 1:
                    return FeedFragment.newInstance(DbUtil.profileFeed);
                case 2:
                    return FeedFragment.newInstance(DbUtil.favoritesFeed);
                case 3:
                    return new FriendsFragment();;
                case 4:
                    return new SettingsFragment();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 5;
        }

    }

}
adrielcafe commented 8 years ago

@blipinsk, problem solved!

Seems that the problem was related to fragment tags and FeedAdapter (that extends FragmentStatePagerAdapter). When I started to using CWAC-Pager the problem was solved.

private class FeedAdapter extends ArrayPagerAdapter<FeedItemFragment> {
    public PagerAdapter(FragmentManager fragmentManager, ArrayList<PageDescriptor> descriptors) {
        super(fragmentManager, descriptors);
    }

    @Override
    protected FeedItemFragment createFragment(PageDescriptor desc) {
        return FeedItemFragment.newInstance(desc.getFragmentTag(), type);
    }
}

2015-11-22 17 17 51

Thanks for your help. Once again, this library is amazing!

blipinsk commented 8 years ago

Cool, glad you solved it. The stack looks nicely in your app. :+1: Cheers!