更新的ViewPager从活动片段片段、ViewPager

2023-09-06 16:44:41 作者:积极向喪

我是新来的Andr​​oid开发,我真的AP preciate一些帮助这里。

I'm new to Android developing and I would really appreciate some help here.

我使用的是包含一个TextView一个片段,我用同样的MyFragment类的5个实例。

I'm using a fragment that contains a TextView and I'm using 5 instances of the same MyFragment class.

在活动中,我得到了一个按钮和一个ViewPager,我需要的按钮更新所有的片段实例的内容,只要它的点击。

In the activity, i got a button and a ViewPager, and I need the button to update all the fragment instances content, whenever its clicked.

这里的活动

public class MainActivity extends FragmentActivity {

final static String[] CONTENT = {"a", "b"};
ViewPager pager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    List<MyFragment> fragments = new Vector<MyFragment>();
    for(int i = 0; i < 5; i++){
        MyFragment fragment = new MyFragment(CONTENT);
        fragments.add(fragment);
    }
    PagerAdapter adapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);
    pager = (ViewPager) findViewById(R.id.viewpager);
    pager.setAdapter(adapter);

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //method that isn't working
            PagerAdapter adapter = (PagerAdapter)pager.getAdapter();
            for(int i = 0; i < 5; i++){
                MyFragment fragment = (MyFragment) adapter.getItem(i);
                fragment.textView.setText(fragment.content[1]);
            }
        }
    });
}
}

该片段

public class MyFragment extends Fragment{

String[] content;
    TextView textView;

public MyFragment(String[] content) {
    this.content = content;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_content, container, false);
    textView = (TextView) view.findViewById(R.id.textView1);
    textView.setText(content[0]);
    return view;
}

}

而FragmentPagerAdapter

And the FragmentPagerAdapter

public class PagerAdapter extends FragmentPagerAdapter{

List<MyFragment> fragments;

public PagerAdapter(FragmentManager fm, List<MyFragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

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

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

}

的onclick方法,给了我一个NullPointerException异常每当我尝试从小于adapter.getCurrentItem()适配器访问片段 - 比adapter.getCurrentItem()+ 1

The OnClick method gives me a NullPointerException whenever i try to access a fragment from the adapter which is less than adapter.getCurrentItem() - 1, or more than adapter.getCurrentItem() + 1.

如何在同一时间更新所有的片段任何想法?

Any idea on how to update all the fragments at the same time?

在此先感谢。

推荐答案

更​​新这些片段最简单的方法就是用你的code和设置碎片数量的 ViewPager 在存储器保存到总片段数 - 1(所以所有片段都有效不管你是什么页)。你的情况:

The easiest way to update those fragments is to use your code and set the number of fragments that the ViewPager holds in memory to the number of total fragments - 1(so all fragments are valid no matter at what page you are). In your case:

pager.setOffscreenPageLimit(4); // you have 5 elements

您仍可以使用该方法从我的意见与方法 onPageScrollStateChanged (这样的更新将开始的那一刻起用户开始刷卡),看看当用户开始刷卡寻呼机和更新片段当前可见片段的左,右,但是这将是一个有点难以得到的权利,所以我建议去的第一个选项。

You can still use the method from my comment with the method onPageScrollStateChanged(so the update will start the moment the user starts swiping) to see when the user is starting to swipe the pager and update the fragments to the left and right of the currently visible fragment, but this will be a bit difficult to get right so I recommend to go with the first option.

对于包含片段的code几点:

Some points regarding your code containing fragments:

如果您嵌套的片段类使其静态的,所以你不打领带就到活动对象。 不要创建一个构造为片段除了默认之一。如果框架需要重新创建片段,它会调用默认的构造函数,如果它不存在,它会抛出异常。例如,尝试更改手机/仿真器的方向,看看会发生什么(这是其中一种情况下,当Android将重新创建的片段)。最后,使用自定义名称 ViewPager 的适配器,使用 PagerAdapter 这是超类的名称对 FragmentViewPager ,这是非常令人困惑的人读你的code。

If you nest the fragment class make it static so you don't tie it to the activity object. Don't create a constructor for a Fragment besides the default one. If the framework needs to recreate the fragment it will call the default constructor and if it is not available it will throw an exception. For example, try to change the orientation of the phone/emulator and see what happens(this is one of the cases when Android will recreate the fragments). Last, use a custom name for the ViewPager's adapter, you use PagerAdapter which is the name of the super class of FragmentViewPager and it's very confusing for someone reading your code.

如果您需要将数据传递到片段您可以使用创建方法像下面这样:

If you need to pass data to the Fragment you could use a creation method like the one below:

public static MyFragment newInstance(String text) {
        MyFragment f = new MyFragment();
        Bundle b = new Bundle();
        b.putString("content", text);
        f.setArguments(b);
        return f;
    }

该文本将在 MyFragment 可以通过使用 getArguments()的getString(内容);