ViewPager选项卡中的碎片显示错误的数据

时间:2015-08-30 21:13:46

标签: android

我从本教程http://blog.grafixartist.com/material-design-tabs-with-android-design-support-library/获得了帮助 实现Viewpager后。即使选项卡中显示正确的界面,我仍然放心。如果我在3个片段中吐司。第一个显示来自第二个的吐司,第二个显示来自第三个的吐司,第三个显示没有吐司,即使它有吐司。

以下是我的代码

public class ActiveSeen extends AppCompatActivity {

// Declaring Your View and Variables

Toolbar toolbar;


ViewPager mViewPager;
 ParseUser mUser;
public String ObjectId;
public String mser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_active_seen);

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    mUser = ParseUser.getCurrentUser();

     mser = mUser.getUsername();

    Intent i = new Intent();

    Intent getI=getIntent();

    ObjectId = getI.getStringExtra("ObjectId");

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

    setupViewPager(viewPager);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabanim_tabs);

    tabLayout.setupWithViewPager(viewPager);


    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            viewPager.setCurrentItem(tab.getPosition());

            switch (tab.getPosition()) {
                case 0:
                 //   showToast("One");
                    break;
                case 1:
                //    showToast("Two");
                    break;
                case 2:
                  //  showToast("Three");
                    break;
            }
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });


}

private void setupViewPager(ViewPager viewPager) {

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.addFrag(new CriticalInformationFragment(), "Critical Information");
    adapter.addFrag(new PersonalHistoryFragment(), "Personal History");
    adapter.addFrag(new ObstetricHistoryFragment(), "Obstetric History");
   // adapter.addFrag(new MedicalHistoryFragment(), "Critical Information");
 //   adapter.addFrag(new SexuallyTransmittedInfectionFragment(), "Sexually Transmitted Infection");
 //   adapter.addFrag(new FamilyHistoryFragment(), "Family History");

    viewPager.setAdapter(adapter);
}



class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();
    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }
    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }
    @Override
    public int getCount() {
        return mFragmentList.size();
    }
    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

我的一个片段类

public  class CriticalInformationFragment extends Fragment implements View.OnClickListener{

    private static final String ARG_SECTION_NUMBER = "section_number";

    TextView name;
    TextView maidenname;
     static TextView dateofbirth;
    TextView registrationNumber;
    TextView bloodgrp;
    TextView rhesusfactor;
    TextView sicklingstatus;
    TextView hbelectro;
    private FloatingActionButton fab1;

    ActiveSeen mActivity;

     static EditText input;
    String ObjectId;
    ParseUser mUser;
    private ProgressDialog progressDialog;


    int color;

    public static CriticalInformationFragment newInstance(int sectionNumber) {
        CriticalInformationFragment fragment = new CriticalInformationFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }


    public CriticalInformationFragment() {
        this.color = color;
    }

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

        Toast.makeText(getActivity(),"Critical" , Toast.LENGTH_SHORT).show();

         final FloatingActionButton fabsave = (FloatingActionButton) rootView.findViewById(R.id.fabsave);
        final FloatingActionButton fabconsult = (FloatingActionButton) rootView.findViewById(R.id.fabconsultation);
        final FloatingActionButton fabrequest = (FloatingActionButton) rootView.findViewById(R.id.fablabrequest);

        final TableRow row1 = (TableRow) rootView.findViewById(R.id.tableRow1);
        final TableRow row2 = (TableRow) rootView.findViewById(R.id.tableRow2);
        final TableRow row3 = (TableRow) rootView.findViewById(R.id.tableRow3);
        final TableRow row4 = (TableRow) rootView.findViewById(R.id.tableRow4);
        final TableRow row5 = (TableRow) rootView.findViewById(R.id.tableRow5);
        final TableRow row6 = (TableRow) rootView.findViewById(R.id.tableRow6);
        final TableRow row7 = (TableRow) rootView.findViewById(R.id.tableRow7);
        final TableRow row8 = (TableRow) rootView.findViewById(R.id.tableRow8);

        row1.setOnClickListener(this);
        row2.setOnClickListener(this);
        row3.setOnClickListener(this);
        row4.setOnClickListener(this);
        row5.setOnClickListener(this);
        row6.setOnClickListener(this);
        row7.setOnClickListener(this);
        row8.setOnClickListener(this);

        final FrameLayout frameLayout = (FrameLayout) rootView.findViewById(R.id.frame_layout);
        frameLayout.getBackground().setAlpha(0);
        final FloatingActionsMenu fabMenu = (FloatingActionsMenu) rootView.findViewById(R.id.fabmenu);


            fabsave.setOnClickListener(this);
        fabconsult.setOnClickListener(this);
        fabrequest.setOnClickListener(this);


        mActivity = (ActiveSeen) getActivity();

         ObjectId = mActivity.ObjectId;



         name = (TextView) rootView.findViewById(R.id.nameEdit);
         maidenname= (TextView) rootView.findViewById(R.id.maidenEdit);
         dateofbirth= (TextView) rootView.findViewById(R.id.dateofbirthEdit);
         registrationNumber= (TextView) rootView.findViewById(R.id.registrationEdit);
         bloodgrp= (TextView) rootView.findViewById(R.id.bloodgrpEdit);
         rhesusfactor= (TextView) rootView.findViewById(R.id.rhesusfactorEdit);
         sicklingstatus= (TextView) rootView.findViewById(R.id.sicklingstatusEdit);
         hbelectro= (TextView) rootView.findViewById(R.id.hbElectroEdit);



        getDataParse();


        fabMenu.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
            @Override
            public void onMenuExpanded() {
                frameLayout.getBackground().setAlpha(300);
                frameLayout.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        fabMenu.collapse();
                        return true;
                    }

                });
            }


            @Override
            public void onMenuCollapsed() {
                frameLayout.getBackground().setAlpha(0);
                frameLayout.setOnTouchListener(null);
            }
        });


        return rootView;
    }

1 个答案:

答案 0 :(得分:1)

您正在调用onCreateView中的toast。在viewpager中,视图在显示之前创建。如此answer中所述,您应该覆盖setUserVisibleHint以在显示片段时执行操作。