Вопрос

I have app with FragmentStatePagerAdapter. One of the fragment contains some linearlayouts. I also have ActionBar. When i click at icon(button) on ActionBar, I will update my ArrayList with strings. After update ArrayList i want to update LinearLayout in fragment.

This is my Java code of FragmentPagerAdapter:

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

Here is my class with creating content of Fragment:

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

Can U help me with my problem pls?

Это было полезно?

Решение

Simplest and most straight forward way is to register each fragment on its activity while creating it. Then delegate event made by actionbar button through the activity to appropriate fragment. Other way is to use some kind event distribution observer pattern or event-bus patter. I use event-bus implemented by RoboGuice personaly. Other implementations could be found in Guava or Otto libraries.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top