使用后栈时布局显示问题

时间:2016-06-03 11:08:30

标签: android android-fragments back-stack

我在使用android后台时出现问题。 因此,在我的片段1中,然后转到片段2,将片段1添加到后台堆栈。一切都很好。

然后当我在片段2中按下后退按钮并调用popBackStack时,片段1再次显示,此时一切都很好。

然后当我尝试从片段1回到片段2时,我看到的只是一个空白片段。

片段2包含一个动态填充了更多片段的视图寻呼机。

关于导致这种情况的任何想法?

以下是一些应该有帮助的代码示例

onBackPressed调用popBackStack(在容器活动中):

@Override
public void onBackPressed() {

    if (drawerOpen) {
        closeDrawer();
    } else if(fragMan.getBackStackEntryCount() > 0){
        fragMan.popBackStackImmediate();
        return;
    } else {
        finish();
        return;
    }

    super.onBackPressed();
}

片段1被片段2替换(在容器活动中):

transaction.replace(fl_frag_container.getId(), new ViewTicketFragment());
transaction.addToBackStack(null);
transaction.commit();

片段1代码:

public class MyTicketsFragment extends Fragment {

LinearLayout ll_parent_layout;

public MyTicketsFragment() {}


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

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

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

    View view = inflater.inflate(R.layout.fragment_my_tickets, container, false);

    ((HomeActivity) getActivity()).hideMapIcon(View.GONE, true);
    ((HomeActivity) getActivity()).hideSpacerLayout(View.GONE);
    ((HomeActivity) getActivity()).hideSearchIcon(View.INVISIBLE, true);

    ll_parent_layout = (LinearLayout) view.findViewById(R.id.ll_parent_layout);
    makeNetworkCall();


    return view;
}

private void makeNetworkCall(){


    //Onresult
    setupScrollView();
}

private void setupScrollView(){
    TicketObject obj = new TicketObject("This is a ticket", "28th May 2016");

    for(int i = 0; i < 20; i++){
        ll_parent_layout.addView(getScrollItem(obj));
    }

}

private LinearLayout getScrollItem(TicketObject ticket){
    LinearLayout layout = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.ticket_list_item, ll_parent_layout, false);

    ImageView img_cal_icon = (ImageView) layout.findViewById(R.id.img_cal_icon);
    AutofitTextView txt_description = (AutofitTextView) layout.findViewById(R.id.txt_ticket_name);
    AutofitTextView txt_date = (AutofitTextView) layout.findViewById(R.id.txt_ticket_date);

    txt_description.setTypeface(FontClass.getOpenSansRegular(getContext()));
    txt_date.setTypeface(FontClass.getOpenSansLight(getContext()));

    txt_description.setText(ticket.getDescription());
    txt_date.setText(ticket.getDate());


    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            ((HomeActivity) getActivity()).setSecondaryFrag(Constants.VIEW_TICKETS);
        }
    });


    return layout;
}

}

片段2代码:

public class ViewTicketFragment extends Fragment {

private static final String ARG_PARAM1 = "param1";

private ViewPager pager;
private ArrayList<Fragment> fragList;

View view;

private String mParam1;

public ViewTicketFragment() {
    // Required empty public constructor
}

public static ViewTicketFragment newInstance(String param1) {
    ViewTicketFragment fragment = new ViewTicketFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
    }
}

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


    return view;

}

@Override
public void onResume() {
    super.onResume();
    setupPager(view);
}

private void setupPager(View view) {
    fragList = new ArrayList<>();
    fragList.add(new TicketPagerItemFragment());
    fragList.add(new TicketPagerItemFragment());

    pager = (ViewPager) view.findViewById(R.id.pager);

    FragmentPagerAdapter adapter = new FragmentPagerAdapter(getFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            return fragList.get(position);
        }

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

    pager.setAdapter(adapter);

    pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}
}

1 个答案:

答案 0 :(得分:1)

在Fragment中使用ViewPager时,必须使用Fragment.getChildFragmentManager()

替换

new FragmentPagerAdapter(getFragmentManager())

用这个:

new FragmentPagerAdapter(getChildFragmentManager())

应该有帮助