如何将数据从 Activity 发送到 Fragment [英] How to send data from Activity to Fragment

查看:40
本文介绍了如何将数据从 Activity 发送到 Fragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有很多关于此的主题.我也多次阅读文档,但我找不到将数据从活动传递到片段的最佳方式.

I know there are many topics about this here. I have also read documentation many times but I can't find the best way to pass data from activity to fragment.

我希望能够使用 使用标签滑动视图.我必须将 2 个数据传递给片段:currentLocation"是当前用户位置,result"是对象列表.

I want to be able to show the results of my Searchable activity in two differents layouts (list and map) using swipe Views with tabs. I have to pass 2 data to the fragments: "currentLocation" which is the current user location and "result" which is a list of objects.

为了更易于理解,我省略了部分代码.

I have omited some parts of my code to make it more understandable.

SearchableActivity.java

SearchableActivity.java

public class SearchableActivity extends ActionBarActivity implements TabListener {

    List<PlaceModel> result = new ArrayList<PlaceModel>();
    private SearchView mSearchView;
    private String currentLocation;

    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
        ViewPager mViewPager;

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

    setContentView(R.layout.activity_searchable);

    final ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mAppSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    actionBar.addTab(actionBar.newTab().setText("List").setTabListener(this));
    actionBar.addTab(actionBar.newTab().setText("Map").setTabListener(this));

    // get currentLocation here

    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {

        final String query = intent.getStringExtra(SearchManager.QUERY);

        // get result here
    }
}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

}

PlaceListFragment.java

PlaceListFragment.java

public class PlaceListFragment extends Fragment {
    ListView listViewData;
    PlaceAdapter placeAdapter;
    List<PlaceModel> result = new ArrayList<PlaceModel>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_list, container, false);
        Bundle args = getArguments();
        listViewData = (ListView) rootView.findViewById(android.R.id.list);
        // I will pass result and currentLocation here
        placeAdapter = new PlaceAdapter(getActivity(), R.layout.fragment_list_item, result, currentLocation);
        listViewData.setAdapter(placeAdapter);
        return rootView;
    }
}

AppSectionsPagerAdapter.java

AppSectionsPagerAdapter.java

public class AppSectionsPagerAdapter extends FragmentPagerAdapter {

final int PAGE_COUNT = 2;

public AppSectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int arg0) {
    Bundle data = new Bundle();
    switch(arg0) {
        case 0:
            PlaceListFragment fragment1 = new PlaceListFragment();
            fragment1.setArguments(data);
            return fragment1;
        default:
            PlaceListFragment fragment2 = new PlaceListFragment();
            fragment2.setArguments(data);
            return fragment2;
    }
}

@Override
public int getCount() {
    return PAGE_COUNT;
}
}

推荐答案

通常活动会引用其片段.在您的 SearchableActivity.java 中,您是否还在 setContentView(activity_searchable.xml); 中加载 PlaceListFragment.java 或者您需要创建一个实例片段并使用 FragmentTransaction 添加/替换片段.

Usually the activities will have a reference to their fragments. In your SearchableActivity.java are you also loading PlaceListFragment.java either in setContentView(activity_searchable.xml); or you need to create a instance of the fragment and add/replace a fragment using FragmentTransaction.

你可以在这里找到一个关于如何在片段之间或活动与活动之间进行通信的很好的例子.片段.

you can find a good example here on how to communicated between fragments or between activity & fragment.

培训链接

这篇关于如何将数据从 Activity 发送到 Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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