Search code examples
androidandroid-fragmentsandroid-viewpager

How to programatically remove any view in a Fragment being utilized by a FragmentStatePagerAdapter that is not a LinearLayout?


I have a relative layout in which I am programatically adding TextViews, but I also need the ability to eventually remove all these views while keeping the LinearLayouts in tact, is this possible?

The xml in this code is a fragment for a FragmentStatePageAdapter. I have been trying to find a way to add and remove views as the user scrolls similar to the functionality of Google Calendar. The code below shows my previous attempt at fixing the solution. The RelativeLayout would be added inside the scrollView and TextViews would be added programatically in onCreateView. This current implementation works for the most part but fails to work correctly upon swiping one view back (The top bar in the LinearLayout is removed rather than the button created in onCreateView. My best guess is that the bringChildToFront() method is not being called, but I'm not sure why.)

I have also tried to implement this code in onPause() and onResume() which throws off the functionality entirely.

Thus, I would like to use the pageChangeListener in onCreateView to remove any view that is not a LinearLayout and then call in the new views being displayed. I should mention I am only testing the code with buttons and plan on replacing them with textViews gathering data from a database later.

single_day_content.xml

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <!--This LinearLayout creates all the hours on main activity-->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                style="@style/primaryHour"
                android:text="@string/one_am"
                android:layout_marginTop="40dp" />

            <TextView
                style="@style/primaryHour"
                android:text="@string/two_am"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/three_am"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/four_am"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/five_am"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/six_am"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/seven_am"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/eight_am"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/nine_am"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/ten_am"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/eleven_am"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/twelve_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/one_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/two_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/three_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/four_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/five_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/six_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/seven_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/eight_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/nine_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/ten_pm"/>

            <TextView
                style="@style/primaryHour"
                android:text="@string/eleven_pm"
                android:layout_marginBottom="40dp"/>

        </LinearLayout>

        <!--This LinearLayout creates all the lines next to hours on main activity-->
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:orientation="vertical"
            android:id="@+id/timeBars">

            <View
                style="@style/primaryHourLines"
                android:layout_marginTop="79dp">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines">
            </View>

            <View
                style="@style/primaryHourLines"
                android:layout_marginBottom="79dp">
            </View>

        </LinearLayout>

    </LinearLayout>

</ScrollView>

DaySliderFragment (CORRECT FUNCTIONAL CODE)

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

    ViewPager viewPager = getActivity().findViewById(R.id.pager);
    ArrayList<Button> viewsList = new ArrayList<Button>();
    Button testBut = new Button(getContext());

    //pageChangeListener to update 
    viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){

        Boolean first = true;

        //This method allows changes to occur on the first fragment which would not normally occur.
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (first && positionOffset == 0 && positionOffsetPixels == 0){
                onPageSelected(0);
                first = false;
            }
        }

        @Override
        public void onPageSelected(int position){
            LinearLayout timeBars = (LinearLayout) rootView.findViewById(R.id.timeBars);

            for(Button btn : viewsList) {
                timeBars.removeView(btn);
            }
            for(Button btn : viewsList) {
                viewsList.remove(btn);
            }

            timeBars.addView(testBut);
            viewsList.add(testBut);
        }
    });

    return rootView;
}

Solution

  • An easier way might be to add the TextViews to an ArrayList

    private  ArrayList<View> viewsList = new ArrayList<View>();
    viewsList.add(myTextView)
    

    If you are adding them programatically it just adds one line of code.

    Call this function to remove all views in this ArrayList:

    private void deleteViewsInList(ArrayList<View> viewsList)  {
        for(View view : viewsList) {
            myLinearLayout.removeView(View view);
        }
    }