java - 通过单击 ActionBar 按钮更改 FragmentPagerAdapter 中 fragment 的内容

标签 java android android-fragments refresh

我有带有 FragmentStatePagerAdapter 的应用程序。其中一个 fragment 包含一些线性布局。我也有 ActionBar。当我单击 ActionBar 上的图标(按钮)时,我将用字符串更新我的 ArrayList。更新 ArrayList 后,我​​想更新 fragment 中的 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;
        }
    }

这是我创建 fragment 内容的类(class):

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

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

最佳答案

最简单、最直接的方法是在创建 fragment 时在其 Activity 上注册每个 fragment 。然后通过 Activity 将操作栏按钮生成的事件委托(delegate)给适当的 fragment 。 其他方法是使用某种事件分发观察者模式或事件总线模式。 我个人使用 RoboGuice 实现的事件总线。其他实现可以在 Guava 或 Otto 库中找到。

关于java - 通过单击 ActionBar 按钮更改 FragmentPagerAdapter 中 fragment 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22694439/

相关文章:

java - 如何授予 Tomcat 9 访问其他文件的权限

android - 如何在 datepickerdialog android 中选择年份?

java - Hibernate:父实体上的过滤器是否隐式过滤子实体?

java - 尝试将实例转换为 PersistenceCapable 失败。确保它已得到增强

android - Fragment 在点击时隐藏和显示

android - 如何使用 flutter 创建单一后端网站、iOS 应用程序和 Android 应用程序?

java - 我想使用 Java 字符串数组循环在 SQLite 中创建多个表。编程不好?

java - 在 fragment 中单击按钮时 SetOnClickListener 不起作用

android - 当我在布局末尾对齐选项卡布局时,布局顶部留下了一个空格

java - 换行符不会在浏览器中呈现