Android的,如何重新启动/刷新从FragmentActivty的片段? [英] Android, How to restart/refresh a fragment from FragmentActivty?

查看:476
本文介绍了Android的,如何重新启动/刷新从FragmentActivty的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我FragmentActivity我有一个片段。从服务器该片段加载数据。 我希望我们的用户在刷新按钮,然后点击我调用片段的方法,做清爽的过程。

In my FragmentActivity I have a fragment. This fragment loads data from server. I want we user clicks on refresh button then I call a method in fragment and do refreshing process.

我在FragmentActivity创建了一个接口,并设置其当用户点击刷新按钮变量。在code是这样的:

I created an interface in FragmentActivity and set its variable when user clicks on refresh button. The code is like this:

public class MainScreen extends FragmentActivity {
    public interface OnRefreshSelected {
        public void refreshFragment(boolean flag);
    }

    private OnRefreshSelected onRefreshSelected;

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Log.i(TAG, "Try to create MainScreen...");

            setContentView(R.layout.activity_main);

        onRefreshSelected = (OnRefreshSelected) MainScreen.this;
    }

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

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                    case R.id.menu_refresh:
                        onRefreshSelected.refreshFragment(true);
                        return true;
                    default:
                        return super.onOptionsItemSelected(item);
        }
    }
}

而在片段类我实现了这个接口:

And in Fragment class i implemented this interface:

public class CategoryFragment extends Fragment implements OnRefreshSelected {
    @Override
    public void refreshFragment(boolean flag) {
        Log.i(TAG, "refresh requested. Try to reload data for this fragment...");

        getData();
    }
} 

当我运行,应用程序崩溃和logcat中显示了该消息:

When I run, the application crashes and logcat shows this message:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.astro.recipe.activities/com.astro.recipe.activities.MainScreen}: java.lang.ClassCastException: com.astro.recipe.activities.MainScreen

和指向该行:

onRefreshSelected = (OnRefreshSelected) MainScreen.this;

什么是访问的片段,从它的主机的方法,最好的方法是什么? 任何建议将AP preciated。谢谢

What is the best way to have access to a method of fragment from its host? any suggestion would be appreciated. Thanks

推荐答案

执行这个

onRefreshSelected = (OnRefreshSelected) MainScreen.this;

要分配的活动 onRefreshSelected 现场,试图将它转换为你的界面,但你的行为没有实现的接口,这就是为什么 ClassCastException异常提升。

you are assigning your activity in onRefreshSelected field, trying to cast it to your interface, but your activity doesn't implement the interface, that's why ClassCastException raised.

相反,使用这样的事情

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
                case R.id.menu_refresh:
                    CategoryFragment fragment = (CategoryFragment) getFragmentManager()
                        .findFragmentById(R.id.category_fragment);
                    if (fragment != null) {
                        fragment.getData();
                    }
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
    }

您的活动可以获取基准从FragmentManager的片段,使用<一个调用的片段方法href="http://developer.android.com/reference/android/app/FragmentManager.html#findFragmentById%28int%29"相对=nofollow> findFragmentById()或<一href="http://developer.android.com/reference/android/app/FragmentManager.html#findFragmentByTag%28java.lang.String%29"相对=nofollow> findFragmentByTag()。

Your activity can call methods in the fragment by acquiring a reference to the Fragment from FragmentManager, using findFragmentById() or findFragmentByTag().

这篇关于Android的,如何重新启动/刷新从FragmentActivty的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆