刷新片段视图按钮被点击时,视图、片段、按钮

2023-09-06 16:43:43 作者:童話ゝ漸漸融化了

予有使用ViewPager显示一组片段的片段的活性。关于该片段的活性我有点击时,它发送一个消息到当前片段以刷新其内容的按钮。一切工作正常(活动/当前片段通讯)不同的是,我不能刷新片段的观点的事实。访问当前视图getView()不工作,该函数返回NULL;看来,在创建片段之后(上ViewCreated叫)getView被摧毁。我失去了一些东西呢?如何使我的片段以编程方式重绘其内容是什么?看来,当该片段是从父活动创造了这个工作的唯一方法是。我必须删除并重新添加片段再次做到这一点?

I have a fragment activity that uses a ViewPager to display a set of fragments. On the fragment activity I have a button that when clicked, it sends a message to the current fragment to refresh its contents. Everything works ok (activity / current fragment communication) except the fact that I cannot refresh the fragment's view. Accessing the current view by getView() does not work as this function returns null; it seems that after the fragment is created (on ViewCreated is called) getView gets destroyed. Am I missing something here? How to I cause a fragment to redraw its contents programmatically? It seems that the only way this works is when the fragment is created from the parent activity. Do I have to remove and re-add the fragment again to do this?

下面是code:

主要活动:

public class MainActivity extends FragmentActivity {

    private MyAdapter mAdapter;
    private static ViewPager mPager;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                setupViewPager();
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.activity_main, menu);
                return true;
        }

        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
                return super.onPrepareOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {

                case R.id.menu_test:
                        updateFragment();
                        return true;

                default: return true;
                }
        }

        private void updateFragment() {
                for (int i=0; i< mAdapter.getCount(); i++) {
                        SampleFragment fragment = (SampleFragment) mAdapter.getItem(i);
                        fragment.update();
                }
        }

        private void setupViewPager() {
                try {
                        mAdapter = new MyAdapter(getSupportFragmentManager());

                        mPager = (ViewPager) findViewById(R.id.pager);
                        mPager.setAdapter(this.mAdapter);

                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public class MyAdapter extends FragmentPagerAdapter {

                public MyAdapter(FragmentManager fm) {
                        super(fm);
                }

                @Override
                public Fragment getItem(int position) {
                        SampleFragment fragment = new SampleFragment(position); 
                        return fragment;
                }

                @Override
                public int getCount() {
                        return 5;
                }
        }
}

和片段类:

public class SampleFragment extends Fragment{

        private int myPosition = -1;

        public SampleFragment(int position) {
                this.myPosition = position;
        }

        @Override
        public void onAttach(Activity activity) {
                super.onAttach(activity);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View view = inflater.inflate(R.layout.fragment, container, false);
                update(view, "Updated from onCreateView");
                return view;
        }

        @Override
        public void onActivityCreated(Bundle bundle) {
                super.onActivityCreated(bundle);
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
                super.onViewCreated(view, savedInstanceState);

                view.findViewById(R.id.textTitle).setOnClickListener(myClickListener);
        }

        private OnClickListener myClickListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                        switch (v.getId()) {

                        case R.id.textTitle:
                                break;

                        }
                }
        };

        public void update() {
                update(getView(), "Updated from main");
        }

        private void update(View view, String subtitleText) {
                TextView title = (TextView) view.findViewById(R.id.textTitle);
                TextView subtitle = (TextView) view.findViewById(R.id.textSubtitle);

                title.setText("Fragment " + myPosition);
                subtitle.setText(subtitleText);
        }
}

上的错误view.FindViewById(查看为null)从主要活动菜单项时调用。发生

The error happens on view.FindViewById (view is null) when called from the menu item in the main activity.

推荐答案

您可以看看的这篇文章这也解释了如何保持你的ViewPager引用片段。

You can take a look at this article which explains how to keep references to the fragments in your ViewPager.

有在网页中描述的两种方法。第一个包括设置标签,当您添加使用FragmentManager片段。然后你可以检索使用片段 findFragmentByTag()。不过,我没有看到如何使用FragmentPagerAdapter或FragmentStatePagerAdapter,使这项工作,因为这些实现增加碎片为您服务。如果您使用的是自己的自定义PagerAdapter,这可能会为你工作。

There are two methods described on the page. The first one involves setting a tag when you add the fragment using the FragmentManager. Then you can retrieve the fragment using findFragmentByTag(). However, I did not see how to make this work using FragmentPagerAdapter or FragmentStatePagerAdapter, since these implementations add the fragments for you. If you are using your own custom PagerAdapter, this may work for you.

另一种方法,它不工作FragmentPagerAdapter或FragmentStatePagerAdapter,包括保持所有的片段的地图,更新在你的的getItem() destroyItem()的实施。这种方法很适合我。

The other method, which does work for FragmentPagerAdapter or FragmentStatePagerAdapter, involves keeping a map of all your fragments, updating inside your getItem() and destroyItem() implementations. This method has worked for me.

一旦你有一个参考目前的片段,你可以调用一个方法,在你的片段刷新其观点。

Once you have a reference to the current fragment, you can just call a method in your fragment to refresh its view.