使用3个片段加载第二个片段FragmentStatePagerAdapter时出现问题

时间:2014-09-17 19:51:55

标签: android android-fragments fragmentstatepageradapter

我有3个片段:Frag1,Frag2和Frag3。当我打开我的应用程序时,Frag1会在第一页上加载并显示一个网页。第二页上的Frag2应显示从同一网页提取的图像。第3页上的Frag3应显示从同一页面提取的一些文本。 在每个片段中,我能够提取并放置内容。 以下代码面临的问题是:

  1. 当我从菜单选项中选择日期时,我正在设置一个新的日期字符串/ url字符串,所有三个片段都应使用新数据进行刷新。 这没有发生,我不知道为什么。 在刷卡过程中,只有Frag1和Frag3,即Page 1和Page 3在几秒钟后才会反射。 第2页,即我有一个imageView和一个保存图像的按钮,这是Frag2永远不会被反映。
  2. 下面是FragmentActivity和Fragment实现。

    如果我从菜单选项函数changeDate()中选择一个新日期,即案例R.id.date_settings,我能帮助我如何刷新所有片段。

    以下是主要功能的摘要。一切都很好,但上面提到的问题。

    public class MainActivity extends FragmentActivity {
    
        public int year;
        public int month;
        public int day;
        public String date_string;
        public static String urlString;
        public static String urlImageString;
        ViewPager mViewPager;
        SectionsPagerAdapter mSectionsPagerAdapter;
        FragmentManager fragmentManager;
        TextView pageTitleText;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
            mViewPager = (ViewPager) findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);
    
            final Calendar c = Calendar.getInstance();
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day = c.get(Calendar.DAY_OF_MONTH);
            createDateString();
    
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    
        public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
    
            public SectionsPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int position) {
                // getItem is called to instantiate the fragment for the given page.
    
                Fragment fragment;
                Bundle args = new Bundle();
                switch(position) {
                case 0:
                    fragment = new Frag1();
                    args.putInt(FullPageFragment.ARG_SECTION_NUMBER, position + 1);
                    fragment.setArguments(args);
                    return fragment;
                case 1:
                    fragment = new Frag2();
                    args.putInt(ImageFragment.ARG_SECTION_NUMBER, position + 1);
                    fragment.setArguments(args);
                    return fragment;
                case 2:
                    fragment = new Frag3();
                    args.putInt(MessageFragment.ARG_SECTION_NUMBER, position + 1);
                    fragment.setArguments(args);
                    return fragment;
                default:
                    return null;
                }
            }
    
    
            @Override
            public int getCount() {
                // Show 3 total pages.
                return 3;
            }
    
            @Override
            public CharSequence getPageTitle(int position) {
                Locale l = Locale.getDefault();
                switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return getString(R.string.title_section3).toUpperCase(l);
                }
                return null;
            }
        }
    
        public static class Frag1 extends Fragment {
            public static final String ARG_SECTION_NUMBER = "section_number";
            WebView webView;
            public FullPageFragment() {
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main,
                        container, false);
    
                webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
                webView.loadUrl(urlString);
                return rootView;
            }
    
            @Override
            public void onSaveInstanceState(Bundle outState) {
                // TODO Auto-generated method stub
                super.onSaveInstanceState(outState);
            };  
        };
    
    
        public static class Frag2 extends Fragment {
    
            public static final String ARG_SECTION_NUMBER = "section_number";
            ImageLoader imgLoader;  // class that downloads and display image also has save option
            ImageView imgview;
            int loader;
    
            public ImageFragment() {
            };
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.image_view,
                        container, false);
    
                loader = R.drawable.ic_launcher;
                imgview = (ImageView) rootView.findViewById(R.id.imageView1);
                imgLoader = new ImageLoader( super.getActivity().getApplicationContext());
                imgLoader.DisplayImage(urlImageString, loader, imgview, file_string);
                }
    
                Button imgBtn = (Button) rootView.findViewById(R.id.button1);
                imgBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
    
                        Log.i(TAG, "onClick: Saving file: < " + file_string + ">");
                        imgLoader.SaveImage(file_string);
                    }
                });
                return rootView;
            }
    
            @Override
            public void onSaveInstanceState(Bundle outState) {
                // TODO Auto-generated method stub
                super.onSaveInstanceState(outState);
            };
    
        }
    
        public static class Frag3 extends Fragment {
    
            public static final String ARG_SECTION_NUMBER = "section_number";
            WebView webView;
            public String finalUrl;
    
            public MessageFragment() {
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.message_view,
                        container, false);
                webView.loadData(finalMsg, "text/html", "UTF-8");
                return rootView;            
            };
    
            @Override
            public void onSaveInstanceState(Bundle outState) {
                // TODO Auto-generated method stub
                super.onSaveInstanceState(outState);
            }
        };
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle item selection
            switch (item.getItemId()) {
                case R.id.date_settings:
                    changeDate();
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
    
        public void changeDate() {
            final Calendar c = Calendar.getInstance();
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day = c.get(Calendar.DAY_OF_MONTH);
    
            DatePickerDialog dpd = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {
    
                        @Override
                        public void onDateSet(DatePicker view, int year1,
                                int monthOfYear, int dayOfMonth) {
                            //txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                            year = year1;
                            month = monthOfYear;
                            day = dayOfMonth;
                            Log.i(TAG, "onDateSet: date has changes resetting values");
                            createDateString();
    
                            createUrl();
                            createUrImage();
                            createFileString();
    
                        }
                    }, year, month, day);
            dpd.show();
        };
    
        public void createUrl() {
            //implementaion is here
        }
    
        public void createUrImage() {
            //implementaion is here
        }
    
        public void createFileString() {
            //implementaion is here
        }
    }
    

0 个答案:

没有答案