如何以编程方式删除不是LinearLayout的FragmentStatePagerAdapter正在使用的Fragment中的任何视图?

时间:2018-07-02 22:52:54

标签: android android-fragments android-viewpager

我有一个相对的布局,可以通过编程方式添加TextView,但是我还需要能够最终删除所有这些视图,同时保持LinearLayouts完整,这有可能吗?

此代码中的xml是FragmentStatePageAdapter的片段。我一直在尝试找到一种方法,以在用户滚动时添加和删除视图,类似于Google日历的功能。下面的代码显示了我先前修复该解决方案的尝试。 RelativeLayout将添加到scrollView内,而TextViews将以编程方式添加到onCreateView中。当前的实现大部分情况下都有效,但是向后滑动一个视图时无法正常工作(LinearLayout的顶部栏被删除,而不是onCreateView中创建的按钮。我的最佳猜测是未调用BringChildToFront()方法,但我不确定为什么。

我还试图在onPause()和onResume()中实现此代码,从而完全放弃了功能。

因此,我想在onCreateView中使用pageChangeListener删除任何不是LinearLayout的视图,然后调用要显示的新视图。我应该提到的是,我仅使用按钮测试代码,并计划以后用textViews替换它们,以便从数据库中收集数据。

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(正确的功能代码)

 @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;
}

2 个答案:

答案 0 :(得分:1)

一种更简单的方法可能是将TextViews添加到ArrayList

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

如果要以编程方式添加它们,则只需添加一行代码即可。

调用此函数以删除此ArrayList中的所有视图:

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

答案 1 :(得分:0)

哪一个linearlayout是父布局,而总体上来说,如果要删除父布局中的所有子布局(真实或线性),则可以得到参考并使用removeAllViews() check this link