1

我实例化了一个 FragmentActivity (FA1),它在它的 Fragments (F1) 中显示文章(使用 ViewPager)。每个文章Fragment都有一个额外的Class(W)的实例,一个WebManager,它保存了文章的ID。如果我单击文章中的图像,WebViewClient 将创建另一个 FragmentActivity (FA2)(将文章 ID 保存在包中),它会在其 Fragments (F2) 中显示图像。

发生以下情况:

FA1 构建三个 F1:A3、A2、A1(按此顺序)。每个 F1 用它的 ID 实例化一个 W(A3:ID = A3,A2:ID = A2,A1:ID = A1)。此外,ID 被覆盖,其值每次都是 A3。如果我单击 A2 中的图像,W 将创建 ID = A3 的 FA2,而不是 A2!

我可以做些什么来为我点击的这个 F1 保存 W 的实例?不知道该怎么办:(

希望我能正确地说明场景(用英语),也在德国论坛上问过这个问题......

- 编辑 -

F1:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
...
    articleID = args.getString("articleID");//set in FA1, will be overwriten
    WebView webView = ((WebView) rootView.findViewById(R.id.articleFragment));
    WebManagerArticle wm = WebManagerArticle.getInstance(activity, Integer.parseInt(articleID));
    webView.setHttpAuthUsernamePassword(Constants.HOST, "", Constants.HTTP_USER, Constants.HTTP_PASSWORD);
    webView.setWebViewClient(wm.getArticleWebViewClient());
...
return rootView;
}

WebManager文章:

public class WebManagerArticle extends WebManager
{
    private static WebManagerArticle instance = null;
    private static int id = -1;
    private static ArticleWebViewClient webViewClient = null;
    private WebManagerArticle()
    {
        super();
        webViewClient = new ArticleWebViewClient();
    }

    public static WebManagerArticle getInstance(Activity a, int articleId)
    {
        if (instance == null)
        {
            instance = new WebManagerArticle();
        }
        activity = a;
        id = articleId;
        return instance;
    }

    public ArticleWebViewClient getArticleWebViewClient(){return webViewClient;}

    private class ArticleWebViewClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            ...//HERE
            Intent intent = new Intent(activity, ImagePageSliderActivity.class);
            intent.putExtra("imagePath", imagePath); //get imagePath from url with pattern matching
            intent.putExtra("articleID", id);
            activity.startActivity(intent);
            ...
            return true;
        }
        @Override
        public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm)
        {
            handler.proceed(Constants.HTTP_USER, Constants.HTTP_PASSWORD);
        }
    }
}

ImagePageSlideActivity

public class ImagePageSliderActivity extends SherlockFragmentActivity
{
    public Context context;
    public int articleID;
    public int pages;
    private int startPosition;
    public ArrayList<String> imagePaths;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        context = this;
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_article_page_slider);

        String startImagePath = getIntent().getStringExtra("imagePath");
        // here's the wrong article ID!!!
        articleID = getIntent().getIntExtra("articleID", -1);

        // pages get From Database
        // startPosition get From Database
        // imagePaths get from database

        viewPager = (ViewPager) findViewById(R.id.articlePager);
        PagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(
                getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(startPosition);
    }

    @Override
    public void onBackPressed()
    {
        if (viewPager.getCurrentItem() == startPosition)
        {
            super.onBackPressed();
        } else if (viewPager.getCurrentItem() > startPosition)
        {
            viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
        } else if (viewPager.getCurrentItem() < startPosition)
        {
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
        }
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter
    {
        public ScreenSlidePagerAdapter(FragmentManager fm){super(fm);}

        @Override
        public Fragment getItem(int position)
        {
            Fragment fragment = new ImagePageFragment();

            Bundle args = new Bundle();
            args.putString("imagePath", imagePaths.get(position));

            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount()
        {
            return pages;
        }
    }
}
4

1 回答 1

1

不要制作WebManagerArticle单例,也不要只制作它的字段static,因为它们将共享上次Fragment初始化的状态。当ViewPager创建第一个页面片段时,WebManagerArticle将与正确的 id 一起创建。当第二个页面片段被构建时,WebManagerArticle它已经被构建并且在其上设置字段将覆盖为第一个片段存储的先前数据。最后一个片段也是如此,这就是为什么你得到第三个片段的唯一ID。

于 2013-06-18T05:02:45.340 回答