通过ActionBar按钮单击更改FragmentPagerAdapter中片段的内容

时间:2014-03-27 17:05:32

标签: java android android-fragments refresh

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

这是我的FragmentPagerAdapter的Java代码:

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;
        }
    }

这是我创建片段内容的课程:

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;
        }

你可以帮我解决我的问题吗?

1 个答案:

答案 0 :(得分:1)

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