从另一个类更新ViewPager的片段: [英] Update ViewPager's Fragments from another class:

查看:83
本文介绍了从另一个类更新ViewPager的片段:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题:

如何在ViewPager中重新创建所有片段:

我有那些课程:

public class ViewPagerAdapter extends FragmentPagerAdapter 
{
private List<Fragment> fragments;

/**
 * @param fm
 * @param fragments
 */
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

/* (non-Javadoc)
 * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
 */

@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}

/* (non-Javadoc)
 * @see android.support.v4.view.PagerAdapter#getCount()
 */

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

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}  
}

FragmentActivity:

 public class TabsViewPagerFragmentActivity extends FragmentActivity implements  
 ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener 
 {
    static final String TAG = TabsViewPagerFragmentActivity.class.getSimpleName();
    private TabHost mTabHost;
    private ViewPager mViewPager;
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String,      TabsViewPagerFragmentActivity.TabInfo>();
    public ViewPagerAdapter mPagerAdapter;
    .....

现在在这个FragmentActivity中,我有这个侦听器:

Now in this FragmentActivity I have this listener:

  private class SlicerSelectedOnClickListener implements OnClickListener 
{
    private ODSharedSlicer slicer;
    private int slicerIndex;

    public SlicerSelectedOnClickListener(ODSharedSlicer slicer, int slicerIntex)
    {
        super();
        this.slicer = slicer;
        this.slicerIndex = slicerIntex;
    }

    public void onClick(View v) 
    {
        slicerOnClickConfiguration(slicerIndex, v);
        mPagerAdapter.notifyDataSetChanged();
    }
}
    ....

而且效果很好,notifyDataSetChanged()被调用,并且ViewPager的Fragments可以根据需要重新创建.

And this works great notifyDataSetChanged() is getting called and the ViewPager's Fragments are getting recreated as I want.

问题:当我尝试从另一个类(在我的情况下是负责解析从服务器接收的数据的类)中调用适配器的notifyDataSetChanged()方法时保持不变.

The Problem: When I try to call the notifyDataSetChanged() method of the adapter from another class (In my case it's a class that responsible to parse data that is received from the server) the fragments remain unchanged.

我试图这样做:我在FragmentActivity类(带有ViewPager的类)中创建了这些方法:

I tried to do this: I created those methods in my FragmentActivity class (the one with the ViewPager):

 public void NotifyTabActivityViewPagerAdapter()
{
    mPagerAdapter.notifyDataSetChanged();
}

public ViewPagerAdapter getTabActivityViewPagerAdapter()
{
    return mPagerAdapter;
}

public ViewPager getTabActivityViewPager()
{
    return mViewPager;
}

并尝试了第二堂课中的以下内容:

And tried the following from the second class:

((TabsViewPagerFragmentActivity)currentActivity).NotifyTabActivityViewPagerAdapter();

或者:

((TabsViewPagerFragmentActivity)currentActivity).getTabActivityViewPager().setAdapter(((TabsViewPagerFragmentActivity)currentActivity).getTabActivityViewPagerAdapter());

但是这些都不行.奇怪的是,当我运行此代码时:

But non of this works. Strangely when I run this:

((TabsViewPagerFragmentActivity)currentActivity).getTabActivityViewPager().setAdapter(null);

片段确实被删除了,但是我无法使用从服务器端接收到的新数据来重新创建新的片段集.

The fragments do get removed, But I can't recreate the new set of fragments with the new data received from server side.

我在这里想念什么?我应该如何从另一个类重新创建FragmentActivity中的所有片段?

What am I missing here? How should I recreate all the fragment in this FragmentActivity from another class?

任何帮助将不胜感激.

推荐答案

里面确实有7个问题.但是我理解主要的问题是要从其他类更新数据或将数据发送到与ViewPager关联的FragmentActivity.我不知道与FragmentActivityViewPager的其他类关系,但是我知道有2种方法可以按照您想要的方式处理更新或传输信息

There's really like 7 questions in there. But I understand the main question to be updating or sending data to a FragmentActivity associated with a ViewPager from some other class. I don't know this other classes relationship to the FragmentActivity or ViewPager but there are 2 ways I know of that could handle updating or transfering information the way you want

(1)一个界面

如果您要引用的类是另一个Fragment,并且以相同的ViewPager加载,您将在另一个FragmentFragmentActivity中更新数据(或调用方法)的方式是通过接口.您可以滚动到该链接的底部以获取一些示例代码,但基本上,您是在Fragment与包含ViewPager的活动(即片段的宿主活动)之间建立了桥梁.然后,您创建一个侦听器",以查找Fragment中的某些更改,然后将其传递给hoast活动一些信息.然后,主机活动在您需要更新的第二个Fragment中调用一个公共方法.这里唯一棘手的部分是要记住在FragmentonAttach方法中初始化接口监听器",否则将不起作用.

If the class you're referring to is another Fragment and loaded in the same ViewPager the way you would update the data (or call a method) in another Fragment or FragmentActivity is with an interface. You can scroll to the bottom of that link for some sample code but basicly you create a bridge between the Fragment and the activity that contains the ViewPager (i.e. the fragment's host activity). You then create a "listener" that looks for some change in the Fragment, upon which it passes some information to the hoast activity. The host activity then calls a public method within the second Fragment you need to update. Only tricky part here is to remember to initialize your interface "listener"within your Fragment's onAttach method or it won't work.

(2)意图

如果要处理两个单独的活动,只需使用Intent.将意图传递给要查找的FragmentActivity,并覆盖onNewIntent(Intent i)以执行接收类中所需的任何功能.

If you're dealing with two seperate activities just use an Intent. Pass an intent to the FragmentActivity you're looking for and override onNewIntent(Intent i) to perform whatever function you want within the recieving class.

希望有帮助-我需要更具体地回答,因为那里有很多子问题.

Hope that helps - I'd need more specificy to answer futher because there's a lot of sub-questions in there.

这篇关于从另一个类更新ViewPager的片段:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆