按下电话的后退按钮后,ViewPager不会生成UI

时间:2014-01-15 07:44:38

标签: java android android-fragments android-viewpager android-nested-fragment

这是我的代码:

Home.java

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Home extends Fragment {

    private TextView textView23;
    private ImageView addNewSalesInquery;
    private LinearLayout addNewSalesButton;
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

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

        //Intializing instance variables
        //addNewSalesInquery = (ImageView)findViewById(R.id.add_new_sales_inq);
        //textView23 = (TextView)findViewById(R.id.textView23);
        //textView23.setSelected(true);
         addNewSalesButton = (LinearLayout)view.findViewById(R.id.add_new_sales_btn);

        //Registering Listeners
        //addNewSalesInquery.setOnClickListener(new AddNewSalesInqury());
         addNewSalesButton.setOnClickListener(new AddNewSalesInqury());
        return view;

    }



    //Event Handler for Add New Sales
    private class AddNewSalesInqury implements OnClickListener
    { 

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub

            FragmentTransaction ft = getFragmentManager().beginTransaction();

            ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
            ft.replace(R.id.fragment_container, new ViewPagerManager(), "fragment").addToBackStack("fragment");
            // Start the animated transition.
            ft.commit();


        }

    }

}

ViewPagerManager.java

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ViewPagerManager extends Fragment {

    private ImageView addNewSalesInqury;
    private RelativeLayout salesInquryMainLayout;
    private TableRow testEditSales; 
    private View view;

     private static final int ITEMS = 2;
        private static ViewPager viewPager;
        private MyAdapter pageAdapter;

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




    }

    private void intialize()
    {
        viewPager = (ViewPager)view.findViewById(R.id.pager);
        pageAdapter = new MyAdapter(getFragmentManager());
        viewPager.setAdapter(pageAdapter);
    }



    private  class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            if(position==0)
            {
                return new SalesInqury();
            }
            else
            {
                return new NewLead();
            }
        }
    }

    public static void setCurrentItem (int item, boolean smoothScroll) {
        viewPager.setCurrentItem(item, smoothScroll);
    }

    //This method will handle menu clicks
        public static void onMenuItemClicked(View view) {
            if(view.getId()==R.id.menu_add_inquiry)
            {
                setCurrentItem(2,true);
            }
        }

}

activity_leads_and_sales_handling.xml

<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=".LeadsAndSalesHandlingActivity"
    android:id="@+id/leadsAndSalesHandlingActivity" >

     <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
       </android.support.v4.view.ViewPager>

</RelativeLayout>

我使用此功能从Home导航到ViewPagerManager。在ViewPagerManager中,如果我点击后退按钮,我可以返回Home。现在,如果我尝试通过单击应用程序上的虚拟按钮导航回ViewPagerManager,会发生什么呢,它只是导航到100%空白的白色屏幕!这有什么不对?

更新

我想我发现了这个问题。在 activity_leads_and_sales_handling.xml 中有ViewPager,它应显示的内容是Fragment中的第一个ViewPager。单击后退按钮时,我认为ViewPager由于某种原因停止工作或停止显示UI。

2 个答案:

答案 0 :(得分:3)

我也遇到了同样的问题,使用 getChildFragmentManager() 代替 getFragmentManager()

getChildFragmentManager() 返回私有FragmentManager,用于放置和管理此片段内的片段

答案 1 :(得分:0)

我有同样的问题。覆盖onResume()并重新初始化UI。

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

private void initialize(){
    btnOne= (Button) getActivity().findViewById(R.id.btnOne);
    btnTwo = (Button) getActivity().findViewById(R.id.btnTwo);      

    btnOne.setOnClickListener(this);
    btnTwo.setOnClickListener(this);    

    viewPager = (ViewPager) getActivity().findViewById(R.id.viewPager);
    viewPager.setAdapter(new ViewPagerAdapter(getActivity().getSupportFragmentManager()));  

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