首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >单击“FragmentPagerAdapter”按钮更改ActionBar中片段的内容

单击“FragmentPagerAdapter”按钮更改ActionBar中片段的内容
EN

Stack Overflow用户
提问于 2014-03-27 17:05:32
回答 1查看 368关注 0票数 0

我有FragmentStatePagerAdapter的应用程序。其中一个片段包含一些线性布局。我也有ActionBar。当我点击ActionBar上的图标(按钮)时,我将用字符串更新我的ArrayList。在更新ArrayList之后,我希望在片段中更新LinearLayout。

这是我的FragmentPagerAdapter的Java代码:

代码语言:javascript
运行
复制
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.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            switch (position) {
            case 0: {
                Fragment fragment = new NejblizsiBary();
                Bundle args = new Bundle();
                args.putInt(NejblizsiBary.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
                return fragment;
            }

            case 1: {
                Fragment fragment3 = new Mapa();
                Bundle args = new Bundle();
                args.putInt(Mapa.ARG_SECTION_NUMBER, position + 2);
                fragment3.setArguments(args);
                return fragment3;
            }

            case 2: {
                Fragment fragment2 = new OblibeneBary();
                Bundle args = new Bundle();
                args.putInt(OblibeneBary.ARG_SECTION_NUMBER, position + 3);
                fragment2.setArguments(args);
                return fragment2;
            }

            default:
                return null;
            }
        }

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

        @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_section3).toUpperCase(l);

            case 2:
                return getString(R.string.title_section2).toUpperCase(l);
            }
            return null;
        }
    }

下面是我创建片段内容的类:

代码语言:javascript
运行
复制
public class NejblizsiBary extends Fragment implements LocationListener {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public NejblizsiBary() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                    container, false);

            LinearLayout mainLayout = (LinearLayout) rootView.findViewById(R.id.hl);
            getApplicationContext();
            LayoutInflater inflater_layout = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            for(int i = 0; i < pocet_baru;i++){
            View itemBox = inflater_layout.inflate(R.layout.jedenbar,null);
            itemBox.setId(i);
            TextView nazev = (TextView)itemBox.findViewById(R.id.nazev);
            nazev.setText(bary.get(i).getNazev());
            View.OnClickListener handler = new View.OnClickListener(){

                @Override
                public void onClick(View arg0) {
                    Detaily dialog = new Detaily();
                    Bundle args = new Bundle();
                    int id = arg0.getId();
                    args.putDouble("hlat", mLatitude);
                    args.putDouble("hlong", mLongitude);
                    args.putDouble("lat", bary.get(id).getLatitude());
                    args.putDouble("long", bary.get(id).getLongtitude());
                    args.putString("nazev", bary.get(id).getNazev());
                    args.putString("adresa", bary.get(id).getAdresa());
                    args.putString("mesto", bary.get(id).getMesto());
                    args.putString("popis", bary.get(id).getPopis());
                    dialog.setArguments(args);
                    dialog.show(getActivity().getFragmentManager(), "MyDialog");
                }

            };
            itemBox.setOnClickListener(handler);
            mainLayout.addView(itemBox);
            }
            return rootView;
        }

你能帮我解决我的问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-27 17:27:52

最简单、最直接的方法是在创建活动时注册每个片段。然后将actionbar按钮通过活动生成的事件委托给适当的片段。其他方法是使用某种事件分布观测器模式或事件总线模式。我使用由RoboGuice个人实现的事件总线。其他实现可以在Guava或Otto库中找到。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22694439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档