Android奇怪的错误:片段Dosen&t;重新创建视图

时间:2015-01-25 18:27:38

标签: android android-layout android-fragments tabs navigation-drawer

这是视频: 的 https://www.youtube.com/watch?v=eush2bY0XlQ&feature=youtu.be&hd=1

所以这是情节:

1)我有一个导航抽屉。

2)通过单击其中一个列表项,标签布局(片段)会膨胀。

3)所以我得到一个标签布局(有6个标签),除了导航抽屉,导航抽屉和tab_layout可以同时使用。

4)每个标签的内容都是不同的拼写。

问题:

当我启动应用程序并单击带有选项卡片段的列表项时,tab_fragment的内容会正常膨胀。

但是当我再次点击它时,第一个和第二个标签的所有内容都会消失。

只有在我向第三个标签滑动并再次向后滑动时再次出现。

简单来说,

On第一次调用Tab_Fragment的静态新Instance方法工作正常。

On再次调用它时,第一个和第二个标签的内容会消失并重新出现

只有当我滑到第3个标签然后回来时才会。

我知道这听起来很奇怪。

代码:

我的Tab_Fragment创建它我从MainActivity调用它的新实例。

public class Tab_Activity extends Fragment {
private final Handler handler = new Handler();
public PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
public final static String TAG = Tab_Activity.class.getSimpleName();

public Tab_Activity() {
    // TODO Auto-generated constructor stub
}

public static Tab_Activity newInstance() {
    return new Tab_Activity();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    return inflater.inflate(R.layout.tab_layout, container, false);


}

@Override
public void onViewCreated(View v, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(v, savedInstanceState);
    tabs = (PagerSlidingTabStrip) v.findViewById(R.id.tabs);
    pager = (ViewPager) v.findViewById(R.id.pager);
    adapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
    pager.setAdapter(adapter);
    final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
            .getDisplayMetrics());
    pager.setPageMargin(pageMargin);
    tabs.setViewPager(pager);
    tabs.setIndicatorColorResource(R.color.grey);
    tabs.setTextColorResource(R.color.black);

}

public class MyPagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = { "  ELECTRONICS  ", "          IT        ", "    COMPUTER    ","     EXTC     ", "INSTRUMENTATION","       MCA      "  };

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

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Fragment getItem(int position) {
        return Courses.newInstance(position);

    }

}



}

课程片段:

public class Courses extends Fragment {
LinearLayout ll ;
private static final String ARG_POSITION = "position";
public static PagerSlidingTabStrip tab ;
private int position;
public static Courses newInstance(int position) {

    Courses f = new Courses();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    position = getArguments().getInt(ARG_POSITION);
    setRetainInstance(true);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.dept_main,container,false);
}

@Override
public void onViewCreated(View v, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(v, savedInstanceState);
    // generate ID's :
    TextView h_name = (TextView) v.findViewById(R.id.tv_dept_name);
    TextView f_info = (TextView) v.findViewById(R.id.tv_dept_info);
    TextView h_vision = (TextView) v.findViewById(R.id.header_vision);
    TextView f_vision = (TextView) v.findViewById(R.id.footer_vision);
    TextView h_mission = (TextView) v.findViewById(R.id.header_mission);
    TextView f_mission = (TextView) v.findViewById(R.id.footer_mission);
    TextView h_eoe = (TextView) v.findViewById(R.id.header_EOE);
    TextView f_eoe = (TextView) v.findViewById(R.id.footer_EOE);
    TextView h_intake = (TextView) v.findViewById(R.id.header_intake);
    TextView f_intake = (TextView) v.findViewById(R.id.footer_intake);
    h_intake.setText(R.string.h_intake);
    h_mission.setText(R.string.h_mission);
    h_vision.setText(R.string.h_vision);
    h_eoe.setText(R.string.h_eoe);



    switch(position){
    case 0 :
        //Electronics 
        h_name.setText("Electronics");
        f_info.setText(R.string.ug_course);
        f_vision.setText(R.string.v_etrx);
        f_mission.setText(R.string.m_etrx);
        f_eoe.setText("1984");
        f_intake.setText(R.string.intake_etrx);
        break ;
    case 1 :
        h_name.setText("Information Technology");
        f_info.setText(R.string.ug_course);
        f_vision.setText(R.string.v_it);
        f_mission.setText(R.string.m_it);
        f_eoe.setText("1984");
        f_intake.setText(R.string.intake_it);
        break ; 
    case 2 :
        h_name.setText("Computer Science");
        f_info.setText(R.string.ug_course);
        f_vision.setText(R.string.v_coms);
        f_mission.setText(R.string.m_coms);
        f_eoe.setText("1984");
        f_intake.setText(R.string.intake_coms);
        break ;
    case 3 :
        h_name.setText("Electronics And Telecommunication");
        f_info.setText(R.string.ug_course);
        f_vision.setText(R.string.v_extc);
        f_mission.setText(R.string.m_extc);
        f_eoe.setText("1984");
        f_intake.setText(R.string.intake_extc);
        break ;
    case 4 :
        h_name.setText("Instrumentation");
        f_info.setText(R.string.ug_course);
        f_vision.setText(R.string.v_it);
        f_mission.setText(R.string.m_it);
        f_eoe.setText("1984");
        f_intake.setText(R.string.intake_it);
        break ;
    case 5 :
        h_name.setText("Master of Computer Applications");
        f_info.setText("Post Graduation Course.(PG)");
        f_vision.setText(R.string.v_mca);
        f_mission.setText(R.string.m_mca);
        f_eoe.setText("1984");
        f_intake.setText(R.string.intake_mca);
        break;
    }



}

}

(评论任何澄清!)

1 个答案:

答案 0 :(得分:0)

找到我的答案==> https://stackoverflow.com/a/12582529/3475933 只将FragmentPagerAdapter更改为FragmentStatePagerAdapter,一切正常。 希望它有所帮助。!