Search code examples
androidscrollviewandroid-recyclerview

Android UI bugs with recyclerview and flipper


I have a fragment. Inside this fragment I have declared one flipper and one horizontal recyclerview. I got a problems and don't know how to solve them.

When I open the app everything looks good screenshot link, but when I rotate screen to landscape I see a visual bug for the flipper screenshot link, but when I rotate it back to portrait already see for the recyclerview already screenshot link.

HomeFragment.class

public class HomeFragment extends RoboFragment {
    @InjectView(R.id.viewFlipper)
    ViewFlipper mViewFlipper;
    ArrayList<Category> categoriesList;
    private View rootView;
    private GestureDetector mGestureDetector;
    private int[] mResources = {
            R.mipmap.temp_screen_1,
            R.mipmap.temp_screen_2,
            R.mipmap.temp_screen_3,
            R.mipmap.temp_screen_4
    };

    public void createCategories() {

        categoriesList = new ArrayList<>();
        categoriesList.add(new Category("Android", R.mipmap.icon_android));
        categoriesList.add(new Category("IOS", R.mipmap.icon_ios));
        categoriesList.add(new Category("Windows Phone", R.mipmap.icon_windows_phone));
        categoriesList.add(new Category("Blackberry", R.mipmap.icon_blackberry));
        categoriesList.add(new Category("Budget phones", R.mipmap.icon_phone));
        categoriesList.add(new Category("Tablets", R.mipmap.icon_tablet));
        categoriesList.add(new Category("Jewelry & Watch", R.mipmap.icon_watch));
        categoriesList.add(new Category("Health & Beauty", R.mipmap.icon_umbrella));
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_home, container, false);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Utils.showToolbar(getActivity());

        setSlideShow();
        setCategories();

    }

    private void setCategories() {
        createCategories();

        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(true);
        HorizontalRecycleViewAdapter adapter = new HorizontalRecycleViewAdapter(getActivity(), categoriesList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
    }

    private void setSlideShow() {
        for (int mResource : mResources) {
            ResizableImageView imgageView = new ResizableImageView(getActivity());
            imgageView.setImageResource(mResource);
            mViewFlipper.addView(imgageView);
        }

        mViewFlipper.setInAnimation(getActivity(), android.R.anim.fade_in);
        mViewFlipper.setOutAnimation(getActivity(), android.R.anim.fade_out);

        CustomGestureDetector customGestureDetector = new CustomGestureDetector(mViewFlipper);
        mGestureDetector = new GestureDetector(getActivity(), customGestureDetector);

        mViewFlipper.setAutoStart(true);
        mViewFlipper.setFlipInterval(5000);
    }
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <ViewFlipper
            android:id="@+id/viewFlipper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v7.widget.RecyclerView
            android:layout_below="@+id/viewFlipper"
            android:id="@+id/my_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scrollbars="none" />
    </RelativeLayout>

</ScrollView>

MainActivity.class

@ContentView(R.layout.activity_main)
public class MainActivity extends RoboActionBarActivity {

    @InjectView(R.id.tool_bar)
    Toolbar toolbar;

    SlidingMenu menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setToolBar();
        setSlideMenu();

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new SplashFragment())
                    .commit();
        } else {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new HomeFragment())
                    .commit();
        }
    }

    private void setToolBar() {
        setSupportActionBar(toolbar);
    }

    private void setSlideMenu() {
        menu = new SlidingMenu(this);
        menu.setMenu(R.layout.slidemenu);
        menu.setMode(SlidingMenu.LEFT);
        menu.setSlidingEnabled(false);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setBehindWidthRes(R.dimen.slidingmenu_behind_width);// Setting toolbar as the ActionBar with setSupportActionBar() call
    }

    public void toggleSlidingMenu(View view) {
        menu.toggle();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar" />
    <FrameLayout
        android:layout_below="@+id/tool_bar"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".activities.MainActivity"
        tools:ignore="MergeRootFrame" />
</RelativeLayout>

Solution

  • It seems like you're adding your fragment multiple times, there is no bugs, you just have the same view twice, if you change the orientation back to portrait you have 3...and so on.

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new SplashFragment())
                .commit();
    } else {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new HomeFragment()) // <- here
                .commit();
    }
    

    Use replace or check if the fragment is already added.