简体   繁体   English

在ViewPager中,在滑动到第3个选项卡时,tab1中的子片段消失

[英]In ViewPager, on sliding to 3rd tab, child fragment in tab1 disappears

I have 3 tabs in a FragmentStatePagerAdapter. 我在FragmentStatePagerAdapter中有3个选项卡。 I have a fragment for tab 1 which adds views to its 'linearlayout' container inside a relative layout dynamically. 我有一个标签1的片段,它动态地在相对布局中为其'linearlayout'容器添加视图。 i add dynamically some views in tab 1 fragment. 我在tab 1片段中动态添加一些视图。 I also added a complete fragment in that linearLayout container with other views up and down. 我还在该linearLayout容器中添加了一个完整的片段,其中包含其他视图。 this works fine.. every thing fine. 这很好..每件事都很好。

but when i go to 3rd page and come back. 但是当我去第3页然后回来。 all views are there. 所有观点都在那里。 but that complete fragment is vanished~~ EDIT 但那个完整的片段消失了~~ 编辑
It vanishes from tab1 and appears below the views in tab2 where another childfragment is added.. 它从tab1消失,并显示在tab2中的视图下方,其中添加了另一个子片段。

First of all i have a listFragment in tab1 and tab2. 首先,我在tab1和tab2中有一个listFragment。 based on listItem click i load a fragment in each tab, that both fragments in tab1 and 2 inflates the same below xml and adds dynamic views, but in some case i add a complete fragment instead of a view in the linear layout given in xml. 基于listItem单击我在每个选项卡中加载一个片段,tab1和2中的两个片段在xml下面膨胀相同并添加动态视图,但在某些情况下,我在xml中给出的线性布局中添加一个完整的片段而不是视图。 Problem arose when i have to add two fragments in both new added fragments in tab 1 and tab2, after there were only lists, and slided to tab3. 问题出现了,当我必须在这两个新增加的片段选项卡1和TAB2添加两个片段,之后有只列出,并滑到TAB3。 on returning back. 回来了。 fragment in 'tab1 main fragment' vanished and appeared below the fragment in 'tab2 main fragment' 'tab1主片段'中的片段消失并出现在'tab2主片段'中的片段下方

this is the viewpager, in which i change different fragments by method setItem(fragment, position): 这是viewpager,我通过方法setItem(片段,位置)更改不同的片段:

public static class ScreenSlidePagerAdapter extends
        FragmentStatePagerAdapter {

    public static ArrayList<Fragment> tabs_fragments = new ArrayList<Fragment>(
            3);

    public ScreenSlidePagerAdapter(FragmentManager fm,
            ArrayList<Fragment> tabs) {
        super(fm);
        ScreenSlidePagerAdapter.tabs_fragments = tabs;
    }

    @Override
    public Fragment getItem(int arg0) {
        return tabs_fragments.get(arg0);
    }

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

    @Override
    public int getItemPosition(Object object) {
        if (tabs_fragments.contains(object)) {
            return POSITION_UNCHANGED;
        }
        return POSITION_NONE;
    }

    public static void setItem(Fragment fr, int position) {
        if (position <= 2 && position >= 0) {
            tabs_fragments.remove(position);
            tabs_fragments.add(position, fr);
        } else
            Log.d("adding tab", "wrong tab position to add fragment at");
    }
}

this is how i add my fragment to linearlayout where 'll' is the linear layout inside the relative layout, that is the actual contentview for the fragment on tab1 这就是我将我的片段添加到linearlayout的方式,其中'll'是相对布局中的线性布局,即tab1上片段的实际内容视图

ll.addView(getTitle("Set Message Receive Settings"));
        // View smsRecLayout = inflater.inflate(R.layout.fr_ev_sms_receive,
        // container, false);
        Bundle b = new Bundle();
        b.putInt("id", eventId);
        b.putInt("event", eventTypeId);
        Fragment fr = new FrEv_SMSReceive();
        fr.setArguments(b);
        getFragmentManager().beginTransaction()
                .add(ll.getId(), fr, "fr_sms_receive").commit();

this is the layout of fragment where another fragment is added in linear layout 'fr_event_container'. 这是片段的布局,其中另一个片段以线性布局'fr_event_container'添加。 FragmentStatePagerAdapter FragmentStatePagerAdapter

<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=".EventDetails" >

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:gravity="center_horizontal"
    android:padding="10dp"
    android:textIsSelectable="false" />

<ScrollView
    android:id="@+id/fr_event_container_scroll"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/sep1"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/title" >

    <LinearLayout
        android:id="@+id/fr_event_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

<View
    android:id="@+id/sep1"
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_above="@+id/addOther"
    android:layout_centerHorizontal="true"
    android:layout_margin="10dp"
    android:background="#000"
    android:padding="10dp"
    android:visibility="gone" />

<Button
    android:id="@+id/next"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/addOther"
    android:layout_alignBottom="@+id/addOther"
    android:layout_alignParentRight="true"
    android:layout_marginRight="54dp"
    android:text="Next"
    android:width="90dp" />

The ViewPager has a maximum offscreen page limit. ViewPager具有最大屏幕外页面限制。 You can set it higher with setOffscreenPageLimit(int) 您可以使用setOffscreenPageLimit(int)将其设置得更高

I have come to know that the problem was in using the same layout for both tabs without changing its Id at runtime by setId ; 我已经知道问题在于为两个选项卡使用相同的布局而不在运行时通过setId更改其Id;

Although the fragments were in different tabs but Android was treating the layout that was first added as main, so if I added a fragment in tab2 after tab1 then any thing added in tab2 or tab2 version of that layout was going in tab1(first added). 虽然片段在不同的选项卡中,但是Android正在处理首次添加为main的布局,所以如果我在tab1之后在tab2中添加了一个片段,那么在tab1或tab2版本中添加的任何内容都会在tab1中进行(首先添加) 。

I changed layoutIds at runtime and problem is solved. 我在运行时更改了layoutIds,问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM